// non-javadoc, see interface HeaderValueFormatter
  public CharArrayBuffer formatHeaderElement(
      CharArrayBuffer buffer, final HeaderElement elem, final boolean quote) {
    if (elem == null) {
      throw new IllegalArgumentException("Header element must not be null.");
    }

    int len = estimateHeaderElementLen(elem);
    if (buffer == null) {
      buffer = new CharArrayBuffer(len);
    } else {
      buffer.ensureCapacity(len);
    }

    buffer.append(elem.getName());
    final String value = elem.getValue();
    if (value != null) {
      buffer.append('=');
      doFormatValue(buffer, value, quote);
    }

    final int parcnt = elem.getParameterCount();
    if (parcnt > 0) {
      for (int i = 0; i < parcnt; i++) {
        buffer.append("; ");
        formatNameValuePair(buffer, elem.getParameter(i), quote);
      }
    }

    return buffer;
  }
  // non-javadoc, see interface HeaderValueFormatter
  public CharArrayBuffer formatElements(
      CharArrayBuffer buffer, final HeaderElement[] elems, final boolean quote) {
    if (elems == null) {
      throw new IllegalArgumentException("Header element array must not be null.");
    }

    int len = estimateElementsLen(elems);
    if (buffer == null) {
      buffer = new CharArrayBuffer(len);
    } else {
      buffer.ensureCapacity(len);
    }

    for (int i = 0; i < elems.length; i++) {
      if (i > 0) {
        buffer.append(", ");
      }
      formatHeaderElement(buffer, elems[i], quote);
    }

    return buffer;
  }
  // non-javadoc, see interface HeaderValueFormatter
  public CharArrayBuffer formatNameValuePair(
      CharArrayBuffer buffer, final NameValuePair nvp, final boolean quote) {
    if (nvp == null) {
      throw new IllegalArgumentException("NameValuePair must not be null.");
    }

    int len = estimateNameValuePairLen(nvp);
    if (buffer == null) {
      buffer = new CharArrayBuffer(len);
    } else {
      buffer.ensureCapacity(len);
    }

    buffer.append(nvp.getName());
    final String value = nvp.getValue();
    if (value != null) {
      buffer.append('=');
      doFormatValue(buffer, value, quote);
    }

    return buffer;
  }
  // non-javadoc, see interface HeaderValueFormatter
  public CharArrayBuffer formatParameters(
      CharArrayBuffer buffer, NameValuePair[] nvps, boolean quote) {
    if (nvps == null) {
      throw new IllegalArgumentException("Parameters must not be null.");
    }

    int len = estimateParametersLen(nvps);
    if (buffer == null) {
      buffer = new CharArrayBuffer(len);
    } else {
      buffer.ensureCapacity(len);
    }

    for (int i = 0; i < nvps.length; i++) {
      if (i > 0) {
        buffer.append("; ");
      }
      formatNameValuePair(buffer, nvps[i], quote);
    }

    return buffer;
  }