MonitoredInputStream extends FilterInputStream { @Override public int read() throws IOException { int value = super.read(); if (value > -1) { bytesRead++; } return value; } MonitoredInputStream(File file);  MonitoredInputStream(InputStream is); @Override int read(); @Override int read(byte[] b); @Override int read(byte[] b, int off, int len); int updateBytesRead(int byteCount); long getBytesRead();  }
@Test public void testRead() throws Exception { stream.read(); assertEquals(1, stream.bytesRead); for (int value = 0; value >= 0; ) { value = stream.read(); } assertEquals(file.length(), stream.bytesRead); }
@Test public void testReadToArray() throws Exception { stream.read(new byte[5]); assertEquals(5, stream.bytesRead); for (int bytesRead = 0; bytesRead >= 0; ) { bytesRead = stream.read(new byte[5]); } assertEquals(file.length(), stream.bytesRead); }
@Test public void testReadToArrayLen() throws Exception { stream.read(new byte[8], 0, 8); assertEquals(8, stream.bytesRead); for (int bytesRead = 0; bytesRead >= 0; ) { bytesRead = stream.read(new byte[8]); } assertEquals(file.length(), stream.bytesRead); }