View Javadoc

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