Java Code Examples for java.nio.charset.CharsetEncoder#encode()

The following examples show how to use java.nio.charset.CharsetEncoder#encode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractSqlPatternMatcher.java    From Bats with Apache License 2.0 6 votes vote down vote up
public AbstractSqlPatternMatcher(String patternString) {
  this.patternString = patternString;

  final CharsetEncoder charsetEncoder = Charsets.UTF_8.newEncoder();
  final CharBuffer patternCharBuffer = CharBuffer.wrap(patternString);

  try {
    patternByteBuffer = charsetEncoder.encode(patternCharBuffer);
  } catch (CharacterCodingException e) {
    throw UserException.validationError(e)
        .message("Failure to encode pattern %s using UTF-8", patternString)
        .addContext("Message: ", e.getMessage())
        .build(logger);
  }
  patternLength = patternByteBuffer.limit();
}
 
Example 2
Source File: ZipCoder.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
byte[] getBytes(String s) {
    CharsetEncoder ce = encoder().reset();
    char[] ca = s.toCharArray();
    int len = (int)(ca.length * ce.maxBytesPerChar());
    byte[] ba = new byte[len];
    if (len == 0)
        return ba;
    ByteBuffer bb = ByteBuffer.wrap(ba);
    CharBuffer cb = CharBuffer.wrap(ca);
    CoderResult cr = ce.encode(cb, bb, true);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    cr = ce.flush(bb);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    if (bb.position() == ba.length)  // defensive copy?
        return ba;
    else
        return Arrays.copyOf(ba, bb.position());
}
 
Example 3
Source File: CharsetEncoderTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public void testFlushWithoutEndOfInput() throws Exception {
    Charset cs = Charset.forName("UTF-32BE");
    CharsetEncoder e = cs.newEncoder();
    ByteBuffer bb = ByteBuffer.allocate(128);
    CoderResult cr = e.encode(CharBuffer.wrap(new char[] { 'x' }), bb, false);
    assertEquals(CoderResult.UNDERFLOW, cr);
    assertEquals(4, bb.position());
    try {
        cr = e.flush(bb);
        fail();
    } catch (IllegalStateException expected) {
        // You must call encode with endOfInput true before you can flush.
    }

    // We had a bug where we wouldn't reset inEnd before calling encode in implFlush.
    // That would result in flush outputting garbage.
    cr = e.encode(CharBuffer.wrap(new char[] { 'x' }), bb, true);
    assertEquals(CoderResult.UNDERFLOW, cr);
    assertEquals(8, bb.position());
    cr = e.flush(bb);
    assertEquals(CoderResult.UNDERFLOW, cr);
    assertEquals(8, bb.position());
}
 
Example 4
Source File: ZipCoder.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
byte[] getBytes(String s) {
    CharsetEncoder ce = encoder().reset();
    char[] ca = s.toCharArray();
    int len = (int)(ca.length * ce.maxBytesPerChar());
    byte[] ba = new byte[len];
    if (len == 0)
        return ba;
    ByteBuffer bb = ByteBuffer.wrap(ba);
    CharBuffer cb = CharBuffer.wrap(ca);
    CoderResult cr = ce.encode(cb, bb, true);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    cr = ce.flush(bb);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    if (bb.position() == ba.length)  // defensive copy?
        return ba;
    else
        return Arrays.copyOf(ba, bb.position());
}
 
Example 5
Source File: CharsetTest.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
@Test
public void testUTF8Charset() throws CharacterCodingException {
    final String m = "Mün";
    final Charset cs = StandardCharsets.UTF_8;
    final CharsetEncoder encoder = cs.newEncoder();
    final ByteBuffer bb = encoder.encode(CharBuffer.wrap(new char[] {'M','ü','n'}));
    final byte[] arr = bb.array();
    Assert.assertEquals(77, arr[0]);
    Assert.assertEquals(-61, arr[1]);
    Assert.assertEquals(-68, arr[2]);

    //final byte[] theBytes = Arrays.copyOf(bb.array(), bb.limit());
    //Assert.assertEquals(4, theBytes.length);
    //Assert.assertEquals(77, theBytes[0]);
    //Assert.assertEquals(-61, theBytes[1]);
    //Assert.assertEquals(-68, theBytes[2]);
    //Assert.assertEquals(110, theBytes[3]);
}
 
