UnsafeByteArrayOutputStream extends OutputStream { public ByteBuffer toByteBuffer() { return ByteBuffer.wrap(mBuffer, 0, mCount); } UnsafeByteArrayOutputStream();  UnsafeByteArrayOutputStream(int size); @Override void write(int b); @Override void write(byte b[], int off, int len); int size(); void reset(); byte[] toByteArray(); ByteBuffer toByteBuffer(); void writeTo(OutputStream out); @Override String toString(); String toString(String charset); @Override void close();  }
@Test public void testToByteBuffer() { UnsafeByteArrayOutputStream outputStream = new UnsafeByteArrayOutputStream(1); outputStream.write((int) 'a'); ByteBuffer byteBuffer = outputStream.toByteBuffer(); assertThat(byteBuffer.get(), is("a".getBytes()[0])); }