Java tutorial
//package com.java2s; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; public class Main { public static final int BYTE_ARRAY_SIZE = 2048; public static byte[] readStreamToBytes(InputStream is) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] byteArray = new byte[BYTE_ARRAY_SIZE]; int length = 0; while ((length = is.read(byteArray)) != -1) { bos.write(byteArray, 0, length); } bos.close(); is.close(); return bos.toByteArray(); } }