code: public static int readFully(InputStream in, byte[] buf,
                                  int offset, int length)
      throws IOException
      {
          int readBytes = 0;
          while (readBytes < length)
          {
              int amount = in.read(buf, offset, length - readBytes);
              if (amount < 0)
              {
                  // EOF
                  return readBytes;
              }
              readBytes += amount;
              offset += amount;
          }
          return readBytes;
      }
docs: Calls {@link InputStream#read(byte[], int, int)} until the requested
  length is read or EOF is encountered.
  This method will block until the request is satisfied.
  
  @param in        The stream to read from.
  @param buf       The buffer to read to.
  @param offset    The offset of the buffer to read from.
  @param length    The length of the data to read.
  
  @return the number of bytes read from the stream.  May be less than
  {@code length} if EOF is encountered before reading that far.
  
  @see #readFully(InputStream, byte[])