/// <summary>Estimates the length of a formatted header element.</summary> /// <remarks>Estimates the length of a formatted header element.</remarks> /// <param name="elem">the header element to format, or <code>null</code></param> /// <returns>a length estimate, in number of characters</returns> protected internal virtual int EstimateHeaderElementLen(HeaderElement elem) { if (elem == null) { return(0); } int result = elem.GetName().Length; // name string value = elem.GetValue(); if (value != null) { // assume quotes, but no escaped characters result += 3 + value.Length; } // ="value" int parcnt = elem.GetParameterCount(); if (parcnt > 0) { for (int i = 0; i < parcnt; i++) { result += 2 + EstimateNameValuePairLen(elem.GetParameter(i)); } } // ; <param> return(result); }
// non-javadoc, see interface HeaderValueFormatter public virtual CharArrayBuffer FormatHeaderElement(CharArrayBuffer charBuffer, HeaderElement elem, bool quote) { Args.NotNull(elem, "Header element"); int len = EstimateHeaderElementLen(elem); CharArrayBuffer buffer = charBuffer; if (buffer == null) { buffer = new CharArrayBuffer(len); } else { buffer.EnsureCapacity(len); } buffer.Append(elem.GetName()); string value = elem.GetValue(); if (value != null) { buffer.Append('='); DoFormatValue(buffer, value, quote); } int parcnt = elem.GetParameterCount(); if (parcnt > 0) { for (int i = 0; i < parcnt; i++) { buffer.Append("; "); FormatNameValuePair(buffer, elem.GetParameter(i), quote); } } return(buffer); }