mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/io/AbstractMessageWriter.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/io/AbstractMessageWriter.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,99 @@
     1.4 +/*
     1.5 + * ====================================================================
     1.6 + * Licensed to the Apache Software Foundation (ASF) under one
     1.7 + * or more contributor license agreements.  See the NOTICE file
     1.8 + * distributed with this work for additional information
     1.9 + * regarding copyright ownership.  The ASF licenses this file
    1.10 + * to you under the Apache License, Version 2.0 (the
    1.11 + * "License"); you may not use this file except in compliance
    1.12 + * with the License.  You may obtain a copy of the License at
    1.13 + *
    1.14 + *   http://www.apache.org/licenses/LICENSE-2.0
    1.15 + *
    1.16 + * Unless required by applicable law or agreed to in writing,
    1.17 + * software distributed under the License is distributed on an
    1.18 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    1.19 + * KIND, either express or implied.  See the License for the
    1.20 + * specific language governing permissions and limitations
    1.21 + * under the License.
    1.22 + * ====================================================================
    1.23 + *
    1.24 + * This software consists of voluntary contributions made by many
    1.25 + * individuals on behalf of the Apache Software Foundation.  For more
    1.26 + * information on the Apache Software Foundation, please see
    1.27 + * <http://www.apache.org/>.
    1.28 + *
    1.29 + */
    1.30 +
    1.31 +package ch.boye.httpclientandroidlib.impl.io;
    1.32 +
    1.33 +import java.io.IOException;
    1.34 +import java.util.Iterator;
    1.35 +
    1.36 +import ch.boye.httpclientandroidlib.Header;
    1.37 +import ch.boye.httpclientandroidlib.HttpException;
    1.38 +import ch.boye.httpclientandroidlib.HttpMessage;
    1.39 +import ch.boye.httpclientandroidlib.io.HttpMessageWriter;
    1.40 +import ch.boye.httpclientandroidlib.io.SessionOutputBuffer;
    1.41 +import ch.boye.httpclientandroidlib.message.LineFormatter;
    1.42 +import ch.boye.httpclientandroidlib.message.BasicLineFormatter;
    1.43 +import ch.boye.httpclientandroidlib.params.HttpParams;
    1.44 +import ch.boye.httpclientandroidlib.util.CharArrayBuffer;
    1.45 +
    1.46 +/**
    1.47 + * Abstract base class for HTTP message writers that serialize output to
    1.48 + * an instance of {@link SessionOutputBuffer}.
    1.49 + *
    1.50 + * @since 4.0
    1.51 + */
    1.52 +public abstract class AbstractMessageWriter implements HttpMessageWriter {
    1.53 +
    1.54 +    protected final SessionOutputBuffer sessionBuffer;
    1.55 +    protected final CharArrayBuffer lineBuf;
    1.56 +    protected final LineFormatter lineFormatter;
    1.57 +
    1.58 +    /**
    1.59 +     * Creates an instance of AbstractMessageWriter.
    1.60 +     *
    1.61 +     * @param buffer the session output buffer.
    1.62 +     * @param formatter the line formatter.
    1.63 +     * @param params HTTP parameters.
    1.64 +     */
    1.65 +    public AbstractMessageWriter(final SessionOutputBuffer buffer,
    1.66 +                                 final LineFormatter formatter,
    1.67 +                                 final HttpParams params) {
    1.68 +        super();
    1.69 +        if (buffer == null) {
    1.70 +            throw new IllegalArgumentException("Session input buffer may not be null");
    1.71 +        }
    1.72 +        this.sessionBuffer = buffer;
    1.73 +        this.lineBuf = new CharArrayBuffer(128);
    1.74 +        this.lineFormatter = (formatter != null) ?
    1.75 +            formatter : BasicLineFormatter.DEFAULT;
    1.76 +    }
    1.77 +
    1.78 +    /**
    1.79 +     * Subclasses must override this method to write out the first header line
    1.80 +     * based on the {@link HttpMessage} passed as a parameter.
    1.81 +     *
    1.82 +     * @param message the message whose first line is to be written out.
    1.83 +     * @throws IOException in case of an I/O error.
    1.84 +     */
    1.85 +    protected abstract void writeHeadLine(HttpMessage message) throws IOException;
    1.86 +
    1.87 +    public void write(
    1.88 +            final HttpMessage message) throws IOException, HttpException {
    1.89 +        if (message == null) {
    1.90 +            throw new IllegalArgumentException("HTTP message may not be null");
    1.91 +        }
    1.92 +        writeHeadLine(message);
    1.93 +        for (Iterator it = message.headerIterator(); it.hasNext(); ) {
    1.94 +            Header header = (Header) it.next();
    1.95 +            this.sessionBuffer.writeLine
    1.96 +                (lineFormatter.formatHeader(this.lineBuf, header));
    1.97 +        }
    1.98 +        this.lineBuf.clear();
    1.99 +        this.sessionBuffer.writeLine(this.lineBuf);
   1.100 +    }
   1.101 +
   1.102 +}

mercurial