ExposedByteArrayInputStream extends ByteArrayInputStream { public byte[] readAll() throws IOException { if (pos == 0 && count == buf.length) { pos = count; return buf; } byte[] ret = new byte[count - pos]; super.read(ret); return ret; } ExposedByteArrayInputStream(byte[] buf); byte[] readAll(); @Override void close();  }
@Test public void testConstructWithEmptyArray() throws IOException { try (ExposedByteArrayInputStream s = new ExposedByteArrayInputStream(new byte[0])) { assertEquals(0, s.available()); byte[] data = s.readAll(); assertEquals(0, data.length); } }
@Test public void testReadAll() throws IOException { assertEquals(TEST_DATA.length, exposedStream.available()); byte[] data = exposedStream.readAll(); assertArrayEquals(TEST_DATA, data); assertSame(TEST_DATA, data); assertEquals(0, exposedStream.available()); }
@Test public void testReadAllAfterReadPartial() throws IOException { assertNotEquals(-1, exposedStream.read()); byte[] ret = exposedStream.readAll(); assertArrayEquals("ello World!".getBytes(Charsets.UTF_8), ret); }