LimitedInputStream extends InputStream { @Override public int read() throws IOException { if (isStreamRead) { return -1; } if (isLimitViolated) { throw new ReadingByteLimitException(); } int nextByte = inputStream.read(); readingByteLimit--; checkReadingByteLimit(nextByte); return nextByte; } LimitedInputStream(InputStream inputStream, long readingByteLimit); @Override int read(); @Override int read(byte[] b); @Override int read(byte[] b, int off, int len); @Override void close(); @Override long skip(long n); @Override int available(); @Override synchronized void mark(int readlimit); @Override synchronized void reset(); @Override boolean markSupported();  }
@Test public void readingByteAfterFileReadingTest() throws IOException { UriResolver uriResolver = new UriResolver(baseUri); URL url = uriResolver.resolveAgainstBaseUri("retrieveStyleSheetTest.css.dat"); InputStream stream = new LimitedInputStream(url.openStream(), 100); for (int i = 0; i < 101; i++) { stream.read(); } }
@Test public void readingByteArrayAfterFileReadingTest() throws IOException { UriResolver uriResolver = new UriResolver(baseUri); URL url = uriResolver.resolveAgainstBaseUri("retrieveStyleSheetTest.css.dat"); InputStream stream = new LimitedInputStream(url.openStream(), 100); stream.read(new byte[100]); stream.read(new byte[1]); }
@Test public void readingByteArrayWithOffsetAfterFileReadingTest() throws IOException { UriResolver uriResolver = new UriResolver(baseUri); URL url = uriResolver.resolveAgainstBaseUri("retrieveStyleSheetTest.css.dat"); InputStream stream = new LimitedInputStream(url.openStream(), 100); stream.read(new byte[100], 0, 100); stream.read(new byte[1], 0, 1); }
@Test public void readingByteWithLimitOfOneLessThenFileSizeTest() throws IOException { UriResolver uriResolver = new UriResolver(baseUri); URL url = uriResolver.resolveAgainstBaseUri("retrieveStyleSheetTest.css.dat"); InputStream stream = new LimitedInputStream(url.openStream(), 88); for (int i = 0; i < 88; i++) { Assert.assertNotEquals(-1, stream.read()); } junitExpectedException.expect(ReadingByteLimitException.class); stream.read(); }
@Test public void readingByteArrayWithLimitOfOneLessThenFileSizeTest() throws IOException { UriResolver uriResolver = new UriResolver(baseUri); URL url = uriResolver.resolveAgainstBaseUri("retrieveStyleSheetTest.css.dat"); InputStream stream = new LimitedInputStream(url.openStream(), 88); byte[] bytes = new byte[100]; int numOfReadBytes = stream.read(bytes); Assert.assertEquals(88, numOfReadBytes); Assert.assertEquals(10, bytes[87]); Assert.assertEquals(0, bytes[88]); junitExpectedException.expect(ReadingByteLimitException.class); stream.read(new byte[1]); }
@Test public void readingByteArrayWithOffsetAndLimitOfOneLessThenFileSizeTest() throws IOException { UriResolver uriResolver = new UriResolver(baseUri); URL url = uriResolver.resolveAgainstBaseUri("retrieveStyleSheetTest.css.dat"); InputStream stream = new LimitedInputStream(url.openStream(), 88); byte[] bytes = new byte[100]; int numOfReadBytes = stream.read(bytes, 0, 88); Assert.assertEquals(88, numOfReadBytes); Assert.assertEquals(10, bytes[87]); Assert.assertEquals(0, bytes[88]); junitExpectedException.expect(ReadingByteLimitException.class); stream.read(bytes, 88, 1); }
@Test public void readingByteArrayWithBigBufferTest() throws IOException { UriResolver uriResolver = new UriResolver(baseUri); URL url = uriResolver.resolveAgainstBaseUri("retrieveStyleSheetTest.css.dat"); InputStream stream = new LimitedInputStream(url.openStream(), 89); byte[] bytes = new byte[100]; Assert.assertEquals(89, stream.read(bytes)); byte[] tempBytes = (byte[]) bytes.clone(); Assert.assertEquals(-1, stream.read(bytes)); Assert.assertArrayEquals(tempBytes, bytes); }
@Test public void readingByteArrayWithOffsetAndBigBufferTest() throws IOException { UriResolver uriResolver = new UriResolver(baseUri); URL url = uriResolver.resolveAgainstBaseUri("retrieveStyleSheetTest.css.dat"); InputStream stream = new LimitedInputStream(url.openStream(), 89); byte[] bytes = new byte[100]; Assert.assertEquals(89, stream.read(bytes, 0, 100)); byte[] tempBytes = (byte[]) bytes.clone(); Assert.assertEquals(-1, stream.read(bytes, 0, 100)); Assert.assertArrayEquals(tempBytes, bytes); }
@Test public void byteArrayOverwritingTest() throws IOException { UriResolver uriResolver = new UriResolver(baseUri); URL url = uriResolver.resolveAgainstBaseUri("retrieveStyleSheetTest.css.dat"); InputStream stream = new LimitedInputStream(url.openStream(), 90); byte[] bytes = new byte[100]; bytes[89] = 13; Assert.assertEquals(89, stream.read(bytes)); Assert.assertEquals(13, bytes[89]); }
@Test public void readingByteWithZeroLimitTest() throws IOException { LimitedInputStream stream = new LimitedInputStream(new ByteArrayInputStream(new byte[1]), 0); junitExpectedException.expect(ReadingByteLimitException.class); stream.read(); }
@Test public void readingByteArrayWithZeroLimitTest() throws IOException { LimitedInputStream stream = new LimitedInputStream(new ByteArrayInputStream(new byte[1]), 0); byte[] bytes = new byte[100]; junitExpectedException.expect(ReadingByteLimitException.class); stream.read(bytes); }
@Test public void readingByteArrayWithOffsetAndZeroLimitTest() throws IOException { LimitedInputStream stream = new LimitedInputStream(new ByteArrayInputStream(new byte[1]), 0); byte[] bytes = new byte[100]; junitExpectedException.expect(ReadingByteLimitException.class); stream.read(bytes, 0, 100); }
@Test public void readingEmptyByteWithZeroLimitTest() throws IOException { LimitedInputStream stream = new LimitedInputStream(new ByteArrayInputStream(new byte[0]), 0); Assert.assertEquals(-1, stream.read()); }
@Test public void readingEmptyByteArrayWithZeroLimitTest() throws IOException { LimitedInputStream stream = new LimitedInputStream(new ByteArrayInputStream(new byte[0]), 0); byte[] bytes = new byte[100]; Assert.assertEquals(-1, stream.read(bytes)); }
@Test public void readingEmptyByteArrayWithOffsetAndZeroLimitTest() throws IOException { LimitedInputStream stream = new LimitedInputStream(new ByteArrayInputStream(new byte[0]), 0); byte[] bytes = new byte[100]; Assert.assertEquals(-1, stream.read(bytes, 0, 100)); }