StreamsUtil { public static long copy(InputStream from, OutputStream to) throws IOException { assert from != null; assert to != null; byte[] buf = new byte[BUF_SIZE]; long total = 0; while (true) { int r = from.read(buf); if (r == -1) { break; } to.write(buf, 0, r); total += r; } return total; }  static long copy(InputStream from, OutputStream to);  }
@Test public void copyStream() throws IOException { String text = randomAsciiLettersOfLengthBetween(10, 1000); ByteArrayInputStream bis = new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8)); ByteArrayOutputStream bos = new ByteArrayOutputStream(); StreamsUtil.copy(bis, bos); String copiedText = bos.toString(StandardCharsets.UTF_8.name()); assertThat(copiedText, is(text)); }