TeeInputStream extends InputStream { @Override public int read() throws IOException { fillBuf(); if (inBuf() > 0) { int r = buf[readPos++]; return r & 0xff; } return -1; } TeeInputStream(InputStream from, OutputStream to); @Override int available(); @Override void close(); @Override int read(); @Override int read(byte[] b); @Override int read(byte[] b, int off, int len);  }
@Test public void testSimpleInput() throws IOException { byte[] input = "very simple input".getBytes(StandardCharsets.UTF_8); ByteArrayInputStream in = new ByteArrayInputStream(input); ByteArrayOutputStream gotten = new ByteArrayOutputStream(); ByteArrayOutputStream output = new ByteArrayOutputStream(); TeeInputStream tee = new TeeInputStream(in, gotten); int b = tee.read(); assertThat(b, is((int)'v')); output.write(b); assertThat(gotten.toString(), is("very simple input")); for (int i = 0; i < 16; i++) { b = tee.read(); assertThat(b, is(greaterThan(0))); output.write(b); } assertThat(tee.read(), is(-1)); assertThat(gotten.toString(), is("very simple input")); assertThat(output.toString(), is("very simple input")); }