ByteBufferOutputStream extends OutputStream { @Override public void write(byte[] b, int off, int len) { needSpace(len); buffer.put(b, off, len); } ByteBufferOutputStream(); private  ByteBufferOutputStream(ByteBuffer buffer);  ByteBufferOutputStream(int initialSize); ByteBuffer getByteBuffer(); boolean isElastic(); void setElastic(boolean elastic); @Override void write(byte[] b, int off, int len); @Override void write(int b);  }
@Test public void testWrite() throws IOException { byte[] src = "HELLO MINA!".getBytes(); ByteBufferOutputStream bbos = new ByteBufferOutputStream(1024); bbos.write(src); bbos.close(); assertEquals(src.length, bbos.getByteBuffer().remaining()); assertEquals(ByteBuffer.wrap(src), bbos.getByteBuffer()); }