code: private static int fill(char[] buf, Reader r) throws IOException {
      int offset = 0;
      int read = 0;
      do {
        read = r.read(buf, offset, buf.length - offset);
        if (read > 0) offset += read;
      } while (offset < buf.length && read >= 0);
      return offset;
    }
docs: Repeatedly invokes the reader until the buffer is filled or EOF is reached. Even BufferedReader
  doesn't guarantee that a given call to read() will get all the requested input, and avoid
  readLine() in case of malformed input.
  @param buf The character buffer to fill
  @param r The reader to read from
  @return The number of characters actually written to the buffer.