UnsyncByteArrayOutputStream extends OutputStream { public byte[] toByteArray() { byte[] newBuffer = new byte[_index]; System.arraycopy(_buffer, 0, newBuffer, 0, _index); return newBuffer; } UnsyncByteArrayOutputStream();  UnsyncByteArrayOutputStream(int size); void reset(); int size(); byte[] toByteArray(); @Override String toString(); String toString(String charsetName); byte[] unsafeGetByteArray(); ByteBuffer unsafeGetByteBuffer(); @Override void write(byte[] bytes); @Override void write(byte[] bytes, int offset, int length); @Override void write(int b); void writeTo(OutputStream outputStream);  }
@Test public void testToByteArray() { UnsyncByteArrayOutputStream unsyncByteArrayOutputStream = new UnsyncByteArrayOutputStream(); unsyncByteArrayOutputStream.write(_BUFFER); byte[] bytes1 = unsyncByteArrayOutputStream.toByteArray(); Assert.assertTrue(Arrays.equals(_BUFFER, bytes1)); byte[] bytes2 = unsyncByteArrayOutputStream.toByteArray(); Assert.assertTrue(Arrays.equals(_BUFFER, bytes2)); Assert.assertNotSame(bytes1, bytes2); }