@Test(expected = EOFException.class) public void readTooMuch() throws Exception { byte output[] = new byte[data.length + 1024]; try (FileChannel fd = FileChannel.open(file, StandardOpenOption.READ)) { try (FileReader reader = new SizeVerifyingReader(new FileChannelReader(fd, 0), data.length)) { int i = 0; while (i < output.length) i += reader.read(ByteBuffer.wrap(output, i, Integer.min(output.length - i, 128))); } } }
@Override public int read(ByteBuffer data) throws IOException { if (read == expected) throw new EOFException("no more data"); if (data.remaining() > expected - read) data.limit(data.position() + (int) (expected - read)); assert (data.remaining() <= expected - read); final int rlen = in.read(data); read += rlen; return rlen; }
SizeVerifyingReader implements FileReader { @Override public int read(ByteBuffer data) throws IOException { if (read == expected) throw new EOFException("no more data"); if (data.remaining() > expected - read) data.limit(data.position() + (int) (expected - read)); assert (data.remaining() <= expected - read); final int rlen = in.read(data); read += rlen; return rlen; } }
SizeVerifyingReader implements FileReader { @Override public int read(ByteBuffer data) throws IOException { if (read == expected) throw new EOFException("no more data"); if (data.remaining() > expected - read) data.limit(data.position() + (int) (expected - read)); assert (data.remaining() <= expected - read); final int rlen = in.read(data); read += rlen; return rlen; }  }
SizeVerifyingReader implements FileReader { @Override public int read(ByteBuffer data) throws IOException { if (read == expected) throw new EOFException("no more data"); if (data.remaining() > expected - read) data.limit(data.position() + (int) (expected - read)); assert (data.remaining() <= expected - read); final int rlen = in.read(data); read += rlen; return rlen; }  @Override int read(ByteBuffer data); @Override void close(); @Override ByteBuffer allocateByteBuffer(int size); }
SizeVerifyingReader implements FileReader { @Override public int read(ByteBuffer data) throws IOException { if (read == expected) throw new EOFException("no more data"); if (data.remaining() > expected - read) data.limit(data.position() + (int) (expected - read)); assert (data.remaining() <= expected - read); final int rlen = in.read(data); read += rlen; return rlen; }  @Override int read(ByteBuffer data); @Override void close(); @Override ByteBuffer allocateByteBuffer(int size);  }