StreamFilter extends FilterInputStream { @Override public long skip(final long n) throws IOException { long sum = 0; final byte[] b = new byte[size]; while (sum < n) { final int left = (int) (n - sum); final int len = left > size ? size : left; final int read = read(b, 0, len); if (read == -1) { return sum; } sum += read; } return sum; } protected  StreamFilter(final InputStream in); protected  StreamFilter(final InputStream in, final int size); @Override int read(); @Override int read(final byte[] b, final int off, final int len); @Override long skip(final long n);  }
@Test void shouldFilterOnSkip() throws Exception { final InputStream stream = new ClassPathResource("account-sequence.json").getInputStream(); try (final StreamFilter unit = new StreamFilter(stream, 5)) { final long filtered = 3; final long available = stream.available(); long sum = 0; long skipped; while ((skipped = unit.skip(10)) != 0) { sum += skipped; } assertThat(sum, is(available - filtered)); } }