Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* |
michael@0 | 2 | * ==================================================================== |
michael@0 | 3 | * Licensed to the Apache Software Foundation (ASF) under one |
michael@0 | 4 | * or more contributor license agreements. See the NOTICE file |
michael@0 | 5 | * distributed with this work for additional information |
michael@0 | 6 | * regarding copyright ownership. The ASF licenses this file |
michael@0 | 7 | * to you under the Apache License, Version 2.0 (the |
michael@0 | 8 | * "License"); you may not use this file except in compliance |
michael@0 | 9 | * with the License. You may obtain a copy of the License at |
michael@0 | 10 | * |
michael@0 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 12 | * |
michael@0 | 13 | * Unless required by applicable law or agreed to in writing, |
michael@0 | 14 | * software distributed under the License is distributed on an |
michael@0 | 15 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
michael@0 | 16 | * KIND, either express or implied. See the License for the |
michael@0 | 17 | * specific language governing permissions and limitations |
michael@0 | 18 | * under the License. |
michael@0 | 19 | * ==================================================================== |
michael@0 | 20 | * |
michael@0 | 21 | * This software consists of voluntary contributions made by many |
michael@0 | 22 | * individuals on behalf of the Apache Software Foundation. For more |
michael@0 | 23 | * information on the Apache Software Foundation, please see |
michael@0 | 24 | * <http://www.apache.org/>. |
michael@0 | 25 | * |
michael@0 | 26 | */ |
michael@0 | 27 | |
michael@0 | 28 | package ch.boye.httpclientandroidlib.impl.io; |
michael@0 | 29 | |
michael@0 | 30 | import java.io.IOException; |
michael@0 | 31 | import java.io.OutputStream; |
michael@0 | 32 | |
michael@0 | 33 | import ch.boye.httpclientandroidlib.io.SessionOutputBuffer; |
michael@0 | 34 | |
michael@0 | 35 | /** |
michael@0 | 36 | * Implements chunked transfer coding. The content is sent in small chunks. |
michael@0 | 37 | * Entities transferred using this output stream can be of unlimited length. |
michael@0 | 38 | * Writes are buffered to an internal buffer (2048 default size). |
michael@0 | 39 | * <p> |
michael@0 | 40 | * Note that this class NEVER closes the underlying stream, even when close |
michael@0 | 41 | * gets called. Instead, the stream will be marked as closed and no further |
michael@0 | 42 | * output will be permitted. |
michael@0 | 43 | * |
michael@0 | 44 | * |
michael@0 | 45 | * @since 4.0 |
michael@0 | 46 | */ |
michael@0 | 47 | public class ChunkedOutputStream extends OutputStream { |
michael@0 | 48 | |
michael@0 | 49 | // ----------------------------------------------------- Instance Variables |
michael@0 | 50 | private final SessionOutputBuffer out; |
michael@0 | 51 | |
michael@0 | 52 | private byte[] cache; |
michael@0 | 53 | |
michael@0 | 54 | private int cachePosition = 0; |
michael@0 | 55 | |
michael@0 | 56 | private boolean wroteLastChunk = false; |
michael@0 | 57 | |
michael@0 | 58 | /** True if the stream is closed. */ |
michael@0 | 59 | private boolean closed = false; |
michael@0 | 60 | |
michael@0 | 61 | // ----------------------------------------------------------- Constructors |
michael@0 | 62 | /** |
michael@0 | 63 | * Wraps a session output buffer and chunk-encodes the output. |
michael@0 | 64 | * |
michael@0 | 65 | * @param out The session output buffer |
michael@0 | 66 | * @param bufferSize The minimum chunk size (excluding last chunk) |
michael@0 | 67 | * @throws IOException in case of an I/O error |
michael@0 | 68 | */ |
michael@0 | 69 | public ChunkedOutputStream(final SessionOutputBuffer out, int bufferSize) |
michael@0 | 70 | throws IOException { |
michael@0 | 71 | super(); |
michael@0 | 72 | this.cache = new byte[bufferSize]; |
michael@0 | 73 | this.out = out; |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | /** |
michael@0 | 77 | * Wraps a session output buffer and chunks the output. The default buffer |
michael@0 | 78 | * size of 2048 was chosen because the chunk overhead is less than 0.5% |
michael@0 | 79 | * |
michael@0 | 80 | * @param out the output buffer to wrap |
michael@0 | 81 | * @throws IOException in case of an I/O error |
michael@0 | 82 | */ |
michael@0 | 83 | public ChunkedOutputStream(final SessionOutputBuffer out) |
michael@0 | 84 | throws IOException { |
michael@0 | 85 | this(out, 2048); |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | // ----------------------------------------------------------- Internal methods |
michael@0 | 89 | /** |
michael@0 | 90 | * Writes the cache out onto the underlying stream |
michael@0 | 91 | */ |
michael@0 | 92 | protected void flushCache() throws IOException { |
michael@0 | 93 | if (this.cachePosition > 0) { |
michael@0 | 94 | this.out.writeLine(Integer.toHexString(this.cachePosition)); |
michael@0 | 95 | this.out.write(this.cache, 0, this.cachePosition); |
michael@0 | 96 | this.out.writeLine(""); |
michael@0 | 97 | this.cachePosition = 0; |
michael@0 | 98 | } |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | /** |
michael@0 | 102 | * Writes the cache and bufferToAppend to the underlying stream |
michael@0 | 103 | * as one large chunk |
michael@0 | 104 | */ |
michael@0 | 105 | protected void flushCacheWithAppend(byte bufferToAppend[], int off, int len) throws IOException { |
michael@0 | 106 | this.out.writeLine(Integer.toHexString(this.cachePosition + len)); |
michael@0 | 107 | this.out.write(this.cache, 0, this.cachePosition); |
michael@0 | 108 | this.out.write(bufferToAppend, off, len); |
michael@0 | 109 | this.out.writeLine(""); |
michael@0 | 110 | this.cachePosition = 0; |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | protected void writeClosingChunk() throws IOException { |
michael@0 | 114 | // Write the final chunk. |
michael@0 | 115 | this.out.writeLine("0"); |
michael@0 | 116 | this.out.writeLine(""); |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | // ----------------------------------------------------------- Public Methods |
michael@0 | 120 | /** |
michael@0 | 121 | * Must be called to ensure the internal cache is flushed and the closing |
michael@0 | 122 | * chunk is written. |
michael@0 | 123 | * @throws IOException in case of an I/O error |
michael@0 | 124 | */ |
michael@0 | 125 | public void finish() throws IOException { |
michael@0 | 126 | if (!this.wroteLastChunk) { |
michael@0 | 127 | flushCache(); |
michael@0 | 128 | writeClosingChunk(); |
michael@0 | 129 | this.wroteLastChunk = true; |
michael@0 | 130 | } |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | // -------------------------------------------- OutputStream Methods |
michael@0 | 134 | public void write(int b) throws IOException { |
michael@0 | 135 | if (this.closed) { |
michael@0 | 136 | throw new IOException("Attempted write to closed stream."); |
michael@0 | 137 | } |
michael@0 | 138 | this.cache[this.cachePosition] = (byte) b; |
michael@0 | 139 | this.cachePosition++; |
michael@0 | 140 | if (this.cachePosition == this.cache.length) flushCache(); |
michael@0 | 141 | } |
michael@0 | 142 | |
michael@0 | 143 | /** |
michael@0 | 144 | * Writes the array. If the array does not fit within the buffer, it is |
michael@0 | 145 | * not split, but rather written out as one large chunk. |
michael@0 | 146 | */ |
michael@0 | 147 | public void write(byte b[]) throws IOException { |
michael@0 | 148 | write(b, 0, b.length); |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | /** |
michael@0 | 152 | * Writes the array. If the array does not fit within the buffer, it is |
michael@0 | 153 | * not split, but rather written out as one large chunk. |
michael@0 | 154 | */ |
michael@0 | 155 | public void write(byte src[], int off, int len) throws IOException { |
michael@0 | 156 | if (this.closed) { |
michael@0 | 157 | throw new IOException("Attempted write to closed stream."); |
michael@0 | 158 | } |
michael@0 | 159 | if (len >= this.cache.length - this.cachePosition) { |
michael@0 | 160 | flushCacheWithAppend(src, off, len); |
michael@0 | 161 | } else { |
michael@0 | 162 | System.arraycopy(src, off, cache, this.cachePosition, len); |
michael@0 | 163 | this.cachePosition += len; |
michael@0 | 164 | } |
michael@0 | 165 | } |
michael@0 | 166 | |
michael@0 | 167 | /** |
michael@0 | 168 | * Flushes the content buffer and the underlying stream. |
michael@0 | 169 | */ |
michael@0 | 170 | public void flush() throws IOException { |
michael@0 | 171 | flushCache(); |
michael@0 | 172 | this.out.flush(); |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | /** |
michael@0 | 176 | * Finishes writing to the underlying stream, but does NOT close the underlying stream. |
michael@0 | 177 | */ |
michael@0 | 178 | public void close() throws IOException { |
michael@0 | 179 | if (!this.closed) { |
michael@0 | 180 | this.closed = true; |
michael@0 | 181 | finish(); |
michael@0 | 182 | this.out.flush(); |
michael@0 | 183 | } |
michael@0 | 184 | } |
michael@0 | 185 | } |