public static ByteBuffer charsToBytes ( CharsetEncoder encoder , CharBuffer chars , ByteBuffer bytes ) throws UnmappableInputException { assert encoder . malformedInputAction ( ) == CodingErrorAction . REPORT ; bytes = clearAndEnsureCapacity ( bytes , ( int ) ( chars . remaining ( ) * encoder . maxBytesPerChar ( ) ) ) ; chars . mark ( ) ; encoder . reset ( ) ; CoderResult cr = encoder . encode ( chars , bytes , true ) ; if ( cr . isError ( ) ) { chars . reset ( ) ; try { cr . throwException ( ) ; } catch ( CharacterCodingException e ) { throw new UnmappableInputException ( "Input cannot be mapped to characters using encoding " + encoder . charset ( ) . name ( ) + ": " + Arrays . toString ( toArray ( bytes ) ) , e ) ; } } assert cr . isUnderflow ( ) ; // This should be guaranteed by ensuring max. capacity. cr = encoder . flush ( bytes ) ; assert cr . isUnderflow ( ) ; bytes . flip ( ) ; chars . reset ( ) ; return bytes ; }
Convert chars into bytes .