Serializer { public ByteBuffer toByteBuffer() { ByteBuffer byteBuffer = ByteBuffer.wrap(Arrays.copyOf(_buffer, _index)); if (_buffer.length <= _THREADLOCAL_BUFFER_SIZE_LIMIT) { BufferQueue bufferQueue = _getBufferQueue(); bufferQueue.enqueue(_buffer); } _buffer = null; return byteBuffer; } Serializer(); ByteBuffer toByteBuffer(); void writeBoolean(boolean b); void writeByte(byte b); void writeChar(char c); void writeDouble(double d); void writeFloat(float f); void writeInt(int i); void writeLong(long l); void writeObject(Serializable serializable); void writeShort(short s); void writeString(String s); void writeTo(OutputStream outputStream);  }
@Test public void testBufferOutputStream() throws Exception { byte[] data = new byte[1024]; _random.nextBytes(data); Serializer serializer = new Serializer(); BufferOutputStream bufferOutputStream = new BufferOutputStream( serializer); for (byte b : data) { bufferOutputStream.write(b); } ByteBuffer byteBuffer = serializer.toByteBuffer(); Assert.assertArrayEquals(data, byteBuffer.array()); serializer = new Serializer(); bufferOutputStream = new BufferOutputStream(serializer); bufferOutputStream.write(data); byteBuffer = serializer.toByteBuffer(); Assert.assertArrayEquals(data, byteBuffer.array()); }