public static CharBuffer bytesToChars(CharsetDecoder decoder, ByteBuffer bytes, CharBuffer chars) {
    assert decoder.malformedInputAction() == CodingErrorAction.REPORT;

    chars = clearAndEnsureCapacity(chars, (int) (bytes.remaining() * decoder.maxCharsPerByte()));

    bytes.mark();
    decoder.reset();
    CoderResult cr = decoder.decode(bytes, chars, true);
    if (cr.isError()) {
      bytes.reset();
      try {
        cr.throwException();
      } catch (CharacterCodingException e) {
        throw new RuntimeException("Input cannot be mapped to bytes using encoding "
            + decoder.charset().name() + ": " + Arrays.toString(toArray(bytes)), e);
      }
    }

    assert cr.isUnderflow();  // This should be guaranteed by ensuring max. capacity.
    cr = decoder.flush(chars);
    assert cr.isUnderflow();

    chars.flip();
    bytes.reset();

    return chars;
  }
public static CharBuffer bytesToChars ( CharsetDecoder decoder , ByteBuffer bytes , CharBuffer chars ) { assert decoder . malformedInputAction ( ) == CodingErrorAction . REPORT ; chars = clearAndEnsureCapacity ( chars , ( int ) ( bytes . remaining ( ) * decoder . maxCharsPerByte ( ) ) ) ; bytes . mark ( ) ; decoder . reset ( ) ; CoderResult cr = decoder . decode ( bytes , chars , true ) ; if ( cr . isError ( ) ) { bytes . reset ( ) ; try { cr . throwException ( ) ; } catch ( CharacterCodingException e ) { throw new RuntimeException ( "Input cannot be mapped to bytes using encoding " + decoder . charset ( ) . name ( ) + ": " + Arrays . toString ( toArray ( bytes ) ) , e ) ; } } assert cr . isUnderflow ( ) ; cr = decoder . flush ( chars ) ; assert cr . isUnderflow ( ) ; chars . flip ( ) ; bytes . reset ( ) ; return chars ; }
Convert byte buffer's content into characters. The input buffer's bytes are not
consumed (mark is set and reset).