LimitedInputStream extends FilterInputStream { @Override public int read() throws IOException { if (read(ONE_BYTE, 0, 1) == 1) { return ONE_BYTE[0]; } return -1; } LimitedInputStream(InputStream in, long numToRead); @Override int read(); @Override int read(byte[] b); @Override int read(byte[] b, int off, int len);  }
@SuppressWarnings("resource") @Test public void testRead_WithLimit0() throws IOException { LimitedInputStream stream = new LimitedInputStream(new ForeverInputStream(), 0); Assert.assertEquals(-1, stream.read()); }
@SuppressWarnings("resource") @Test public void testRead_WithLimit1() throws IOException { LimitedInputStream stream = new LimitedInputStream(new ForeverInputStream(), 1); Assert.assertEquals(0, stream.read()); Assert.assertEquals(-1, stream.read()); }
@SuppressWarnings("resource") @Test public void testRead_WithLimit100() throws IOException { LimitedInputStream stream = new LimitedInputStream(new ForeverInputStream(), 100); Assert.assertEquals(100, stream.read(new byte[1000])); Assert.assertEquals(-1, stream.read()); }