BufferUtil { public static void write(ByteBuffer source, OutputStream destination) throws IOException { int length = source.limit() - source.position(); if (length == 0) { return; } byte[] data = new byte[length]; source.get(data); destination.write(data); }  static void write(ByteBuffer source, OutputStream destination); static long readUInt32(ByteBuffer source); static int readUInt24(ByteBuffer source); static int readUInt16(ByteBuffer source); static int readUInt8(ByteBuffer source); static byte[] readOpaque8(ByteBuffer source); static byte[] readOpaque16(ByteBuffer source); static byte[] readOpaque24(ByteBuffer source); static void debug(ByteBuffer buffer, int length);  }
@Test void testWrite() throws IOException { String input = "hello world"; buffer.put(input.getBytes()); buffer.flip(); BufferUtil.write(buffer, output); byte[] result = output.toByteArray(); assertEquals(input, new String(result)); }