### Question:
IoUtils { public static byte[] toByteArray(InputStream in) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); copy(in, out); return out.toByteArray(); }  static byte[] toByteArray(InputStream in); static long copy(InputStream from, OutputStream to);  }### Answer:
@Test public void testRoundTrip() throws Exception { byte[] bytes = { 1, 2, 3, -127, 100, 127}; InputStream is = new ByteArrayInputStream(bytes); byte[] bytes2 = IoUtils.toByteArray(is); assertNotSame(bytes2, bytes); assertEquals(bytes.length, bytes2.length); for (int i = 0; i < bytes.length; ++i) { assertEquals(bytes[i], bytes2[i]); } }
@Test public void testEmpty() throws Exception { InputStream is = new ByteArrayInputStream(new byte[0]); byte[] bytes = IoUtils.toByteArray(is); assertEquals(0, bytes.length); }