APIdia Launch Partner
Top Description Fields Constructors Methods
sun.net.www.http

public Class ChunkedInputStream

extends InputStream
implements Hurryable
Class Inheritance
All Implemented Interfaces
sun.net.www.http.Hurryable
Imports
java.io.*, java.util.concurrent.locks.ReentrantLock, sun.net.www.*, sun.nio.cs.US_ASCII

A 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.

Field Summary

Modifier and TypeField 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 close method.

private boolean
error

Indicates if an error was encountered when processing the chunked stream.

private final HttpClient
hc

The HttpClient that should be notified when the chunked stream has completed.

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 MessageHeader that is populated with any optional trailer that appear after the last chunk.

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.

Constructor Summary

AccessConstructor 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 ChunkedInputStream and saves its arguments, for later use.

Method Summary

Modifier and TypeMethod and Description
public int

Returns:

the number of bytes that can be read from this input stream without blocking.
available
()

Overrides java.io.InputStream.available.

Returns the number of bytes that can be read from this input stream without blocking.

public void
close()

Overrides java.io.InputStream.close.

Implements java.io.Closeable.close.

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
ensureOpen()

Check to make sure that this stream has not been closed.

private void
ensureRawAvailable(int size)

Ensures there is size bytes available in rawData.

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.net.www.http.Hurryable.hurry.

Hurry the input stream by reading everything from the underlying stream.

private void
processRaw()

Process any outstanding bytes that have already been read into rawData.

public int

Returns:

the next byte of data, or -1 if the end of the stream is reached.
read
()

Implements abstract java.io.InputStream.read.

See the general contract of the read method of InputStream.

public int

Returns:

the number of bytes read, or -1 if the end of the stream has been reached.
read
(byte[]
destination buffer.
b
,
int
offset at which to start storing bytes.
off
,
int
maximum number of bytes to read.
len
)

Overrides java.io.InputStream.read.

Reads bytes from this stream into the specified byte array, starting at the given offset.

private int
readAhead(boolean allowBlocking)

Read ahead in either blocking or non-blocking mode.

private int
readAheadBlocking()

Reads from the underlying stream until there is chunk data available in chunkData for the application to read.

private int
readAheadNonBlocking()

Reads any available bytes from the underlying stream into rawData and returns the number of bytes of chunk data available in chunkData that the application can read.

Inherited from java.io.InputStream:
markmarkSupportednullInputStreamreadreadAllBytesreadNBytesreadNBytesresetskipskipNBytestransferTo

Field Detail

chunkCountback 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 0 through chunkData.length.

chunkDataback to summary
private byte[] chunkData

The internal buffer array where chunk data is available for the application to read.

chunkPosback to summary
private int chunkPos

The current position in the buffer. It contains the index of the next byte to read from chunkData

chunkReadback 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 0 through to chunkSize

chunkSizeback 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).

closedback to summary
private boolean closed

Indicates if the chunked stream has been closed using the close method.

errorback to summary
private boolean error

Indicates if an error was encountered when processing the chunked stream.

hcback to summary
private final HttpClient hc

The HttpClient that should be notified when the chunked stream has completed.

inback to summary
private InputStream in

The underlying stream

MAX_CHUNK_HEADER_SIZEback to summary
private static final int MAX_CHUNK_HEADER_SIZE
rawCountback 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 0 through rawData.length.

rawDataback 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.

rawPosback to summary
private int rawPos

The current position in the buffer. It contains the index of the next byte to read from rawData

readLockback to summary
private final ReentrantLock readLock
responsesback to summary
private final MessageHeader responses

The MessageHeader that is populated with any optional trailer that appear after the last chunk.

stateback to summary
private int state

Indicates the current state.

STATE_AWAITING_CHUNK_EOLback 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_HEADERback 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_TRAILERSback 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_DONEback 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_CHUNKback to summary
pack-priv static final int STATE_READING_CHUNK

State to indicate that we are currently reading the chunk-data.

Constructor Detail

ChunkedInputStreamback to summary
public ChunkedInputStream(InputStream in, HttpClient hc, MessageHeader responses) throws IOException

Creates a ChunkedInputStream and saves its arguments, for later use.

Parameters
in:InputStream

the underlying input stream.

hc:HttpClient

the HttpClient

responses:MessageHeader

the MessageHeader that should be populated with optional trailers.

Method Detail

availableback to summary
public int available() throws IOException

Overrides java.io.InputStream.available.

Returns the number of bytes that can be read from this input stream without blocking.

Returns:int

the number of bytes that can be read from this input stream without blocking.

Exceptions
IOException:
if an I/O error occurs.
See Also
java.io.FilterInputStream#in
closeback to summary
public void close() throws IOException

Overrides java.io.InputStream.close.

Implements java.io.Closeable.close.

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.

Exceptions
IOException:
if an I/O error occurs.
closeUnderlyingback 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.

ensureOpenback to summary
private void ensureOpen() throws IOException

Check to make sure that this stream has not been closed.

ensureRawAvailableback to summary
private void ensureRawAvailable(int size)

Ensures there is size bytes available in rawData. This requires that we either shift the bytes in use to the beginning of the buffer or allocate a large buffer with sufficient space available.

fastReadback 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.

hurryback to summary
public boolean hurry()

Implements sun.net.www.http.Hurryable.hurry.

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.

processRawback to summary
private void processRaw() throws IOException

Process any outstanding bytes that have already been read into rawData.

The parsing of the chunked stream is performed as a state machine with state representing the current state of the processing.

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 rawPos will not have been updated and thus the processing can be restarted once further bytes have been read into rawData.

readback to summary
public int read() throws IOException

Implements abstract java.io.InputStream.read.

See the general contract of the read method of InputStream.

Returns:int

the next byte of data, or -1 if the end of the stream is reached.

Exceptions
IOException:
if an I/O error occurs.
See Also
java.io.FilterInputStream#in
readback to summary
public int read(byte[] b, int off, int len) throws IOException

Overrides java.io.InputStream.read.

Reads bytes from this stream into the specified byte array, starting at the given offset.

Parameters
b:byte[]

destination buffer.

off:int

offset at which to start storing bytes.

len:int

maximum number of bytes to read.

Returns:int

the number of bytes read, or -1 if the end of the stream has been reached.

Exceptions
IOException:
if an I/O error occurs.
readAheadback 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 chunkData or we need to determine how many bytes are available on the input stream.

readAheadBlockingback to summary
private int readAheadBlocking() throws IOException

Reads from the underlying stream until there is chunk data available in chunkData for the application to read.

readAheadNonBlockingback to summary
private int readAheadNonBlocking() throws IOException

Reads any available bytes from the underlying stream into rawData and returns the number of bytes of chunk data available in chunkData that the application can read.