ObservableRedableByteChannel implements ReadableByteChannel { @Override public int read(ByteBuffer dst) throws IOException { final int i = this.channel.read(dst); if (i >= 0) { this.listener.transferred(i); } return i; } ObservableRedableByteChannel(ReadableByteChannel channel, TransferListener listener); @Override boolean isOpen(); @Override int read(ByteBuffer dst); @Override void close();  }
@Test public void testRead() throws IOException { EventLogHttpTransferListener listener = new EventLogHttpTransferListener(); ReadableByteChannel channel = Channels.newChannel(new ByteArrayInputStream(new byte[3])); @SuppressWarnings("resource") ReadableByteChannel in = new ObservableRedableByteChannel(channel, listener); ByteBuffer dst = ByteBuffer.allocate(4); dst.limit(1); assertEquals(1, in.read(dst)); dst.limit(4); assertEquals(2, in.read(dst)); assertEquals(-1, in.read(dst)); assertEquals(of(new Transferred(1), new Transferred(2)), listener.getEventLog()); }