public final ByteBuffer encode ( CharBuffer in ) throws CharacterCodingException { int n = ( int ) ( in . remaining ( ) * averageBytesPerChar ( ) ) ; ByteBuffer out = ByteBuffer . allocate ( n ) ; if ( ( n == 0 ) && ( in . remaining ( ) == 0 ) ) return out ; reset ( ) ; for ( ; ; ) { CoderResult cr = in . hasRemaining ( ) ? encode ( in , out , true ) : CoderResult . UNDERFLOW ; if ( cr . isUnderflow ( ) ) cr = flush ( out ) ; if ( cr . isUnderflow ( ) ) break ; if ( cr . isOverflow ( ) ) { n = 2 * n + 1 ; // Ensure progress; n might be 0! ByteBuffer o = ByteBuffer . allocate ( n ) ; out . flip ( ) ; o . put ( out ) ; out = o ; continue ; } cr . throwException ( ) ; } out . flip ( ) ; return out ; }
Convenience method that encodes the remaining content of a single input character buffer into a newly - allocated byte buffer .