public static byte[] toByteArray(InputStream in,int expectedSize) throws IOException {
  byte[] bytes=new byte[expectedSize];
  int remaining=expectedSize;
  while (remaining > 0) {
    int off=expectedSize - remaining;
    int read=in.read(bytes,off,remaining);
    if (read == -1) {
      return Arrays.copyOf(bytes,off);
    }
    remaining-=read;
  }
  int b=in.read();
  if (b == -1) {
    return bytes;
  }
  FastByteArrayOutputStream out=new FastByteArrayOutputStream();
  out.write(b);
  copy(in,out);
  byte[] result=new byte[bytes.length + out.size()];
  System.arraycopy(bytes,0,result,0,bytes.length);
  out.writeTo(result,bytes.length);
  return result;
}

public static byte [ ] toByteArray ( InputStream in , int expectedSize ) throws IOException { byte [ ] bytes = new byte [ expectedSize ] ; int remaining = expectedSize ; while ( remaining > 0 ) { int off = expectedSize - remaining ; int read = in . read ( bytes , off , remaining ) ; if ( read == - 1 ) { return Arrays . copyOf ( bytes , off ) ; } remaining -= read ; } int b = in . read ( ) ; if ( b == - 1 ) { return bytes ; } FastByteArrayOutputStream out = new FastByteArrayOutputStream ( ) ; out . write ( b ) ; copy ( in , out ) ; byte [ ] result = new byte [ bytes . length + out . size ( ) ] ; System . arraycopy ( bytes , 0 , result , 0 , bytes . length ) ; out . writeTo ( result , bytes . length ) ; return result ; }
reads all bytes from an input stream into a byte array . the given expected size is used to create an initial byte array , but if the actual number of bytes read from the stream differs , the correct result will be returned anyway .