Example 6
Source File: Utf7ImeHelper.java    From uiautomator-unicode-input-helper with Apache License 2.0 6 votes vote down vote up
private static ByteBuffer encode(CharBuffer in, CharsetEncoder encoder) {
    int length = (int) (in.remaining() * (double) encoder.averageBytesPerChar());
    ByteBuffer out = ByteBuffer.allocate(length);

    encoder.reset();
    CoderResult flushResult = null;

    while (flushResult != CoderResult.UNDERFLOW) {
        CoderResult encodeResult = encoder.encode(in, out, true);
        if (encodeResult == CoderResult.OVERFLOW) {
            out = allocateMore(out);
            continue;
        }

        flushResult = encoder.flush(out);
        if (flushResult == CoderResult.OVERFLOW) {
            out = allocateMore(out);
        }
    }

    out.flip();
    return out;
}
 
Example 7
Source File: IppUtil.java    From cups4j with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 
 * @param str
 * @param charsetName
 * @return
 * @throws CharacterCodingException
 */
static public String getTranslatedString(String str, String charsetName) throws CharacterCodingException {
  if (charsetName == null) {
    charsetName = DEFAULT_CHARSET;
  }
  Charset charset = Charset.forName(charsetName);
  CharsetDecoder decoder = charset.newDecoder();
  CharsetEncoder encoder = charset.newEncoder();
  decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
  encoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
  // Convert a string to charsetName bytes in a ByteBuffer
  // The new ByteBuffer is ready to be read.
  ByteBuffer buf = encoder.encode(CharBuffer.wrap(str));
  // Convert charsetName bytes in a ByteBuffer to a character ByteBuffer
  // and then to a string. The new ByteBuffer is ready to be read.
  CharBuffer cbuf = decoder.decode(buf);
  return cbuf.toString();
}
 
Example 8
Source File: AsciiType.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public ByteBuffer fromString(String source)
{
    // the encoder must be reset each time it's used, hence the thread-local storage
    CharsetEncoder theEncoder = encoder.get();
    theEncoder.reset();

    try
    {
        return theEncoder.encode(CharBuffer.wrap(source));
    }
    catch (CharacterCodingException exc)
    {
        throw new MarshalException(String.format("Invalid ASCII character in string literal: %s", exc));
    }
}
 
Example 9
Source File: StringCoding.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
static byte[] encode(Charset cs, char[] ca, int off, int len) {
    CharsetEncoder ce = cs.newEncoder();
    int en = scale(len, ce.maxBytesPerChar());
    byte[] ba = new byte[en];
    if (len == 0)
        return ba;
    boolean isTrusted = false;
    if (System.getSecurityManager() != null) {
        if (!(isTrusted = (cs.getClass().getClassLoader0() == null))) {
            ca =  Arrays.copyOfRange(ca, off, off + len);
            off = 0;
        }
    }
    ce.onMalformedInput(CodingErrorAction.REPLACE)
      .onUnmappableCharacter(CodingErrorAction.REPLACE)
      .reset();
    if (ce instanceof ArrayEncoder) {
        int blen = ((ArrayEncoder)ce).encode(ca, off, len, ba);
        return safeTrim(ba, blen, cs, isTrusted);
    } else {
        ByteBuffer bb = ByteBuffer.wrap(ba);
        CharBuffer cb = CharBuffer.wrap(ca, off, len);
        try {
            CoderResult cr = ce.encode(cb, bb, true);
            if (!cr.isUnderflow())
                cr.throwException();
            cr = ce.flush(bb);
            if (!cr.isUnderflow())
                cr.throwException();
        } catch (CharacterCodingException x) {
            throw new Error(x);
        }
        return safeTrim(ba, bb.position(), cs, isTrusted);
    }
}
 
