UnsyncBufferedOutputStream extends UnsyncFilterOutputStream { @Override public void write(byte[] bytes) throws IOException { write(bytes, 0, bytes.length); } UnsyncBufferedOutputStream(OutputStream outputStream);  UnsyncBufferedOutputStream(OutputStream outputStream, int size); @Override void flush(); @Override void write(byte[] bytes); @Override void write(byte[] bytes, int offset, int length); @Override void write(int b);  }
@Test public void testBlockWrite() throws Exception { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); UnsyncBufferedOutputStream unsyncBufferedOutputStream = new UnsyncBufferedOutputStream(byteArrayOutputStream, _SIZE * 2); byte[] buffer = (byte[])_bufferField.get(unsyncBufferedOutputStream); Assert.assertEquals(Arrays.toString(buffer), _SIZE * 2, buffer.length); unsyncBufferedOutputStream.write(_BUFFER); for (int i = 0; i < _SIZE; i++) { Assert.assertEquals(i, buffer[i]); } unsyncBufferedOutputStream.write(_BUFFER); for (int i = _SIZE; i < (_SIZE * 2); i++) { Assert.assertEquals(i - _SIZE, buffer[i]); } unsyncBufferedOutputStream.write(100); buffer = (byte[])_bufferField.get(unsyncBufferedOutputStream); Assert.assertEquals(100, buffer[0]); Assert.assertEquals(_SIZE * 2, byteArrayOutputStream.size()); byteArrayOutputStream.reset(); unsyncBufferedOutputStream = new UnsyncBufferedOutputStream( byteArrayOutputStream, _SIZE + 1); unsyncBufferedOutputStream.write(100); unsyncBufferedOutputStream.write(110); Assert.assertEquals(0, byteArrayOutputStream.size()); unsyncBufferedOutputStream.write(_BUFFER); Assert.assertEquals(2, byteArrayOutputStream.size()); byte[] bytes = byteArrayOutputStream.toByteArray(); Assert.assertEquals(100, bytes[0]); Assert.assertEquals(110, bytes[1]); buffer = (byte[])_bufferField.get(unsyncBufferedOutputStream); for (int i = 0; i < _SIZE; i++) { Assert.assertEquals(i, buffer[i]); } byteArrayOutputStream.reset(); unsyncBufferedOutputStream = new UnsyncBufferedOutputStream( byteArrayOutputStream, _SIZE / 2); unsyncBufferedOutputStream.write(_BUFFER); Assert.assertArrayEquals(_BUFFER, byteArrayOutputStream.toByteArray()); }
@Test public void testWrite() throws Exception { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); UnsyncBufferedOutputStream unsyncBufferedOutputStream = new UnsyncBufferedOutputStream(byteArrayOutputStream, _SIZE * 2); byte[] buffer = (byte[])_bufferField.get(unsyncBufferedOutputStream); Assert.assertEquals(Arrays.toString(buffer), _SIZE * 2, buffer.length); for (int i = 0; i < _SIZE; i++) { unsyncBufferedOutputStream.write(i); Assert.assertEquals(i, buffer[i]); } unsyncBufferedOutputStream.flush(); Assert.assertTrue( Arrays.equals(_BUFFER, byteArrayOutputStream.toByteArray())); }