public void testUnboundedStream() throws IOException { BoundedInputStream bin = new BoundedInputStream(new ByteArrayInputStream(TEST_BYTES), -1, false); assert 0 == bin.getProcessedBytes(); assert Long.MAX_VALUE == bin.getRemainingBytes(); assert -1 == bin.getAllowedBytes(); assert 1 == bin.getProcessedBytes(); assert Long.MAX_VALUE == bin.getRemainingBytes(); assert -1 == bin.getAllowedBytes(); assert 2 == bin.getProcessedBytes(); assert Long.MAX_VALUE == bin.getRemainingBytes(); assert -1 == bin.getAllowedBytes(); assert 3 == bin.getProcessedBytes(); assert Long.MAX_VALUE == bin.getRemainingBytes(); assert -1 == bin.getAllowedBytes();
/** * Performs some tests that are common to {@link #testBoundedStreamWithException()} and * {@link #testBoundedStreamWithoutException()}. * * @param bin the BoundedInputStream to prepare * @throws IOException should not happen */ private void prepareBoundedStream(BoundedInputStream bin) throws IOException { assert 0 == bin.getProcessedBytes(); assert 4 == bin.getRemainingBytes(); assert 4 == bin.getAllowedBytes(); assert bin.read()!=-1; assert 1 == bin.getProcessedBytes(); assert 3 == bin.getRemainingBytes(); assert 4 == bin.getAllowedBytes(); assert bin.read(new byte[1])!=-1; assert 2 == bin.getProcessedBytes(); assert 2 == bin.getRemainingBytes(); assert 4 == bin.getAllowedBytes(); assert bin.read(new byte[1], 0, 1)!=-1; assert 3 == bin.getProcessedBytes(); assert 1 == bin.getRemainingBytes(); assert 4 == bin.getAllowedBytes(); assert bin.skip(1)!=-1; assert 4 == bin.getProcessedBytes(); assert 0 == bin.getRemainingBytes(); assert 4 == bin.getAllowedBytes(); }
/** * Tests a <code>BoundedInputStream</code> operating in bounded mode and returning <code>-1/</code>. * * @throws IOException should not happen */ @Test public void testBoundedStreamWithoutException() throws IOException { BoundedInputStream bin = new BoundedInputStream(new ByteArrayInputStream(TEST_BYTES), 4, false); prepareBoundedStream(bin); assert -1 == bin.read(); assert -1 == bin.read(new byte[1]); assert -1 == bin.read(new byte[1], 0, 1); assert -1 == bin.skip(1); assert 4 == bin.getProcessedBytes(); assert 0 == bin.getRemainingBytes(); assert 4 == bin.getAllowedBytes(); // Attempt to read a chunk larger than the remaining bytes and assert that it does not return -1 bin = new BoundedInputStream(new ByteArrayInputStream(TEST_BYTES), 4, false); assert bin.read(new byte[6])!=-1; }
assert 4 == bin.getProcessedBytes(); assert 0 == bin.getRemainingBytes(); assert 4 == bin.getAllowedBytes(); assert bin.read(new byte[6])!=-1; assert 4 == bin.getProcessedBytes(); assert 0 == bin.getRemainingBytes(); assert 4 == bin.getAllowedBytes();