UnsyncByteArrayOutputStream extends OutputStream { @Override public String toString() { return new String(_buffer, 0, _index); } 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 testToString() throws UnsupportedEncodingException { UnsyncByteArrayOutputStream unsyncByteArrayOutputStream = new UnsyncByteArrayOutputStream(); unsyncByteArrayOutputStream.write(_BUFFER); Assert.assertEquals( new String(_BUFFER), unsyncByteArrayOutputStream.toString()); String charsetName1 = "UTF-16BE"; String charsetName2 = "UTF-16LE"; String s = new String(_BUFFER, charsetName1); Assert.assertFalse( s.equals(unsyncByteArrayOutputStream.toString(charsetName2))); Assert.assertEquals( new String(_BUFFER, charsetName1), unsyncByteArrayOutputStream.toString(charsetName1)); Assert.assertEquals( new String(_BUFFER, charsetName2), unsyncByteArrayOutputStream.toString(charsetName2)); }