Example 10
Source File: MD5DigesterTest.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
@Test
public void test_findAndReplaceEncodedClientLineEndingIfRequireLineEndingCovert_3()
        throws IOException, NoSuchMethodException, IllegalAccessException,
        InvocationTargetException {
    byte[] sourceBytes = new byte[] { 'q', 'c', 'e', '\r', '\n', '\r', '\n', 'd', 'h', 'b',
            '\n', '\r', '\n' };
    RpcUnicodeInputStream unicodeInputStream = new RpcUnicodeInputStream(
            new ByteArrayInputStream(sourceBytes));
    InputStreamReader encodedStreamReader = new InputStreamReader(unicodeInputStream,
            StandardCharsets.US_ASCII);
    CharsetEncoder utf8CharsetEncoder = CharsetDefs.UTF8.newEncoder()
            .onMalformedInput(CodingErrorAction.REPORT)
            .onUnmappableCharacter(CodingErrorAction.REPORT);
    char[] buffer = new char[9];
    buffer[0] = 'q';
    buffer[1] = 'c';
    buffer[2] = 'e';
    buffer[3] = '\r';
    buffer[4] = '\n';
    buffer[5] = '\r';
    buffer[6] = '\n';
    buffer[7] = 'd';
    ByteBuffer utf8ByteBuffer = utf8CharsetEncoder.encode(CharBuffer.wrap(buffer, 0, 8));
    encodedStreamReader.read(new char[8]);

    ByteBuffer actual = invokePrivate_findAndReplaceEncodedClientLineEndingIfRequireLineEndingCovert(
            encodedStreamReader, utf8CharsetEncoder, utf8ByteBuffer,
            ClientLineEnding.FST_L_CRLF);

    byte[] expectedBytes = new byte[] { 'q', 'c', 'e', '\n', '\n', 'd' };
    ByteBuffer expected = ByteBuffer.wrap(expectedBytes, 0, 6);

    assertThat(actual, is(expected));
}
 
Example 11
Source File: ZipCoder.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
byte[] getBytes(String s) {
    CharsetEncoder ce = encoder().reset();
    char[] ca = s.toCharArray();
    int len = (int)(ca.length * ce.maxBytesPerChar());
    byte[] ba = new byte[len];
    if (len == 0)
        return ba;
    // UTF-8 only for now. Other ArrayDeocder only handles
    // CodingErrorAction.REPLACE mode.
    if (isUTF8 && ce instanceof ArrayEncoder) {
        int blen = ((ArrayEncoder)ce).encode(ca, 0, ca.length, ba);
        if (blen == -1)    // malformed
            throw new IllegalArgumentException("MALFORMED");
        return Arrays.copyOf(ba, blen);
    }
    ByteBuffer bb = ByteBuffer.wrap(ba);
    CharBuffer cb = CharBuffer.wrap(ca);
    CoderResult cr = ce.encode(cb, bb, true);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    cr = ce.flush(bb);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    if (bb.position() == ba.length)  // defensive copy?
        return ba;
    else
        return Arrays.copyOf(ba, bb.position());
}
 
