Here you can find the source of readBytes(InputStream inputStream)
public static byte[] readBytes(InputStream inputStream) throws IOException
//package com.java2s; //License from project: Apache License import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.List; public class Main { public static byte[] readBytes(InputStream inputStream) throws IOException { // this dynamically extends to take the bytes you read ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream(); // this is storage overwritten on each iteration with bytes int bufferSize = 1024; byte[] buffer = new byte[bufferSize]; // we need to know how may bytes were read to write them to the byteBuffer int len = 0; while ((len = inputStream.read(buffer)) != -1) { byteBuffer.write(buffer, 0, len); }/*ww w . jav a 2 s. co m*/ // and then we can return your byte array. return byteBuffer.toByteArray(); } public static byte[] ToByteArray(List<Byte> list) { byte[] array = new byte[list.size()]; for (int i = 0; i < list.size(); i++) { array[i] = (byte) list.get(i); } return array; } }