Here you can find the source of toByteArray(InputStream is)
public static byte[] toByteArray(InputStream is) throws IOException
//package com.java2s; import java.io.*; public class Main { public static byte[] toByteArray(InputStream is) throws IOException { if (null == is) { return null; }/* w w w . j a v a 2 s. c o m*/ byte[] cache = new byte[2 * 1024]; ByteArrayOutputStream buffer = new ByteArrayOutputStream(); for (int length; (length = is.read(cache)) != -1;) { buffer.write(cache, 0, length); } is.close(); return buffer.toByteArray(); } }