@Override public int write(ByteBuffer data) throws IOException { int size = data.remaining(); ByteBuffers.writeToStream(data, outputStream); return size; }
private void testWriteBuffer(ByteBuffer buffer, String content) throws IOException { ByteArrayOutputStream os = new ByteArrayOutputStream(); // Write the whole buffer ByteBuffers.writeToStream(buffer, os); Assert.assertEquals(content, new String(os.toByteArray(), Charsets.UTF_8)); // Write part of the buffer int pos = content.length() / 2; buffer.position(pos); os.reset(); ByteBuffers.writeToStream(buffer, os); Assert.assertEquals(content.substring(pos), new String(os.toByteArray(), Charsets.UTF_8)); // Write with a slice of the buffer pos = content.length() / 3; buffer.position(pos); os.reset(); ByteBuffers.writeToStream(buffer.slice(), os); Assert.assertEquals(content.substring(pos), new String(os.toByteArray(), Charsets.UTF_8)); } }