public static byte[] toByteArray(InputStream inputStream,final int size) throws IOException {
  ByteArrayOutputStream out=new ByteArrayOutputStream();
  long length=size == -1 ? Long.MAX_VALUE : size;
  pipe(inputStream,out,length);
  return out.toByteArray();
}

public static byte [ ] toByteArray ( InputStream inputStream , final int size ) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream ( ) ; long length = size == - 1 ? Long . MAX_VALUE : size ; pipe ( inputStream , out , length ) ; return out . toByteArray ( ) ; }
converts ( a portion ) of the given stream to a byte array .
