ChunkedInputStream provides a stream for reading a body of
a http message that can be sent as a series of chunks, each with its own
size indicator. Optionally the last chunk can be followed by trailers
containing entity-header fields.
A ChunkedInputStream is also Hurryable so it
can be hurried to the end of the stream if the bytes are available on
the underlying stream.
| Modifier and Type | Field and Description |
|---|---|
| private int | chunkCount
The index one greater than the index of the last valid byte in the buffer. |
| private byte[] | chunkData
The internal buffer array where chunk data is available for the application to read. |
| private int | chunkPos
The current position in the buffer. |
| private int | chunkRead
The number of bytes read from the underlying stream for the current chunk. |
| private int | chunkSize
The size, in bytes, of the chunk that is currently being read. |
| private boolean | closed
Indicates if the chunked stream has been closed using the
|
| private boolean | error
Indicates if an error was encountered when processing the chunked stream. |
| private final HttpClient | hc
The |
| private InputStream | in
The underlying stream |
| private static final int | |
| private int | rawCount
The index one greater than the index of the last valid byte in the buffer. |
| private byte[] | rawData
The internal buffer where bytes from the underlying stream can be read. |
| private int | rawPos
The current position in the buffer. |
| private final ReentrantLock | |
| private final MessageHeader | responses
The |
| private int | state
Indicates the current state. |
| pack-priv static final int | STATE_AWAITING_CHUNK_EOL
Indicates that a chunk has been completely read and the next fields to be examine should be CRLF |
| pack-priv static final int | STATE_AWAITING_CHUNK_HEADER
State to indicate that next field should be :- chunk-size [ chunk-extension ] CRLF |
| pack-priv static final int | STATE_AWAITING_TRAILERS
Indicates that all chunks have been read and the next field should be optional trailers or an indication that the chunked stream is complete. |
| pack-priv static final int | STATE_DONE
State to indicate that the chunked stream is complete and no further bytes should be read from the underlying stream. |
| pack-priv static final int | STATE_READING_CHUNK
State to indicate that we are currently reading the chunk-data. |
| Access | Constructor and Description |
|---|---|
| public | ChunkedInputStream(InputStream
the underlying input stream. in, HttpClient the HttpClient hc, MessageHeader the MessageHeader that should be populated with optional
trailers. responses)Creates a |
| Modifier and Type | Method and Description |
|---|---|
| public int | Returns: the number of bytes that can be read from this input stream without blocking.Overrides java. Returns the number of bytes that can be read from this input stream without blocking. |
| public void | close()
Overrides java. Implements java. Close the stream by either returning the connection to the keep alive cache or closing the underlying stream. |
| private void | closeUnderlying()
Close the underlying input stream by either returning it to the keep alive cache or closing the stream. |
| private void | |
| private void | |
| private int | fastRead(byte[] b, int off, int len)
Attempt to read the remainder of a chunk directly into the caller's buffer. |
| public boolean | hurry()
Implements sun. Hurry the input stream by reading everything from the underlying stream. |
| private void | |
| public int | Returns: the next byte of data, or-1 if the end of the
stream is reached.Implements abstract java. See
the general contract of the |
| public int | Returns: the number of bytes read, or-1 if the end of
the stream has been reached.destination buffer. b, int offset at which to start storing bytes. off, int maximum number of bytes to read. len)Overrides java. Reads bytes from this stream into the specified byte array, starting at the given offset. |
| private int | |
| private int | readAheadBlocking()
Reads from the underlying stream until there is chunk data
available in |
| private int | readAheadNonBlocking()
Reads any available bytes from the underlying stream into
|
| chunkCount | back to summary |
|---|---|
| private int chunkCount The index one greater than the index of the last valid byte in the
buffer. This value is always in the range | |
| chunkData | back to summary |
|---|---|
| private byte[] chunkData The internal buffer array where chunk data is available for the application to read. | |
| chunkPos | back to summary |
|---|---|
| private int chunkPos The current position in the buffer. It contains the index
of the next byte to read from | |
| chunkRead | back to summary |
|---|---|
| private int chunkRead The number of bytes read from the underlying stream for the current
chunk. This value is always in the range | |
| chunkSize | back to summary |
|---|---|
| private int chunkSize The size, in bytes, of the chunk that is currently being read. This size is only valid if the current position in the underlying input stream is inside a chunk (ie: state == STATE_READING_CHUNK). | |
| closed | back to summary |
|---|---|
| private boolean closed Indicates if the chunked stream has been closed using the
| |
| error | back to summary |
|---|---|
| private boolean error Indicates if an error was encountered when processing the chunked stream. | |
| hc | back to summary |
|---|---|
| private final HttpClient hc The | |
| in | back to summary |
|---|---|
| private InputStream in The underlying stream | |
| MAX_CHUNK_HEADER_SIZE | back to summary |
|---|---|
| private static final int MAX_CHUNK_HEADER_SIZE | |
| rawCount | back to summary |
|---|---|
| private int rawCount The index one greater than the index of the last valid byte in the
buffer. This value is always in the range | |
| rawData | back to summary |
|---|---|
| private byte[] rawData The internal buffer where bytes from the underlying stream can be read. It may contain bytes representing chunk-size, chunk-data, or trailer fields. | |
| rawPos | back to summary |
|---|---|
| private int rawPos The current position in the buffer. It contains the index
of the next byte to read from | |
| readLock | back to summary |
|---|---|
| private final ReentrantLock readLock | |
| responses | back to summary |
|---|---|
| private final MessageHeader responses The | |
| state | back to summary |
|---|---|
| private int state Indicates the current state. | |
| STATE_AWAITING_CHUNK_EOL | back to summary |
|---|---|
| pack-priv static final int STATE_AWAITING_CHUNK_EOL Indicates that a chunk has been completely read and the next fields to be examine should be CRLF | |
| STATE_AWAITING_CHUNK_HEADER | back to summary |
|---|---|
| pack-priv static final int STATE_AWAITING_CHUNK_HEADER State to indicate that next field should be :- chunk-size [ chunk-extension ] CRLF | |
| STATE_AWAITING_TRAILERS | back to summary |
|---|---|
| pack-priv static final int STATE_AWAITING_TRAILERS Indicates that all chunks have been read and the next field should be optional trailers or an indication that the chunked stream is complete. | |
| STATE_DONE | back to summary |
|---|---|
| pack-priv static final int STATE_DONE State to indicate that the chunked stream is complete and no further bytes should be read from the underlying stream. | |
| STATE_READING_CHUNK | back to summary |
|---|---|
| pack-priv static final int STATE_READING_CHUNK State to indicate that we are currently reading the chunk-data. | |
| ChunkedInputStream | back to summary |
|---|---|
| public ChunkedInputStream(InputStream in, HttpClient hc, MessageHeader responses) throws IOException Creates a
| |
| available | back to summary |
|---|---|
| public int available() throws IOException Overrides java. Returns the number of bytes that can be read from this input stream without blocking.
| |
| close | back to summary |
|---|---|
| public void close() throws IOException Overrides java. Implements java. Close the stream by either returning the connection to the keep alive cache or closing the underlying stream. If the chunked response hasn't been completely read we try to "hurry" to the end of the response. If this is possible (without blocking) then the connection can be returned to the keep alive cache.
| |
| closeUnderlying | back to summary |
|---|---|
| private void closeUnderlying() throws IOException Close the underlying input stream by either returning it to the keep alive cache or closing the stream. As a chunked stream is inheritly persistent (see HTTP 1.1 RFC) the underlying stream can be returned to the keep alive cache if the stream can be completely read without error. | |
| ensureOpen | back to summary |
|---|---|
| private void ensureOpen() throws IOException Check to make sure that this stream has not been closed. | |
| ensureRawAvailable | back to summary |
|---|---|
| private void ensureRawAvailable(int size) Ensures there is | |
| fastRead | back to summary |
|---|---|
| private int fastRead(byte[] b, int off, int len) throws IOException Attempt to read the remainder of a chunk directly into the caller's buffer. Return the number of bytes read. | |
| hurry | back to summary |
|---|---|
| public boolean hurry() Implements sun. Hurry the input stream by reading everything from the underlying stream. If the last chunk (and optional trailers) can be read without blocking then the stream is considered hurried. Note that if an error has occurred or we can't get to last chunk without blocking then this stream can't be hurried and should be closed. | |
| processRaw | back to summary |
|---|---|
| private void processRaw() throws IOException Process any outstanding bytes that have already been read into
The parsing of the chunked stream is performed as a state machine with
Returns when either all the outstanding bytes in rawData have been
processed or there is insufficient bytes available to continue
processing. When the latter occurs | |
| read | back to summary |
|---|---|
| public int read() throws IOException Implements abstract java. See
the general contract of the
| |
| read | back to summary |
|---|---|
| public int read(byte[] b, int off, int len) throws IOException Overrides java. Reads bytes from this stream into the specified byte array, starting at the given offset.
| |
| readAhead | back to summary |
|---|---|
| private int readAhead(boolean allowBlocking) throws IOException Read ahead in either blocking or non-blocking mode. This method
is typically used when we run out of available bytes in
| |
| readAheadBlocking | back to summary |
|---|---|
| private int readAheadBlocking() throws IOException Reads from the underlying stream until there is chunk data
available in | |
| readAheadNonBlocking | back to summary |
|---|---|
| private int readAheadNonBlocking() throws IOException Reads any available bytes from the underlying stream into
| |