StreamFilter extends FilterInputStream { @Override public int read() throws IOException { while (true) { final int read = super.read(); if (read == -1) { return -1; } else if (!filtered((byte) read)) { return read; } } } 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 shouldFilterOnReadInteger() throws Exception { final InputStream stream = new ClassPathResource("account-sequence.json").getInputStream(); try (final StreamFilter unit = new StreamFilter(stream)) { final int filtered = 3; final int available = stream.available(); int read = 0; while (unit.read() != -1) { read++; } assertThat(read, is(available - filtered)); } }
@Test void shouldFilterOnReadBuffer() throws Exception { final InputStream stream = new ClassPathResource("account-sequence.json").getInputStream(); try (final StreamFilter unit = new StreamFilter(stream)) { final int filtered = 3; final int available = stream.available(); int sum = 0; int read; final byte[] buffer = new byte[10]; while ((read = unit.read(buffer, 0, buffer.length)) != -1) { sum += read; } assertThat(sum, is(available - filtered)); } }