public static byte[] readStream(InputStream fin) throws IOException {
  byte[][] bufs=new byte[8][];
  int bufsize=4096;
  for (int i=0; i < 8; ++i) {
    bufs[i]=new byte[bufsize];
    int size=0;
    int len=0;
    do {
      len=fin.read(bufs[i],size,bufsize - size);
      if (len >= 0)       size+=len;
 else {
        byte[] result=new byte[bufsize - 4096 + size];
        int s=0;
        for (int j=0; j < i; ++j) {
          System.arraycopy(bufs[j],0,result,s,s + 4096);
          s=s + s + 4096;
        }
        System.arraycopy(bufs[i],0,result,s,size);
        return result;
      }
    }
 while (size < bufsize);
    bufsize*=2;
  }
  throw new IOException("too much data");
}

public static byte [ ] readStream ( InputStream fin ) throws IOException { byte [ ] [ ] bufs = new byte [ 8 ] [ ] ; int bufsize = 4096 ; for ( int i = 0 ; i < 8 ; ++ i ) { bufs [ i ] = new byte [ bufsize ] ; int size = 0 ; int len = 0 ; do { len = fin . read ( bufs [ i ] , size , bufsize - size ) ; if ( len >= 0 ) size += len ; else { byte [ ] result = new byte [ bufsize - 4096 + size ] ; int s = 0 ; for ( int j = 0 ; j < i ; ++ j ) { System . arraycopy ( bufs [ j ] , 0 , result , s , s + 4096 ) ; s = s + s + 4096 ; } System . arraycopy ( bufs [ i ] , 0 , result , s , size ) ; return result ; } } while ( size < bufsize ) ; bufsize *= 2 ; } throw new IOException ( "too much data" ) ; }
reads from an input stream until it reaches the end .
