ObservableInputStream extends FilterInputStream { @Override public int read() throws IOException { final int i = super.read(); if (i >= 0) { this.observer.transferred(1); } return i; } ObservableInputStream(InputStream in, TransferListener observer); @Override int read(); @Override int read(byte[] b, int off, int len); @Override void close();  }
@Test public void testRead() throws IOException { EventLogHttpTransferListener listener = new EventLogHttpTransferListener(); @SuppressWarnings("resource") InputStream in = new ObservableInputStream(new ByteArrayInputStream(new byte[10]), listener); in.read(); in.read(new byte[2]); assertEquals(of(new Transferred(1), new Transferred(2)), listener.getEventLog()); }
@Test public void testNoRead() throws IOException { EventLogHttpTransferListener listener = new EventLogHttpTransferListener(); @SuppressWarnings("resource") InputStream in = new ObservableInputStream(new ByteArrayInputStream(new byte[0]), listener); in.read(); in.read(new byte[2]); assertTrue(listener.getEventLog().isEmpty()); }