IoUtils { public static int redirect(final InputStream from, final OutputStream to) throws IOException { assertNotNull(to); try { return stream(from, new StreamConsumer() { @Override public void apply(byte[] bytes, int offset, int length) throws Exception { to.write(bytes, offset, length); } }); } catch (final Exception e) { throw (IOException) e; } }  static void tryFlush(final Object... flushable); static int stream(final InputStream in, final StreamConsumer consumer); static int redirect(final InputStream from, final OutputStream to); static int redirect(final Reader from, final Writer to); static byte[] from(
      final InputStream in); static String from(
      final Reader reader); static byte[] getChecksum(final InputStream in); static String getChecksumAsString(final InputStream in);  }
@Test public void testRedirectStream() throws IOException { final Object[][] testParameters = new Object[][] { {new ByteArrayInputStream(new byte[] {1, 2, 3}), new ByteArrayOutputStream(), 3}, {new ByteArrayInputStream(new byte[] {4, 5, 6, 7, 8}), new ByteArrayOutputStream(), 5}, {new ByteArrayInputStream(new byte[] {9, 10, 11, 12, 13, 14, 15}), new ByteArrayOutputStream(), 7}}; for (final Object[] testParameter : testParameters) { int expected = (Integer) testParameter[2]; InputStream from = (InputStream) testParameter[0]; OutputStream to = (OutputStream) testParameter[1]; assertEquals(expected, IoUtils.redirect(from, to)); } }
@Test public void testRedirectReaderWriter() throws IOException { final Object[][] testParameters = new Object[][] { {new ByteArrayInputStream(new byte[] {1, 2, 3}), new ByteArrayOutputStream(), 3}, {new ByteArrayInputStream(new byte[] {4, 5, 6, 7, 8}), new ByteArrayOutputStream(), 5}, {new ByteArrayInputStream(new byte[] {9, 10, 11, 12, 13, 14, 15}), new ByteArrayOutputStream(), 7}}; for (final Object[] testParameter : testParameters) { int expected = (Integer) testParameter[2]; InputStream inputStream = (InputStream) testParameter[0]; OutputStream outputStream = (OutputStream) testParameter[1]; Reader from = new BufferedReader(new InputStreamReader(inputStream)); Writer to = new BufferedWriter(new OutputStreamWriter(outputStream)); assertEquals(expected, IoUtils.redirect(from, to)); } }