ByteBufferUtils { public static String readString(ByteBuffer buffer)throws CharacterCodingException { int position = buffer.position(); CharBuffer charBuffer = DECODER.decode(buffer); buffer.position(position); return charBuffer.toString(); }  static String readString(ByteBuffer buffer);  }
@Test public void testReadString()throws Exception { String string = "I like cake"; ByteBuffer buffer = ByteBuffer.wrap(string.getBytes()); int positionBeforeCall = buffer.position(); assertEquals(string, ByteBufferUtils.readString(buffer)); assertTrue(positionBeforeCall==buffer.position()); }