Here you can find the source of getBytesFromStream(InputStream is)
public static byte[] getBytesFromStream(InputStream is) throws Exception
//package com.java2s; //License from project: Apache License import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class Main { public static byte[] getBytesFromStream(InputStream is) throws Exception { InputStreamReader isr = new InputStreamReader(is); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try {//from w w w. j a v a 2s . c o m int bytesRead = 0; int chunkSize = 10000000; byte[] chunk = new byte[chunkSize]; while ((bytesRead = is.read(chunk)) > 0) { byte[] ba = new byte[bytesRead]; for (int i = 0; i < bytesRead; i++) { ba[i] = chunk[i]; } baos.write(ba, 0, ba.length); } } catch (Exception e) { e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } try { baos.close(); } catch (IOException e) { e.printStackTrace(); } } if (baos != null) return baos.toByteArray(); else throw new Exception("cannot convert stream to bytes"); } }