Java tutorial
//package com.java2s; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; public class Main { public static byte[] toByteArray(InputStream is) throws IOException { final ByteArrayOutputStream baos = toByteArrayOutputStream(is); return (baos != null) ? baos.toByteArray() : null; } public static ByteArrayOutputStream toByteArrayOutputStream(InputStream is) throws IOException { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final byte[] buffer = new byte[512]; int length = 0; while ((length = is.read(buffer)) != -1) { baos.write(buffer, 0, length); } return baos; } }