ByteBufferOutputStream extends OutputStream { @Override public void write(final int b) { destinationBuffer.put((byte) b); } ByteBufferOutputStream(final ByteBuffer destinationBuffer); @Override void write(final int b); @Override void write(final byte[] b, final int off, final int len);  }
@Test public void writesThroughToBuffer() throws IOException { final int expectedCount = 11; final ByteBuffer buffer = ByteBuffer.allocate(expectedCount); final ByteBufferOutputStream outputStream = new ByteBufferOutputStream(buffer); for (int i = 0; i < 5; i++) { outputStream.write(i); } outputStream.write(new byte[]{7,8,9}); outputStream.write(new byte[]{10,11,12,13,14}, 1, 3); Assertions.assertThat(buffer.position()).isEqualTo(expectedCount); }