StreamUtil { public static byte[] toByteArray(InputStream inputStream) throws IOException { if (inputStream == null) { return null; } try { UnsyncByteArrayOutputStream unsyncByteArrayOutputStream = new UnsyncByteArrayOutputStream(inputStream.available()); _transferByteArray( inputStream, unsyncByteArrayOutputStream, BUFFER_SIZE, -1); byte[] unsafeByteArray = unsyncByteArrayOutputStream.unsafeGetByteArray(); if (unsafeByteArray.length == unsyncByteArrayOutputStream.size()) { return unsafeByteArray; } return unsyncByteArrayOutputStream.toByteArray(); } finally { inputStream.close(); } }  static void cleanUp(Closeable... closeables); static byte[] toByteArray(InputStream inputStream); static String toString(InputStream inputStream); static String toString(InputStream inputStream, String charsetName); static void transfer(
			InputStream inputStream, OutputStream outputStream); static void transfer(
			InputStream inputStream, OutputStream outputStream, boolean cleanUp); static void transfer(
			InputStream inputStream, OutputStream outputStream, int bufferSize); static void transfer(
			InputStream inputStream, OutputStream outputStream, int bufferSize,
			boolean cleanUp); static void transfer(
			InputStream inputStream, OutputStream outputStream, int bufferSize,
			boolean cleanUp, long length); static void transfer(
			InputStream inputStream, OutputStream outputStream, long length); static final int BUFFER_SIZE; static final boolean FORCE_TIO; }
@Test public void testToByteArray() throws IOException { Assert.assertNull(StreamUtil.toByteArray(null)); byte[] bytes = new byte[StreamUtil.BUFFER_SIZE + 1]; Random random = new Random(); random.nextBytes(bytes); UnsyncByteArrayInputStream unsyncByteArrayInputStream = new UnsyncByteArrayInputStream(bytes); Assert.assertArrayEquals( bytes, StreamUtil.toByteArray(unsyncByteArrayInputStream)); unsyncByteArrayInputStream = new UnsyncByteArrayInputStream(bytes) { @Override public int available() { return 0; } }; Assert.assertArrayEquals( bytes, StreamUtil.toByteArray(unsyncByteArrayInputStream)); }