### Question:
StreamUtil { public static byte[] streamToByteArray(final InputStream in) throws IOException { final ByteArrayOutputStream result = new ByteArrayOutputStream(); copy(in, result); return result.toByteArray(); }  static String toString(final InputStream in, final String encoding); static byte[] streamToByteArray(final InputStream in); static InputStream copyStream(final InputStream in); static void copy(final InputStream input, final OutputStream output);  }### Answer:
@Test public void shouldCopyStreamsToByteArrays() throws IOException { final byte[] expected = createByteArray(); final ByteArrayInputStream bis = new ByteArrayInputStream(expected); final byte[] actual = StreamUtil.streamToByteArray(bis); assertThat(expected).isEqualTo(actual); }
@Test public void shouldCopyStreamsLargerThanBufferSize() throws IOException { final byte[] expected = new byte[(17 * 1024)]; Arrays.fill(expected, (byte) 2); final ByteArrayInputStream bis = new ByteArrayInputStream(expected); final byte[] actual = StreamUtil.streamToByteArray(bis); assertThat(expected).isEqualTo(actual); }