private static void read(final InputStream istream,final byte[] bytes) throws IOException {
  int pos=0;
  while (pos < bytes.length) {
    int read=istream.read(bytes,pos,bytes.length - pos);
    if (read < 0) {
      throw new RuntimeException("premature EOF");
    }
    pos+=read;
  }
}

private static void read ( final InputStream istream , final byte [ ] bytes ) throws IOException { int pos = 0 ; while ( pos < bytes . length ) { int read = istream . read ( bytes , pos , bytes . length - pos ) ; if ( read < 0 ) { throw new RuntimeException ( "premature EOF" ) ; } pos += read ; } }
helper method that will read the inputstream repeatedly until the given array is filled .
