CopyInputStream extends FilterInputStream { @Override public int read() throws IOException { int ret = super.read(); if (ret > -1) { for (int i = 0; i < copy.length; i++) { if (copy[i] == null) continue; try { copy[i].write(ret); } catch (IOException ioe) { copy[i] = null; } } } return ret; } CopyInputStream(InputStream in, OutputStream copy);  CopyInputStream(InputStream in, OutputStream[] copy); @Override boolean markSupported(); @Override int read(); @Override int read(byte[] b, int off, int len); String readLine(); @Override void close();  }
@Test public void testRead() { try { CopyInputStream cis = new CopyInputStream(in, copies); while (cis.read() > -1) ; assertEquals(sample, new String(copies[0].toByteArray())); assertEquals(new String(copies[0].toByteArray()), new String( copies[1].toByteArray())); cis.close(); } catch (IOException ioe) { fail("IOException not expected!" + ioe); ioe.printStackTrace(); } }