Code example for CharArrayBuffer

Methods: append, ensureCapacity

0
        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;
    } 
 
 
    /** 
     * 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 <code>null</code> 
     * 
     * @return  the estimated length of the formatted protocol version, 
     *          in characters 
     */ 
    protected int estimateProtocolVersionLen(final ProtocolVersion version) {
        return version.getProtocol().length() + 4; // room for "HTTP/1.1"
    } 
 
 
    /** 
     * Formats a request line. 
     * 
     * @param reqline           the request line to format 
     * @param formatter         the formatter to use, or 
     *                          <code>null</code> for the 
     *                          {@link #DEFAULT default} 
     * 
     * @return  the formatted request line 
     */ 
    public final static String formatRequestLine(final RequestLine reqline,
                                                 LineFormatter formatter) {
        if (formatter == null)
            formatter = BasicLineFormatter.DEFAULT;
        return formatter.formatRequestLine(null, reqline).toString();
    } 
 
 
    // non-javadoc, see interface LineFormatter 
    public CharArrayBuffer formatRequestLine(CharArrayBuffer buffer,
                                             RequestLine reqline) {
        if (reqline == null) {
            throw new IllegalArgumentException
                ("Request line may not be null"); 
        } 
 
        CharArrayBuffer result = initBuffer(buffer);
        doFormatRequestLine(result, reqline);
 
        return result;
    } 
 
 
    /** 
     * Actually formats a request line. 
     * Called from {@link #formatRequestLine}. 
     * 
     * @param buffer    the empty buffer into which to format, 
     *                  never <code>null</code> 
     * @param reqline   the request line to format, never <code>null</code> 
     */ 
    protected void doFormatRequestLine(final CharArrayBuffer buffer,
                                       final RequestLine reqline) {
        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());
    }