private void fill() throws IOException
  {
    if (eof)
      return;

    // as long as we are not just starting up
    if (count > 0)
    {
      // if the caller left the requisite amount spare in the buffer
      if (count - pos == 2) {
        // copy it back to the start of the buffer
        System.arraycopy(buf, pos, buf, 0, count - pos);
        count -= pos;
        pos = 0;
      } else {
        // should never happen, but just in case
        throw new IllegalStateException("fill() detected illegal buffer state");
      }
    }

    // Try and fill the entire buffer, starting at count, line by line
    // but never read so close to the end that we might split a boundary
    // Thanks to Tony Chu, tony.chu@brio.com, for the -2 suggestion.
    int read = 0;
    int boundaryLength = boundary.length();
    int maxRead = buf.length - boundaryLength - 2;  // -2 is for /r/n
    while (count < maxRead) {
      // read a line
      read = ((MultipartInputStream)in).readLine(buf, count, buf.length - count);
      // check for eof and boundary
      if (read == -1) {
        throw new IOException("unexpected end of part");
      } else {
        if (read >= boundaryLength) {
          eof = true;
          for (int i=0; i < boundaryLength; i++) {
            if (boundary.charAt(i) != buf[count + i]) {
              // Not the boundary!
              eof = false;
              break;
            }
          }
          if (eof) {
            break;
          }
        }
      }
      // success
      count += read;
    }
  }