Here you can find the source of toByteArray(InputStream is)
public static byte[] toByteArray(InputStream is) throws IOException
//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 { if (null == is) return null; byte[] cache = new byte[2 * 1024]; ByteArrayOutputStream buffer = new ByteArrayOutputStream(); for (int length; (length = is.read(cache)) != -1;) { buffer.write(cache, 0, length); }// w w w . j a v a 2 s .co m is.close(); return buffer.toByteArray(); } }