docs: 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;
      }