Example 12
Source File: MD5DigesterTest.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
@Test
public void test_findAndReplaceEncodedClientLineEndingIfRequireLineEndingCovert()
        throws IOException, NoSuchMethodException, IllegalAccessException,
        InvocationTargetException {
    byte[] sourceBytes = new byte[] { 'q', 'c', 'e', '\r', '\n', '\r', '\n', 'd', 'h', 'b',
            '\n', '\r', '\n' };
    RpcUnicodeInputStream unicodeInputStream = new RpcUnicodeInputStream(
            new ByteArrayInputStream(sourceBytes));
    InputStreamReader encodedStreamReader = new InputStreamReader(unicodeInputStream,
            StandardCharsets.US_ASCII);
    CharsetEncoder utf8CharsetEncoder = CharsetDefs.UTF8.newEncoder()
            .onMalformedInput(CodingErrorAction.REPORT)
            .onUnmappableCharacter(CodingErrorAction.REPORT);
    char[] buffer = new char[5];
    buffer[0] = 'q';
    buffer[1] = 'c';
    buffer[2] = 'e';
    buffer[3] = '\r';
    ByteBuffer utf8ByteBuffer = utf8CharsetEncoder.encode(CharBuffer.wrap(buffer, 0, 4));
    encodedStreamReader.read(new char[4]);

    ByteBuffer actual = invokePrivate_findAndReplaceEncodedClientLineEndingIfRequireLineEndingCovert(
            encodedStreamReader, utf8CharsetEncoder, utf8ByteBuffer,
            ClientLineEnding.FST_L_CRLF);

    byte[] expectedBytes = new byte[] { 'q', 'c', 'e', '\n' };
    ByteBuffer expected = ByteBuffer.wrap(expectedBytes, 0, 4);

    assertThat(actual, is(expected));
}
 
Example 13
Source File: StringCoding.java    From Java8CN with Apache License 2.0 5 votes vote down vote up
static byte[] encode(Charset cs, char[] ca, int off, int len) {
    CharsetEncoder ce = cs.newEncoder();
    int en = scale(len, ce.maxBytesPerChar());
    byte[] ba = new byte[en];
    if (len == 0)
        return ba;
    boolean isTrusted = false;
    if (System.getSecurityManager() != null) {
        if (!(isTrusted = (cs.getClass().getClassLoader0() == null))) {
            ca =  Arrays.copyOfRange(ca, off, off + len);
            off = 0;
        }
    }
    ce.onMalformedInput(CodingErrorAction.REPLACE)
      .onUnmappableCharacter(CodingErrorAction.REPLACE)
      .reset();
    if (ce instanceof ArrayEncoder) {
        int blen = ((ArrayEncoder)ce).encode(ca, off, len, ba);
        return safeTrim(ba, blen, cs, isTrusted);
    } else {
        ByteBuffer bb = ByteBuffer.wrap(ba);
        CharBuffer cb = CharBuffer.wrap(ca, off, len);
        try {
            CoderResult cr = ce.encode(cb, bb, true);
            if (!cr.isUnderflow())
                cr.throwException();
            cr = ce.flush(bb);
            if (!cr.isUnderflow())
                cr.throwException();
        } catch (CharacterCodingException x) {
            throw new Error(x);
        }
        return safeTrim(ba, bb.position(), cs, isTrusted);
    }
}
 
Example 14
Source File: MD5DigesterTest.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
@Test
public void test_findAndReplaceEncodedClientLineEndingIfRequireLineEndingCovert_3()
        throws IOException, NoSuchMethodException, IllegalAccessException,
        InvocationTargetException {
    byte[] sourceBytes = new byte[] { 'q', 'c', 'e', '\r', '\n', '\r', '\n', 'd', 'h', 'b',
            '\n', '\r', '\n' };
    RpcUnicodeInputStream unicodeInputStream = new RpcUnicodeInputStream(
            new ByteArrayInputStream(sourceBytes));
    InputStreamReader encodedStreamReader = new InputStreamReader(unicodeInputStream,
            StandardCharsets.US_ASCII);
    CharsetEncoder utf8CharsetEncoder = CharsetDefs.UTF8.newEncoder()
            .onMalformedInput(CodingErrorAction.REPORT)
            .onUnmappableCharacter(CodingErrorAction.REPORT);
    char[] buffer = new char[9];
    buffer[0] = 'q';
    buffer[1] = 'c';
    buffer[2] = 'e';
    buffer[3] = '\r';
    buffer[4] = '\n';
    buffer[5] = '\r';
    buffer[6] = '\n';
    buffer[7] = 'd';
    ByteBuffer utf8ByteBuffer = utf8CharsetEncoder.encode(CharBuffer.wrap(buffer, 0, 8));
    encodedStreamReader.read(new char[8]);

    ByteBuffer actual = invokePrivate_findAndReplaceEncodedClientLineEndingIfRequireLineEndingCovert(
            encodedStreamReader, utf8CharsetEncoder, utf8ByteBuffer,
            ClientLineEnding.FST_L_CRLF);

    byte[] expectedBytes = new byte[] { 'q', 'c', 'e', '\n', '\n', 'd' };
    ByteBuffer expected = ByteBuffer.wrap(expectedBytes, 0, 6);

    assertThat(actual, is(expected));
}
 
