ExposedByteArrayOutputStream extends ByteArrayOutputStream { @Override public synchronized byte[] toByteArray() { if (!isFallback && count > 0) { return buf; } else { return super.toByteArray(); } }  synchronized void writeAndOwn(byte[] b); @Override synchronized void write(byte[] b, int off, int len); @Override synchronized void write(int b); @Override // Exposes internal mutable reference by design - Spotbugs is right to warn that this is dangerous synchronized byte[] toByteArray(); @Override synchronized void reset();  }
@Test public void testWriteSingleArray() throws IOException { writeToBoth(TEST_DATA); assertStreamContentsEquals(stream, exposedStream); assertNotSame(TEST_DATA, exposedStream.toByteArray()); }
@Test public void testWriteSingleArrayFast() throws IOException { writeToBothFast(TEST_DATA); assertStreamContentsEquals(stream, exposedStream); assertSame(TEST_DATA, exposedStream.toByteArray()); }
@Test public void testWriteSingleArrayWithLength() { writeToBoth(TEST_DATA, 0, TEST_DATA.length); assertStreamContentsEquals(stream, exposedStream); assertNotSame(TEST_DATA, exposedStream.toByteArray()); }
@Test public void testWriteArrayFastAfterReset() throws IOException { writeToBothFast(TEST_DATA); resetBoth(); writeToBothFast(TEST_DATA); assertStreamContentsEquals(stream, exposedStream); assertSame(TEST_DATA, exposedStream.toByteArray()); }
@Test public void testWriteArrayFastAfterReset1() throws IOException { writeToBothFast(TEST_DATA); writeToBothFast(TEST_DATA); resetBoth(); writeToBothFast(TEST_DATA); assertStreamContentsEquals(stream, exposedStream); assertSame(TEST_DATA, exposedStream.toByteArray()); }
@Test public void testWriteArrayFastAfterReset2() throws IOException { resetBoth(); writeToBothFast(TEST_DATA); assertStreamContentsEquals(stream, exposedStream); assertSame(TEST_DATA, exposedStream.toByteArray()); }