ChunkedOutputStream extends OutputStream { @Override @SuppressWarnings("checkstyle:ParameterName") public void write(final int b) throws IOException { checkNotClosed(); ensureOneByte(); currentChunk[currentOffset] = (byte) b; currentOffset++; size++; } ChunkedOutputStream(final int requestedInitialCapacity); @Override @SuppressWarnings("checkstyle:ParameterName") void write(final int b); @Override @SuppressWarnings("checkstyle:ParameterName") void write(final byte[] b, final int off, final int len); @Override void close();  }
@Test public void testBasicWrite() throws IOException { for (int i = 0; i < INITIAL_SIZE; ++i) { stream.write(i); } final byte[] chunk = assertFinishedStream(INITIAL_SIZE, 1).get(0); assertEquals(INITIAL_SIZE, chunk.length); for (int i = 0; i < INITIAL_SIZE; ++i) { assertEquals((byte) i, chunk[i]); } }
@Test public void testBasicLargeWrite() throws IOException { final byte[] array = createArray(INITIAL_SIZE); stream.write(array); final byte[] chunk = assertFinishedStream(INITIAL_SIZE, 1).get(0); assertArrayEquals(array, chunk); }
@Test public void testGrowWrite() throws IOException { for (int i = 0; i < INITIAL_SIZE * 2; ++i) { stream.write(i); } final byte[] chunk = assertFinishedStream(INITIAL_SIZE * 2, 1).get(0); assertEquals(INITIAL_SIZE * 2, chunk.length); for (int i = 0; i < INITIAL_SIZE * 2; ++i) { assertEquals((byte) i, chunk[i]); } }
@Test public void testGrowLargeWrite() throws IOException { final byte[] array = createArray(INITIAL_SIZE * 2); stream.write(array); final byte[] chunk = assertFinishedStream(INITIAL_SIZE * 2, 1).get(0); assertArrayEquals(array, chunk); }
@Test public void testTwoChunksWrite() throws IOException { int size = ChunkedOutputStream.MAX_ARRAY_SIZE + 1; for (int i = 0; i < size; ++i) { stream.write(i); } int counter = 0; for (byte[] chunk: assertFinishedStream(size, 2)) { for (byte actual: chunk) { assertEquals((byte) counter++, actual); } } }