Example 15
Source File: StringCoding.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
static byte[] encode(Charset cs, char[] ca, int off, int len) {
    CharsetEncoder ce = cs.newEncoder();
    int en = scale(len, ce.maxBytesPerChar());
    byte[] ba = new byte[en];
    if (len == 0)
        return ba;
    boolean isTrusted = false;
    if (System.getSecurityManager() != null) {
        if (!(isTrusted = (cs.getClass().getClassLoader0() == null))) {
            ca =  Arrays.copyOfRange(ca, off, off + len);
            off = 0;
        }
    }
    ce.onMalformedInput(CodingErrorAction.REPLACE)
      .onUnmappableCharacter(CodingErrorAction.REPLACE)
      .reset();
    if (ce instanceof ArrayEncoder) {
        int blen = ((ArrayEncoder)ce).encode(ca, off, len, ba);
        return safeTrim(ba, blen, cs, isTrusted);
    } else {
        ByteBuffer bb = ByteBuffer.wrap(ba);
        CharBuffer cb = CharBuffer.wrap(ca, off, len);
        try {
            CoderResult cr = ce.encode(cb, bb, true);
            if (!cr.isUnderflow())
                cr.throwException();
            cr = ce.flush(bb);
            if (!cr.isUnderflow())
                cr.throwException();
        } catch (CharacterCodingException x) {
            throw new Error(x);
        }
        return safeTrim(ba, bb.position(), cs, isTrusted);
    }
}
 
Example 16
Source File: StringCoding.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
static byte[] encode(Charset cs, char[] ca, int off, int len) {
    CharsetEncoder ce = cs.newEncoder();
    int en = scale(len, ce.maxBytesPerChar());
    byte[] ba = new byte[en];
    if (len == 0)
        return ba;
    boolean isTrusted = false;
    if (System.getSecurityManager() != null) {
        if (!(isTrusted = (cs.getClass().getClassLoader0() == null))) {
            ca =  Arrays.copyOfRange(ca, off, off + len);
            off = 0;
        }
    }
    ce.onMalformedInput(CodingErrorAction.REPLACE)
      .onUnmappableCharacter(CodingErrorAction.REPLACE)
      .reset();
    if (ce instanceof ArrayEncoder) {
        int blen = ((ArrayEncoder)ce).encode(ca, off, len, ba);
        return safeTrim(ba, blen, cs, isTrusted);
    } else {
        ByteBuffer bb = ByteBuffer.wrap(ba);
        CharBuffer cb = CharBuffer.wrap(ca, off, len);
        try {
            CoderResult cr = ce.encode(cb, bb, true);
            if (!cr.isUnderflow())
                cr.throwException();
            cr = ce.flush(bb);
            if (!cr.isUnderflow())
                cr.throwException();
        } catch (CharacterCodingException x) {
            throw new Error(x);
        }
        return safeTrim(ba, bb.position(), cs, isTrusted);
    }
}
 
Example 17
Source File: StringCodec.java    From camunda-bpm-reactor with Apache License 2.0 5 votes vote down vote up
protected Buffer encode(String s, CharsetEncoder charsetEncoder) {
  try {
    ByteBuffer bb = charsetEncoder.encode(CharBuffer.wrap(s));
    if (delimiter != null) {
      return addDelimiterIfAny(new Buffer().append(bb));
    } else {
      return new Buffer(bb);
    }
  } catch (CharacterCodingException e) {
    throw new IllegalStateException(e);
  }
}
 
