2362N/A * Copyright (c) 1999, 2006, Oracle and/or its affiliates. All rights reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 2362N/A * published by the Free Software Foundation. Oracle designates this 0N/A * particular file as subject to the "Classpath" exception as provided 2362N/A * by Oracle in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2362N/A * or visit www.oracle.com if you need additional information or have any 0N/A * A <code>ChunkedInputStream</code> provides a stream for reading a body of 0N/A * a http message that can be sent as a series of chunks, each with its own 0N/A * size indicator. Optionally the last chunk can be followed by trailers 0N/A * containing entity-header fields. 0N/A * A <code>ChunkedInputStream</code> is also <code>Hurryable</code> so it 0N/A * can be hurried to the end of the stream if the bytes are available on 0N/A * the underlying stream. 0N/A * The underlying stream 0N/A * The <code>HttpClient</code> that should be notified when the chunked stream has 0N/A * The <code>MessageHeader</code> that is populated with any optional trailer 0N/A * that appear after the last chunk. 0N/A * The size, in bytes, of the chunk that is currently being read. 0N/A * This size is only valid if the current position in the underlying 0N/A * input stream is inside a chunk (ie: state == STATE_READING_CHUNK). 0N/A * The number of bytes read from the underlying stream for the current 0N/A * chunk. This value is always in the range <code>0</code> through to 0N/A * <code>chunkSize</code> 0N/A * The internal buffer array where chunk data is available for the 0N/A * application to read. 0N/A * The current position in the buffer. It contains the index 0N/A * of the next byte to read from <code>chunkData</code> 0N/A * The index one greater than the index of the last valid byte in the 0N/A * buffer. This value is always in the range <code>0</code> through 0N/A * <code>chunkData.length</code>. 0N/A * The internal buffer where bytes from the underlying stream can be 0N/A * read. It may contain bytes representing chunk-size, chunk-data, or 0N/A * The current position in the buffer. It contains the index 0N/A * of the next byte to read from <code>rawData</code> 0N/A * The index one greater than the index of the last valid byte in the 0N/A * buffer. This value is always in the range <code>0</code> through 0N/A * <code>rawData.length</code>. 0N/A * Indicates if an error was encountered when processing the chunked 0N/A * Indicates if the chunked stream has been closed using the 0N/A * <code>close</code> method. 5683N/A * Maximum chunk header size of 2KB + 2 bytes for CRLF 0N/A * State to indicate that next field should be :- 0N/A * chunk-size [ chunk-extension ] CRLF 0N/A * State to indicate that we are currently reading the chunk-data. 0N/A * Indicates that a chunk has been completely read and the next 0N/A * fields to be examine should be CRLF 0N/A * Indicates that all chunks have been read and the next field 0N/A * should be optional trailers or an indication that the chunked 0N/A * stream is complete. 0N/A * State to indicate that the chunked stream is complete and 0N/A * no further bytes should be read from the underlying stream. 0N/A * Indicates the current state. 0N/A * Check to make sure that this stream has not been closed. 0N/A * Ensures there is <code>size</code> bytes available in 0N/A * <code>rawData</code>. This requires that we either 0N/A * shift the bytes in use to the begining of the buffer 0N/A * or allocate a large buffer with sufficient space available. 0N/A * Close the underlying input stream by either returning it to the 0N/A * keep alive cache or closing the stream. 0N/A * As a chunked stream is inheritly persistent (see HTTP 1.1 RFC) the 0N/A * underlying stream can be returned to the keep alive cache if the 0N/A * stream can be completely read without error. 0N/A * Attempt to read the remainder of a chunk directly into the 0N/A * Return the number of bytes read. 0N/A // assert state == STATE_READING_CHUNKS; 0N/A * Process any outstanding bytes that have already been read into 0N/A * <code>rawData</code>. 0N/A * The parsing of the chunked stream is performed as a state machine with 0N/A * <code>state</code> representing the current state of the processing. 0N/A * Returns when either all the outstanding bytes in rawData have been 0N/A * processed or there is insufficient bytes available to continue 0N/A * processing. When the latter occurs <code>rawPos</code> will not have 0N/A * been updated and thus the processing can be restarted once further 0N/A * bytes have been read into <code>rawData</code>. 0N/A * We are awaiting a line with a chunk header 0N/A * Find \n to indicate end of chunk header. If not found when there is 0N/A * insufficient bytes in the raw buffer to parse a chunk header. 0N/A * Extract the chunk size from the header (ignoring extensions). 0N/A * Chunk has been parsed so move rawPos to first byte of chunk 0N/A * A chunk size of 0 means EOF. 0N/A * We are awaiting raw entity data (some may have already been 0N/A * read). chunkSize is the size of the chunk; chunkRead is the 0N/A * total read from the underlying stream to date. 0N/A /* no data available yet */ 0N/A * Compute the number of bytes of chunk data available in the 0N/A * Expand or compact chunkData if needed. 0N/A * Copy the chunk data into chunkData so that it's available 0N/A * to the read methods. 0N/A * If all the chunk has been copied into chunkData then the next 0N/A * token should be CRLF. 0N/A * Awaiting CRLF after the chunk 0N/A /* not available yet */ 0N/A * Move onto the next chunk 0N/A * Last chunk has been read so not we're waiting for optional 0N/A * Do we have an entire line in the raw buffer? 0N/A * Stream done so close underlying stream. 0N/A * Extract any tailers and append them to the message 0N/A throw new IOException(
"Malformed tailer - format should be key:value");
0N/A * Move onto the next trailer. 0N/A * Reads any available bytes from the underlying stream into 0N/A * <code>rawData</code> and returns the number of bytes of 0N/A * chunk data available in <code>chunkData</code> that the 0N/A * application can read. 0N/A * If there's anything available on the underlying stream then we read 0N/A * it into the raw buffer and process it. Processing ensures that any 0N/A * available chunk data is made available in chunkData. 0N/A /* ensure that there is space in rawData to read the available */ 0N/A * Process the raw bytes that have been read. 0N/A * Return the number of chunked bytes available to read 0N/A * Reads from the underlying stream until there is chunk data 0N/A * available in <code>chunkData</code> for the application to 0N/A * All of chunked response has been read to return EOF. 0N/A * We must read into the raw buffer so make sure there is space 0N/A * available. We use a size of 32 to avoid too much chunk data 0N/A * being read into the raw buffer. 0N/A * If we hit EOF it means there's a problem as we should never 0N/A * attempt to read once the last chunk and trailers have been 0N/A * Process the bytes from the underlying stream 0N/A * Return the number of chunked bytes available to read 0N/A * Read ahead in either blocking or non-blocking mode. This method 0N/A * is typically used when we run out of available bytes in 0N/A * <code>chunkData</code> or we need to determine how many bytes 0N/A * are available on the input stream. 0N/A * Last chunk already received - return EOF 0N/A * Read ahead blocking or non-blocking 0N/A * Creates a <code>ChunkedInputStream</code> and saves its arguments, for 0N/A * @param in the underlying input stream. 0N/A * @param hc the HttpClient 0N/A * @param responses the MessageHeader that should be populated with optional 0N/A /* save arguments */ 0N/A * Set our initial state to indicate that we are first starting to 0N/A * look for a chunk header. 0N/A * the general contract of the <code>read</code> 0N/A * method of <code>InputStream</code>. 0N/A * @return the next byte of data, or <code>-1</code> if the end of the 0N/A * stream is reached. 0N/A * @exception IOException if an I/O error occurs. 0N/A * @see java.io.FilterInputStream#in 0N/A * Reads bytes from this stream into the specified byte array, starting at 0N/A * @param b destination buffer. 0N/A * @param off offset at which to start storing bytes. 0N/A * @param len maximum number of bytes to read. 0N/A * @return the number of bytes read, or <code>-1</code> if the end of 0N/A * the stream has been reached. 0N/A * @exception IOException if an I/O error occurs. 0N/A * Optimization: if we're in the middle of the chunk read 0N/A * directly from the underlying stream into the caller's 0N/A * We're not in the middle of a chunk so we must read ahead 0N/A * until there is some chunk data available. 0N/A return -
1;
/* EOF */ 0N/A * Returns the number of bytes that can be read from this input 0N/A * stream without blocking. 0N/A * @return the number of bytes that can be read from this input 0N/A * stream without blocking. 0N/A * @exception IOException if an I/O error occurs. 0N/A * @see java.io.FilterInputStream#in 0N/A * Close the stream by either returning the connection to the 0N/A * keep alive cache or closing the underlying stream. 0N/A * If the chunked response hasn't been completely read we 0N/A * try to "hurry" to the end of the response. If this is 0N/A * possible (without blocking) then the connection can be 0N/A * returned to the keep alive cache. 0N/A * @exception IOException if an I/O error occurs. 0N/A * Hurry the input stream by reading everything from the underlying 0N/A * stream. If the last chunk (and optional trailers) can be read without 0N/A * blocking then the stream is considered hurried. 0N/A * Note that if an error has occured or we can't get to last chunk 0N/A * without blocking then this stream can't be hurried and should be