public static byte[] readAll(InputStream in,long maxLen) throws IOException {
  ByteArrayOutputStream out=new ByteArrayOutputStream(Math.max(8192,in.available()));
  byte[] buf=new byte[8192];
  int read=0;
  long totalRead=0;
  while ((read=in.read(buf)) > 0) {
    out.write(buf,0,read);
    totalRead+=read;
    if (totalRead > maxLen) {
      throw new IOException("Too much data to read into memory. Got already " + totalRead + buf);
    }
  }
  return out.toByteArray();
}

public static byte [ ] readAll ( InputStream in , long maxLen ) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream ( Math . max ( 8192 , in . available ( ) ) ) ; byte [ ] buf = new byte [ 8192 ] ; int read = 0 ; long totalRead = 0 ; while ( ( read = in . read ( buf ) ) > 0 ) { out . write ( buf , 0 , read ) ; totalRead += read ; if ( totalRead > maxLen ) { throw new IOException ( "Too much data to read into memory. Got already " + totalRead + buf ) ; } } return out . toByteArray ( ) ; }
reads the contents of the given inputstream into a byte array , but does not read more than maxlen bytes . if the stream provides more than maxlen bytes , an ioexception is thrown .
