mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/io/ChunkedOutputStream.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/ChunkedOutputStream.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,185 @@
     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.io.OutputStream;
    1.35 +
    1.36 +import ch.boye.httpclientandroidlib.io.SessionOutputBuffer;
    1.37 +
    1.38 +/**
    1.39 + * Implements chunked transfer coding. The content is sent in small chunks.
    1.40 + * Entities transferred using this output stream can be of unlimited length.
    1.41 + * Writes are buffered to an internal buffer (2048 default size).
    1.42 + * <p>
    1.43 + * Note that this class NEVER closes the underlying stream, even when close
    1.44 + * gets called.  Instead, the stream will be marked as closed and no further
    1.45 + * output will be permitted.
    1.46 + *
    1.47 + *
    1.48 + * @since 4.0
    1.49 + */
    1.50 +public class ChunkedOutputStream extends OutputStream {
    1.51 +
    1.52 +    // ----------------------------------------------------- Instance Variables
    1.53 +    private final SessionOutputBuffer out;
    1.54 +
    1.55 +    private byte[] cache;
    1.56 +
    1.57 +    private int cachePosition = 0;
    1.58 +
    1.59 +    private boolean wroteLastChunk = false;
    1.60 +
    1.61 +    /** True if the stream is closed. */
    1.62 +    private boolean closed = false;
    1.63 +
    1.64 +    // ----------------------------------------------------------- Constructors
    1.65 +    /**
    1.66 +     * Wraps a session output buffer and chunk-encodes the output.
    1.67 +     *
    1.68 +     * @param out The session output buffer
    1.69 +     * @param bufferSize The minimum chunk size (excluding last chunk)
    1.70 +     * @throws IOException in case of an I/O error
    1.71 +     */
    1.72 +    public ChunkedOutputStream(final SessionOutputBuffer out, int bufferSize)
    1.73 +            throws IOException {
    1.74 +        super();
    1.75 +        this.cache = new byte[bufferSize];
    1.76 +        this.out = out;
    1.77 +    }
    1.78 +
    1.79 +    /**
    1.80 +     * Wraps a session output buffer and chunks the output. The default buffer
    1.81 +     * size of 2048 was chosen because the chunk overhead is less than 0.5%
    1.82 +     *
    1.83 +     * @param out       the output buffer to wrap
    1.84 +     * @throws IOException in case of an I/O error
    1.85 +     */
    1.86 +    public ChunkedOutputStream(final SessionOutputBuffer out)
    1.87 +            throws IOException {
    1.88 +        this(out, 2048);
    1.89 +    }
    1.90 +
    1.91 +    // ----------------------------------------------------------- Internal methods
    1.92 +    /**
    1.93 +     * Writes the cache out onto the underlying stream
    1.94 +     */
    1.95 +    protected void flushCache() throws IOException {
    1.96 +        if (this.cachePosition > 0) {
    1.97 +            this.out.writeLine(Integer.toHexString(this.cachePosition));
    1.98 +            this.out.write(this.cache, 0, this.cachePosition);
    1.99 +            this.out.writeLine("");
   1.100 +            this.cachePosition = 0;
   1.101 +        }
   1.102 +    }
   1.103 +
   1.104 +    /**
   1.105 +     * Writes the cache and bufferToAppend to the underlying stream
   1.106 +     * as one large chunk
   1.107 +     */
   1.108 +    protected void flushCacheWithAppend(byte bufferToAppend[], int off, int len) throws IOException {
   1.109 +        this.out.writeLine(Integer.toHexString(this.cachePosition + len));
   1.110 +        this.out.write(this.cache, 0, this.cachePosition);
   1.111 +        this.out.write(bufferToAppend, off, len);
   1.112 +        this.out.writeLine("");
   1.113 +        this.cachePosition = 0;
   1.114 +    }
   1.115 +
   1.116 +    protected void writeClosingChunk() throws IOException {
   1.117 +        // Write the final chunk.
   1.118 +        this.out.writeLine("0");
   1.119 +        this.out.writeLine("");
   1.120 +    }
   1.121 +
   1.122 +    // ----------------------------------------------------------- Public Methods
   1.123 +    /**
   1.124 +     * Must be called to ensure the internal cache is flushed and the closing
   1.125 +     * chunk is written.
   1.126 +     * @throws IOException in case of an I/O error
   1.127 +     */
   1.128 +    public void finish() throws IOException {
   1.129 +        if (!this.wroteLastChunk) {
   1.130 +            flushCache();
   1.131 +            writeClosingChunk();
   1.132 +            this.wroteLastChunk = true;
   1.133 +        }
   1.134 +    }
   1.135 +
   1.136 +    // -------------------------------------------- OutputStream Methods
   1.137 +    public void write(int b) throws IOException {
   1.138 +        if (this.closed) {
   1.139 +            throw new IOException("Attempted write to closed stream.");
   1.140 +        }
   1.141 +        this.cache[this.cachePosition] = (byte) b;
   1.142 +        this.cachePosition++;
   1.143 +        if (this.cachePosition == this.cache.length) flushCache();
   1.144 +    }
   1.145 +
   1.146 +    /**
   1.147 +     * Writes the array. If the array does not fit within the buffer, it is
   1.148 +     * not split, but rather written out as one large chunk.
   1.149 +     */
   1.150 +    public void write(byte b[]) throws IOException {
   1.151 +        write(b, 0, b.length);
   1.152 +    }
   1.153 +
   1.154 +    /**
   1.155 +     * Writes the array. If the array does not fit within the buffer, it is
   1.156 +     * not split, but rather written out as one large chunk.
   1.157 +     */
   1.158 +    public void write(byte src[], int off, int len) throws IOException {
   1.159 +        if (this.closed) {
   1.160 +            throw new IOException("Attempted write to closed stream.");
   1.161 +        }
   1.162 +        if (len >= this.cache.length - this.cachePosition) {
   1.163 +            flushCacheWithAppend(src, off, len);
   1.164 +        } else {
   1.165 +            System.arraycopy(src, off, cache, this.cachePosition, len);
   1.166 +            this.cachePosition += len;
   1.167 +        }
   1.168 +    }
   1.169 +
   1.170 +    /**
   1.171 +     * Flushes the content buffer and the underlying stream.
   1.172 +     */
   1.173 +    public void flush() throws IOException {
   1.174 +        flushCache();
   1.175 +        this.out.flush();
   1.176 +    }
   1.177 +
   1.178 +    /**
   1.179 +     * Finishes writing to the underlying stream, but does NOT close the underlying stream.
   1.180 +     */
   1.181 +    public void close() throws IOException {
   1.182 +        if (!this.closed) {
   1.183 +            this.closed = true;
   1.184 +            finish();
   1.185 +            this.out.flush();
   1.186 +        }
   1.187 +    }
   1.188 +}

mercurial