code: protected static int readFully(InputStream stm, 
        byte[] buf, int offset, int len) throws IOException {
      int n = 0;
      for (;;) {
        int nread = stm.read(buf, offset + n, len - n);
        if (nread <= 0) 
          return (n == 0) ? nread : n;
        n += nread;
        if (n >= len)
          return n;
      }
    }
docs: A utility function that tries to read up to <code>len</code> bytes from
  <code>stm</code>
  
  @param stm    an input stream
  @param buf    destiniation buffer
  @param offset offset at which to store data
  @param len    number of bytes to read
  @return actual number of bytes read
  @throws IOException if there is any IO error