ZeroPaddedReadableByteChannel implements ReadableByteChannel { @Override public int read(ByteBuffer dst) throws IOException { if (this.remaining == -1) { int read = this.channel.read(dst); if (read == -1) { this.remaining = this.zeros; return readPadded(dst); } else { return read; } } else { return readPadded(dst); } } ZeroPaddedReadableByteChannel(ReadableByteChannel channel, int zeros); @Override boolean isOpen(); @Override void close(); @Override int read(ByteBuffer dst);  }
@Test public void testReadUnderlying() throws IOException { final byte[] b = new byte[] {1, 1, 1, 1, 1}; final ReadableByteChannel channel = create(b, 2); final ByteBuffer buffer = ByteBuffer.allocate(5); assertEquals(buffer.capacity(), channel.read(buffer)); assertArrayEquals(b, buffer.array()); }
@Test public void testReadAllZeros() throws IOException { final byte[] b = new byte[] {1, 1, 1, 1, 1}; final int zeros = 2; final ReadableByteChannel channel = create(b, zeros); final ByteBuffer buffer = ByteBuffer.allocate(7); assertEquals(b.length, channel.read(buffer)); assertEquals(zeros, channel.read(buffer)); assertArrayEquals(copyOf(b, buffer.capacity()), buffer.array()); buffer.flip(); assertEquals(-1, channel.read(buffer)); }
@Test public void testReadSomeZeros() throws IOException { final byte[] b = new byte[] {1, 1, 1, 1, 1}; final int zeros = 2; final ReadableByteChannel channel = create(b, zeros); final ByteBuffer buffer = ByteBuffer.allocate(7); buffer.limit(buffer.capacity() - 1); assertEquals(b.length, channel.read(buffer)); assertEquals(zeros - 1, channel.read(buffer)); assertArrayEquals(copyOf(b, buffer.capacity()), buffer.array()); buffer.limit(buffer.capacity()); assertEquals(1, channel.read(buffer)); assertArrayEquals(copyOf(b, buffer.capacity()), buffer.array()); buffer.flip(); assertEquals(-1, channel.read(buffer)); }
@Test public void testZeroZeros() throws IOException { final byte[] b = new byte[] {1, 1, 1, 1, 1}; final ReadableByteChannel channel = create(b, 0); final ByteBuffer buffer = ByteBuffer.allocate(5); assertEquals(b.length, channel.read(buffer)); assertArrayEquals(copyOf(b, buffer.capacity()), buffer.array()); buffer.flip(); assertEquals(-1, channel.read(buffer)); }