### Question:
LimitedInputStream extends InputStream { @Override public int read() throws IOException { if (bytesRead >= limit) { return -1; } final int val = in.read(); if (val > -1) { bytesRead++; } return val; } LimitedInputStream(final InputStream in, final long limit); @Override int read(); @Override int read(final byte[] b); @Override int read(byte[] b, int off, int len); @Override long skip(final long n); @Override int available(); @Override void close(); @Override void mark(int readlimit); @Override boolean markSupported(); @Override void reset();  }### Answer:
@Test public void testSingleByteRead() throws IOException { final LimitedInputStream lis = new LimitedInputStream(bais, 4); assertEquals(0, lis.read()); assertEquals(1, lis.read()); assertEquals(2, lis.read()); assertEquals(3, lis.read()); assertEquals(-1, lis.read()); }
@Test public void testByteArrayRead() throws IOException { final LimitedInputStream lis = new LimitedInputStream(bais, 4); final int len = lis.read(buffer10); assertEquals(4, len); assertEquals(-1, lis.read(buffer10)); for (int i = 0; i < 4; i++) { assertEquals(i, buffer10[i]); } }
@Test public void testByteArrayReadWithRange() throws IOException { final LimitedInputStream lis = new LimitedInputStream(bais, 4); final int len = lis.read(buffer10, 4, 12); assertEquals(4, len); assertEquals(-1, lis.read(buffer10, 8, 2)); for (int i = 0; i < 4; i++) { assertEquals(i, buffer10[i + 4]); } }