@Override public synchronized int available() throws IOException { return Math.min(in.available(), (int)getRemainingBytes()); }
@Override public synchronized int read(byte b[], int off, int len) throws IOException { int canRead = (int)Math.min(getRemainingBytes(), len); if(canRead==0) return handleStreamOutOfBound(); int nbRead = in.read(b, off, canRead); if(nbRead>0) totalRead += nbRead; return nbRead; }
@Override public synchronized int read() throws IOException { if(getRemainingBytes()==0) return handleStreamOutOfBound(); int i = in.read(); totalRead++; return i; }
@Override public synchronized long skip(long n) throws IOException { int canSkip = (int)Math.min(getRemainingBytes(), n); if(canSkip==0) return handleStreamOutOfBound(); long nbSkipped = in.skip(canSkip); if(nbSkipped>0) totalRead += nbSkipped; return nbSkipped; }
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 Long.MAX_VALUE == bin.getRemainingBytes(); assert -1 == bin.getAllowedBytes(); assert Long.MAX_VALUE == bin.getRemainingBytes(); assert -1 == bin.getAllowedBytes(); 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 0 == bin.getRemainingBytes(); assert 4 == bin.getAllowedBytes(); assert 0 == bin.getRemainingBytes(); assert 4 == bin.getAllowedBytes();