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.InputStream; |
michael@0 | 32 | |
michael@0 | 33 | import ch.boye.httpclientandroidlib.Header; |
michael@0 | 34 | import ch.boye.httpclientandroidlib.HttpException; |
michael@0 | 35 | import ch.boye.httpclientandroidlib.MalformedChunkCodingException; |
michael@0 | 36 | import ch.boye.httpclientandroidlib.TruncatedChunkException; |
michael@0 | 37 | import ch.boye.httpclientandroidlib.io.BufferInfo; |
michael@0 | 38 | import ch.boye.httpclientandroidlib.io.SessionInputBuffer; |
michael@0 | 39 | import ch.boye.httpclientandroidlib.util.CharArrayBuffer; |
michael@0 | 40 | import ch.boye.httpclientandroidlib.util.ExceptionUtils; |
michael@0 | 41 | |
michael@0 | 42 | /** |
michael@0 | 43 | * Implements chunked transfer coding. The content is received in small chunks. |
michael@0 | 44 | * Entities transferred using this input stream can be of unlimited length. |
michael@0 | 45 | * After the stream is read to the end, it provides access to the trailers, |
michael@0 | 46 | * if any. |
michael@0 | 47 | * <p> |
michael@0 | 48 | * Note that this class NEVER closes the underlying stream, even when close |
michael@0 | 49 | * gets called. Instead, it will read until the "end" of its chunking on |
michael@0 | 50 | * close, which allows for the seamless execution of subsequent HTTP 1.1 |
michael@0 | 51 | * requests, while not requiring the client to remember to read the entire |
michael@0 | 52 | * contents of the response. |
michael@0 | 53 | * |
michael@0 | 54 | * |
michael@0 | 55 | * @since 4.0 |
michael@0 | 56 | * |
michael@0 | 57 | */ |
michael@0 | 58 | public class ChunkedInputStream extends InputStream { |
michael@0 | 59 | |
michael@0 | 60 | private static final int CHUNK_LEN = 1; |
michael@0 | 61 | private static final int CHUNK_DATA = 2; |
michael@0 | 62 | private static final int CHUNK_CRLF = 3; |
michael@0 | 63 | |
michael@0 | 64 | private static final int BUFFER_SIZE = 2048; |
michael@0 | 65 | |
michael@0 | 66 | /** The session input buffer */ |
michael@0 | 67 | private final SessionInputBuffer in; |
michael@0 | 68 | |
michael@0 | 69 | private final CharArrayBuffer buffer; |
michael@0 | 70 | |
michael@0 | 71 | private int state; |
michael@0 | 72 | |
michael@0 | 73 | /** The chunk size */ |
michael@0 | 74 | private int chunkSize; |
michael@0 | 75 | |
michael@0 | 76 | /** The current position within the current chunk */ |
michael@0 | 77 | private int pos; |
michael@0 | 78 | |
michael@0 | 79 | /** True if we've reached the end of stream */ |
michael@0 | 80 | private boolean eof = false; |
michael@0 | 81 | |
michael@0 | 82 | /** True if this stream is closed */ |
michael@0 | 83 | private boolean closed = false; |
michael@0 | 84 | |
michael@0 | 85 | private Header[] footers = new Header[] {}; |
michael@0 | 86 | |
michael@0 | 87 | /** |
michael@0 | 88 | * Wraps session input stream and reads chunk coded input. |
michael@0 | 89 | * |
michael@0 | 90 | * @param in The session input buffer |
michael@0 | 91 | */ |
michael@0 | 92 | public ChunkedInputStream(final SessionInputBuffer in) { |
michael@0 | 93 | super(); |
michael@0 | 94 | if (in == null) { |
michael@0 | 95 | throw new IllegalArgumentException("Session input buffer may not be null"); |
michael@0 | 96 | } |
michael@0 | 97 | this.in = in; |
michael@0 | 98 | this.pos = 0; |
michael@0 | 99 | this.buffer = new CharArrayBuffer(16); |
michael@0 | 100 | this.state = CHUNK_LEN; |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | public int available() throws IOException { |
michael@0 | 104 | if (this.in instanceof BufferInfo) { |
michael@0 | 105 | int len = ((BufferInfo) this.in).length(); |
michael@0 | 106 | return Math.min(len, this.chunkSize - this.pos); |
michael@0 | 107 | } else { |
michael@0 | 108 | return 0; |
michael@0 | 109 | } |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | /** |
michael@0 | 113 | * <p> Returns all the data in a chunked stream in coalesced form. A chunk |
michael@0 | 114 | * is followed by a CRLF. The method returns -1 as soon as a chunksize of 0 |
michael@0 | 115 | * is detected.</p> |
michael@0 | 116 | * |
michael@0 | 117 | * <p> Trailer headers are read automatically at the end of the stream and |
michael@0 | 118 | * can be obtained with the getResponseFooters() method.</p> |
michael@0 | 119 | * |
michael@0 | 120 | * @return -1 of the end of the stream has been reached or the next data |
michael@0 | 121 | * byte |
michael@0 | 122 | * @throws IOException in case of an I/O error |
michael@0 | 123 | */ |
michael@0 | 124 | public int read() throws IOException { |
michael@0 | 125 | if (this.closed) { |
michael@0 | 126 | throw new IOException("Attempted read from closed stream."); |
michael@0 | 127 | } |
michael@0 | 128 | if (this.eof) { |
michael@0 | 129 | return -1; |
michael@0 | 130 | } |
michael@0 | 131 | if (state != CHUNK_DATA) { |
michael@0 | 132 | nextChunk(); |
michael@0 | 133 | if (this.eof) { |
michael@0 | 134 | return -1; |
michael@0 | 135 | } |
michael@0 | 136 | } |
michael@0 | 137 | int b = in.read(); |
michael@0 | 138 | if (b != -1) { |
michael@0 | 139 | pos++; |
michael@0 | 140 | if (pos >= chunkSize) { |
michael@0 | 141 | state = CHUNK_CRLF; |
michael@0 | 142 | } |
michael@0 | 143 | } |
michael@0 | 144 | return b; |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | /** |
michael@0 | 148 | * Read some bytes from the stream. |
michael@0 | 149 | * @param b The byte array that will hold the contents from the stream. |
michael@0 | 150 | * @param off The offset into the byte array at which bytes will start to be |
michael@0 | 151 | * placed. |
michael@0 | 152 | * @param len the maximum number of bytes that can be returned. |
michael@0 | 153 | * @return The number of bytes returned or -1 if the end of stream has been |
michael@0 | 154 | * reached. |
michael@0 | 155 | * @throws IOException in case of an I/O error |
michael@0 | 156 | */ |
michael@0 | 157 | public int read (byte[] b, int off, int len) throws IOException { |
michael@0 | 158 | |
michael@0 | 159 | if (closed) { |
michael@0 | 160 | throw new IOException("Attempted read from closed stream."); |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | if (eof) { |
michael@0 | 164 | return -1; |
michael@0 | 165 | } |
michael@0 | 166 | if (state != CHUNK_DATA) { |
michael@0 | 167 | nextChunk(); |
michael@0 | 168 | if (eof) { |
michael@0 | 169 | return -1; |
michael@0 | 170 | } |
michael@0 | 171 | } |
michael@0 | 172 | len = Math.min(len, chunkSize - pos); |
michael@0 | 173 | int bytesRead = in.read(b, off, len); |
michael@0 | 174 | if (bytesRead != -1) { |
michael@0 | 175 | pos += bytesRead; |
michael@0 | 176 | if (pos >= chunkSize) { |
michael@0 | 177 | state = CHUNK_CRLF; |
michael@0 | 178 | } |
michael@0 | 179 | return bytesRead; |
michael@0 | 180 | } else { |
michael@0 | 181 | eof = true; |
michael@0 | 182 | throw new TruncatedChunkException("Truncated chunk " |
michael@0 | 183 | + "( expected size: " + chunkSize |
michael@0 | 184 | + "; actual size: " + pos + ")"); |
michael@0 | 185 | } |
michael@0 | 186 | } |
michael@0 | 187 | |
michael@0 | 188 | /** |
michael@0 | 189 | * Read some bytes from the stream. |
michael@0 | 190 | * @param b The byte array that will hold the contents from the stream. |
michael@0 | 191 | * @return The number of bytes returned or -1 if the end of stream has been |
michael@0 | 192 | * reached. |
michael@0 | 193 | * @throws IOException in case of an I/O error |
michael@0 | 194 | */ |
michael@0 | 195 | public int read (byte[] b) throws IOException { |
michael@0 | 196 | return read(b, 0, b.length); |
michael@0 | 197 | } |
michael@0 | 198 | |
michael@0 | 199 | /** |
michael@0 | 200 | * Read the next chunk. |
michael@0 | 201 | * @throws IOException in case of an I/O error |
michael@0 | 202 | */ |
michael@0 | 203 | private void nextChunk() throws IOException { |
michael@0 | 204 | chunkSize = getChunkSize(); |
michael@0 | 205 | if (chunkSize < 0) { |
michael@0 | 206 | throw new MalformedChunkCodingException("Negative chunk size"); |
michael@0 | 207 | } |
michael@0 | 208 | state = CHUNK_DATA; |
michael@0 | 209 | pos = 0; |
michael@0 | 210 | if (chunkSize == 0) { |
michael@0 | 211 | eof = true; |
michael@0 | 212 | parseTrailerHeaders(); |
michael@0 | 213 | } |
michael@0 | 214 | } |
michael@0 | 215 | |
michael@0 | 216 | /** |
michael@0 | 217 | * Expects the stream to start with a chunksize in hex with optional |
michael@0 | 218 | * comments after a semicolon. The line must end with a CRLF: "a3; some |
michael@0 | 219 | * comment\r\n" Positions the stream at the start of the next line. |
michael@0 | 220 | * |
michael@0 | 221 | * @param in The new input stream. |
michael@0 | 222 | * @param required <tt>true<tt/> if a valid chunk must be present, |
michael@0 | 223 | * <tt>false<tt/> otherwise. |
michael@0 | 224 | * |
michael@0 | 225 | * @return the chunk size as integer |
michael@0 | 226 | * |
michael@0 | 227 | * @throws IOException when the chunk size could not be parsed |
michael@0 | 228 | */ |
michael@0 | 229 | private int getChunkSize() throws IOException { |
michael@0 | 230 | int st = this.state; |
michael@0 | 231 | switch (st) { |
michael@0 | 232 | case CHUNK_CRLF: |
michael@0 | 233 | this.buffer.clear(); |
michael@0 | 234 | int i = this.in.readLine(this.buffer); |
michael@0 | 235 | if (i == -1) { |
michael@0 | 236 | return 0; |
michael@0 | 237 | } |
michael@0 | 238 | if (!this.buffer.isEmpty()) { |
michael@0 | 239 | throw new MalformedChunkCodingException( |
michael@0 | 240 | "Unexpected content at the end of chunk"); |
michael@0 | 241 | } |
michael@0 | 242 | state = CHUNK_LEN; |
michael@0 | 243 | //$FALL-THROUGH$ |
michael@0 | 244 | case CHUNK_LEN: |
michael@0 | 245 | this.buffer.clear(); |
michael@0 | 246 | i = this.in.readLine(this.buffer); |
michael@0 | 247 | if (i == -1) { |
michael@0 | 248 | return 0; |
michael@0 | 249 | } |
michael@0 | 250 | int separator = this.buffer.indexOf(';'); |
michael@0 | 251 | if (separator < 0) { |
michael@0 | 252 | separator = this.buffer.length(); |
michael@0 | 253 | } |
michael@0 | 254 | try { |
michael@0 | 255 | return Integer.parseInt(this.buffer.substringTrimmed(0, separator), 16); |
michael@0 | 256 | } catch (NumberFormatException e) { |
michael@0 | 257 | throw new MalformedChunkCodingException("Bad chunk header"); |
michael@0 | 258 | } |
michael@0 | 259 | default: |
michael@0 | 260 | throw new IllegalStateException("Inconsistent codec state"); |
michael@0 | 261 | } |
michael@0 | 262 | } |
michael@0 | 263 | |
michael@0 | 264 | /** |
michael@0 | 265 | * Reads and stores the Trailer headers. |
michael@0 | 266 | * @throws IOException in case of an I/O error |
michael@0 | 267 | */ |
michael@0 | 268 | private void parseTrailerHeaders() throws IOException { |
michael@0 | 269 | try { |
michael@0 | 270 | this.footers = AbstractMessageParser.parseHeaders |
michael@0 | 271 | (in, -1, -1, null); |
michael@0 | 272 | } catch (HttpException e) { |
michael@0 | 273 | IOException ioe = new MalformedChunkCodingException("Invalid footer: " |
michael@0 | 274 | + e.getMessage()); |
michael@0 | 275 | ExceptionUtils.initCause(ioe, e); |
michael@0 | 276 | throw ioe; |
michael@0 | 277 | } |
michael@0 | 278 | } |
michael@0 | 279 | |
michael@0 | 280 | /** |
michael@0 | 281 | * Upon close, this reads the remainder of the chunked message, |
michael@0 | 282 | * leaving the underlying socket at a position to start reading the |
michael@0 | 283 | * next response without scanning. |
michael@0 | 284 | * @throws IOException in case of an I/O error |
michael@0 | 285 | */ |
michael@0 | 286 | public void close() throws IOException { |
michael@0 | 287 | if (!closed) { |
michael@0 | 288 | try { |
michael@0 | 289 | if (!eof) { |
michael@0 | 290 | // read and discard the remainder of the message |
michael@0 | 291 | byte buffer[] = new byte[BUFFER_SIZE]; |
michael@0 | 292 | while (read(buffer) >= 0) { |
michael@0 | 293 | } |
michael@0 | 294 | } |
michael@0 | 295 | } finally { |
michael@0 | 296 | eof = true; |
michael@0 | 297 | closed = true; |
michael@0 | 298 | } |
michael@0 | 299 | } |
michael@0 | 300 | } |
michael@0 | 301 | |
michael@0 | 302 | public Header[] getFooters() { |
michael@0 | 303 | return (Header[])this.footers.clone(); |
michael@0 | 304 | } |
michael@0 | 305 | |
michael@0 | 306 | } |