ByteBufferExample { public void enCode(CharBuffer cb) throws CharacterCodingException { Charset latin1 = Charset.forName( "ISO-8859-1" ); CharsetEncoder encoder = latin1.newEncoder(); ByteBuffer bb = encoder.encode(cb.asReadOnlyBuffer()); buffer.put(bb); }  ByteBuffer put(byte b); ByteBuffer put(byte src[]); ByteBuffer put(byte src[], int offset, int length); ByteBuffer put(ByteBuffer src); ByteBuffer put(int index, byte b); byte get(); CharBuffer decode(); void enCode(CharBuffer cb);  }
@Test public void enCode() throws CharacterCodingException { ByteBufferExample buffer = new ByteBufferExample(); char destArry[] = new char[1]; destArry[0] = 'a'; CharBuffer cb = CharBuffer.wrap(destArry); buffer.enCode(cb); buffer.buffer.flip(); String aa = buffer.decode().toString(); Assert.assertEquals("a", aa); }