FileDocCategorySizeDatePackage
BasicLineFormatter.javaAPI DocAndroid 1.5 API11977Wed May 06 22:41:10 BST 2009org.apache.http.message

BasicLineFormatter

public class BasicLineFormatter extends Object implements LineFormatter
Interface for formatting elements of the HEAD section of an HTTP message. This is the complement to {@link LineParser}. There are individual methods for formatting a request line, a status line, or a header line. The formatting does not include the trailing line break sequence CR-LF. The formatted lines are returned in memory, the formatter does not depend on any specific IO mechanism. Instances of this interface are expected to be stateless and thread-safe.
author
Remy Maucherat
author
Mike Bowler
author
Jeff Dever
author
Oleg Kalnichevski
author
and others
version
$Revision: 574185 $
since
4.0

Fields Summary
public static final BasicLineFormatter
DEFAULT
A default instance of this class, for use as default or fallback. Note that {@link BasicLineFormatter} is not a singleton, there can be many instances of the class itself and of derived classes. The instance here provides non-customized, default behavior.
Constructors Summary
Methods Summary
public org.apache.http.util.CharArrayBufferappendProtocolVersion(org.apache.http.util.CharArrayBuffer buffer, org.apache.http.ProtocolVersion version)

        if (version == null) {
            throw new IllegalArgumentException
                ("Protocol version may not be null");
        }

        // can't use initBuffer, that would clear the argument!
        CharArrayBuffer result = buffer;
        final int len = estimateProtocolVersionLen(version);
        if (result == null) {
            result = new CharArrayBuffer(len);
        } else {
            result.ensureCapacity(len);
        }

        result.append(version.getProtocol());
        result.append('/"); 
        result.append(Integer.toString(version.getMajor())); 
        result.append('."); 
        result.append(Integer.toString(version.getMinor())); 

        return result;
    
protected voiddoFormatHeader(org.apache.http.util.CharArrayBuffer buffer, org.apache.http.Header header)
Actually formats a header. Called from {@link #formatHeader}.

param
buffer the empty buffer into which to format, never null
param
header the header to format, never null

        final String name = header.getName();
        final String value = header.getValue();

        int len = name.length() + 2;
        if (value != null) {
            len += value.length();
        }
        buffer.ensureCapacity(len);

        buffer.append(name);
        buffer.append(": ");
        if (value != null) {
            buffer.append(value);
        }
    
protected voiddoFormatRequestLine(org.apache.http.util.CharArrayBuffer buffer, org.apache.http.RequestLine reqline)
Actually formats a request line. Called from {@link #formatRequestLine}.

param
buffer the empty buffer into which to format, never null
param
reqline the request line to format, never null

        final String method = reqline.getMethod();
        final String uri    = reqline.getUri();

        // room for "GET /index.html HTTP/1.1"
        int len = method.length() + 1 + uri.length() + 1 + 
            estimateProtocolVersionLen(reqline.getProtocolVersion());
        buffer.ensureCapacity(len);

        buffer.append(method);
        buffer.append(' ");
        buffer.append(uri);
        buffer.append(' ");
        appendProtocolVersion(buffer, reqline.getProtocolVersion());
    
protected voiddoFormatStatusLine(org.apache.http.util.CharArrayBuffer buffer, org.apache.http.StatusLine statline)
Actually formats a status line. Called from {@link #formatStatusLine}.

param
buffer the empty buffer into which to format, never null
param
statline the status line to format, never null


        int len = estimateProtocolVersionLen(statline.getProtocolVersion())
            + 1 + 3 + 1; // room for "HTTP/1.1 200 "
        final String reason = statline.getReasonPhrase();
        if (reason != null) {
            len += reason.length();
        }
        buffer.ensureCapacity(len);

        appendProtocolVersion(buffer, statline.getProtocolVersion());
        buffer.append(' ");
        buffer.append(Integer.toString(statline.getStatusCode()));
        buffer.append(' "); // keep whitespace even if reason phrase is empty
        if (reason != null) {
            buffer.append(reason);
        }
    
protected intestimateProtocolVersionLen(org.apache.http.ProtocolVersion version)
Guesses the length of a formatted protocol version. Needed to guess the length of a formatted request or status line.

param
version the protocol version to format, or null
return
the estimated length of the formatted protocol version, in characters

        return version.getProtocol().length() + 4; // room for "HTTP/1.1"
    
public static final java.lang.StringformatHeader(org.apache.http.Header header, org.apache.http.message.LineFormatter formatter)
Formats a header.

param
header the header to format
param
formatter the formatter to use, or null for the {@link #DEFAULT default}
return
the formatted header

        if (formatter == null)
            formatter = BasicLineFormatter.DEFAULT;
        return formatter.formatHeader(null, header).toString();
    
public org.apache.http.util.CharArrayBufferformatHeader(org.apache.http.util.CharArrayBuffer buffer, org.apache.http.Header header)

        if (header == null) {
            throw new IllegalArgumentException
                ("Header may not be null");
        }
        CharArrayBuffer result = null;

        if (header instanceof FormattedHeader) {
            // If the header is backed by a buffer, re-use the buffer
            result = ((FormattedHeader)header).getBuffer();
        } else {
            result = initBuffer(buffer);
            doFormatHeader(result, header);
        }
        return result;

    
public static final java.lang.StringformatProtocolVersion(org.apache.http.ProtocolVersion version, org.apache.http.message.LineFormatter formatter)
Formats a protocol version.

param
version the protocol version to format
param
formatter the formatter to use, or null for the {@link #DEFAULT default}
return
the formatted protocol version

        if (formatter == null)
            formatter = BasicLineFormatter.DEFAULT;
        return formatter.appendProtocolVersion(null, version).toString();
    
public static final java.lang.StringformatRequestLine(org.apache.http.RequestLine reqline, org.apache.http.message.LineFormatter formatter)
Formats a request line.

param
reqline the request line to format
param
formatter the formatter to use, or null for the {@link #DEFAULT default}
return
the formatted request line

        if (formatter == null)
            formatter = BasicLineFormatter.DEFAULT;
        return formatter.formatRequestLine(null, reqline).toString();
    
public org.apache.http.util.CharArrayBufferformatRequestLine(org.apache.http.util.CharArrayBuffer buffer, org.apache.http.RequestLine reqline)

        if (reqline == null) {
            throw new IllegalArgumentException
                ("Request line may not be null");
        }

        CharArrayBuffer result = initBuffer(buffer);
        doFormatRequestLine(result, reqline);

        return result;
    
public static final java.lang.StringformatStatusLine(org.apache.http.StatusLine statline, org.apache.http.message.LineFormatter formatter)
Formats a status line.

param
statline the status line to format
param
formatter the formatter to use, or null for the {@link #DEFAULT default}
return
the formatted status line

        if (formatter == null)
            formatter = BasicLineFormatter.DEFAULT;
        return formatter.formatStatusLine(null, statline).toString();
    
public org.apache.http.util.CharArrayBufferformatStatusLine(org.apache.http.util.CharArrayBuffer buffer, org.apache.http.StatusLine statline)

        if (statline == null) {
            throw new IllegalArgumentException
                ("Status line may not be null");
        }

        CharArrayBuffer result = initBuffer(buffer);
        doFormatStatusLine(result, statline);

        return result;
    
protected org.apache.http.util.CharArrayBufferinitBuffer(org.apache.http.util.CharArrayBuffer buffer)
Obtains a buffer for formatting.

param
buffer a buffer already available, or null
return
the cleared argument buffer if there is one, or a new empty buffer that can be used for formatting




    // public default constructor


                                                       
        
        if (buffer != null) {
            buffer.clear();
        } else {
            buffer = new CharArrayBuffer(64);
        }
        return buffer;