ReadWrite { public static String byteBufferToString (final ByteBuffer buffer) { buffer.rewind(); return new String(buffer.array()); } private  ReadWrite(); static final int readOneByteInt(ByteBuffer buffer, int start); static final int readOneByteInt(byte[] array, int start); static final int readTwoByteInt(ByteBuffer buffer, int start); static final int readTwoByteInt(byte[] array, int start); static final int readThreeByteInt(ByteBuffer buffer, int start); static final int readThreeByteInt(byte[] array, int start); static final int readFourByteInt(ByteBuffer buffer, int start); static final int readFourByteInt(byte[] array, int start); static ByteBuffer[] stringToTextDataSegments(String string, int bufferSize); static final void appendTextDataSegmentToStringBuffer(final ByteBuffer byteBuffer, final StringBuilder stringBuilder); static final void writeInt(int value, final ByteBuffer buffer, int start); static String byteBufferToString(final ByteBuffer buffer); static byte[] longToBytes(final long value); static void writeLong(final ByteBuffer buffer, final long value, final int index); static void writeTwoByteInt(final ByteBuffer buffer, final int value, final int index); static void writeThreeByteInt(final ByteBuffer buffer, final int value, final int index); static long readUnsignedInt(final ByteBuffer buffer, final int start);  }
@Test public void testByteBufferToString () { ByteBuffer stringBuffer = ByteBuffer.allocate(32); String hello = "hello world"; stringBuffer.put(hello.getBytes()); String s = ReadWrite.byteBufferToString(stringBuffer); Assert.assertEquals(hello, s.trim()); }