Example 18
Source File: ByteBufUtil.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
static ByteBuf encodeString0(ByteBufAllocator alloc, boolean enforceHeap, CharBuffer src, Charset charset) {
    final CharsetEncoder encoder = CharsetUtil.getEncoder(charset);
    int length = (int) ((double) src.remaining() * encoder.maxBytesPerChar());
    boolean release = true;
    final ByteBuf dst;
    if (enforceHeap) {
        dst = alloc.heapBuffer(length);
    } else {
        dst = alloc.buffer(length);
    }
    try {
        final ByteBuffer dstBuf = dst.internalNioBuffer(0, length);
        final int pos = dstBuf.position();
        CoderResult cr = encoder.encode(src, dstBuf, true);
        if (!cr.isUnderflow()) {
            cr.throwException();
        }
        cr = encoder.flush(dstBuf);
        if (!cr.isUnderflow()) {
            cr.throwException();
        }
        dst.writerIndex(dst.writerIndex() + dstBuf.position() - pos);
        release = false;
        return dst;
    } catch (CharacterCodingException x) {
        throw new IllegalStateException(x);
    } finally {
        if (release) {
            dst.release();
        }
    }
}
 
Example 19
Source File: AbstractIoBuffer.java    From neoscada with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public IoBuffer putString(CharSequence val, int fieldSize, CharsetEncoder encoder) throws CharacterCodingException {
    checkFieldSize(fieldSize);

    if (fieldSize == 0) {
        return this;
    }

    autoExpand(fieldSize);

    boolean utf16 = encoder.charset().name().startsWith("UTF-16");

    if (utf16 && (fieldSize & 1) != 0) {
        throw new IllegalArgumentException("fieldSize is not even.");
    }

    int oldLimit = limit();
    int end = position() + fieldSize;

    if (oldLimit < end) {
        throw new BufferOverflowException();
    }

    if (val.length() == 0) {
        if (!utf16) {
            put((byte) 0x00);
        } else {
            put((byte) 0x00);
            put((byte) 0x00);
        }
        position(end);
        return this;
    }

    CharBuffer in = CharBuffer.wrap(val);
    limit(end);
    encoder.reset();

    for (;;) {
        CoderResult cr;
        if (in.hasRemaining()) {
            cr = encoder.encode(in, buf(), true);
        } else {
            cr = encoder.flush(buf());
        }

        if (cr.isUnderflow() || cr.isOverflow()) {
            break;
        }
        cr.throwException();
    }

    limit(oldLimit);

    if (position() < end) {
        if (!utf16) {
            put((byte) 0x00);
        } else {
            put((byte) 0x00);
            put((byte) 0x00);
        }
    }

    position(end);
    return this;
}
 
Example 20
Source File: AbstractAdaptiveByteBuffer.java    From craft-atom with MIT License 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public AdaptiveByteBuffer putString(CharSequence val, CharsetEncoder encoder) throws CharacterCodingException {
    if (val.length() == 0) {
        return this;
    }

    CharBuffer in = CharBuffer.wrap(val);
    encoder.reset();

    int expandedState = 0;

    for (;;) {
        CoderResult cr;
        if (in.hasRemaining()) {
            cr = encoder.encode(in, buf(), true);
        } else {
            cr = encoder.flush(buf());
        }

        if (cr.isUnderflow()) {
            break;
        }
        if (cr.isOverflow()) {
            if (isAutoExpand()) {
                switch (expandedState) {
                case 0:
                    autoExpand((int) Math.ceil(in.remaining() * encoder.averageBytesPerChar()));
                    expandedState++;
                    break;
                case 1:
                    autoExpand((int) Math.ceil(in.remaining() * encoder.maxBytesPerChar()));
                    expandedState++;
                    break;
                default:
                    throw new RuntimeException("Expanded by "
                            + (int) Math.ceil(in.remaining() * encoder.maxBytesPerChar())
                            + " but that wasn't enough for '" + val + "'");
                }
                continue;
            }
        } else {
            expandedState = 0;
        }
        cr.throwException();
    }
    return this;
}