Top Description Fields Constructors Methods
org.apache.http.impl.io

public Class ChunkedInputStream

extends InputStream
Class Inheritance
Imports
java.io.IOException, .InputStream, org.apache.http.ConnectionClosedException, .Header, .HttpException, .MalformedChunkCodingException, .TruncatedChunkException, org.apache.http.config.MessageConstraints, org.apache.http.io.BufferInfo, .SessionInputBuffer, org.apache.http.util.Args, .CharArrayBuffer

Implements chunked transfer coding. The content is received in small chunks. Entities transferred using this input stream can be of unlimited length. After the stream is read to the end, it provides access to the trailers, if any.

Note that this class NEVER closes the underlying stream, even when close gets called. Instead, it will read until the "end" of its chunking on close, which allows for the seamless execution of subsequent HTTP 1.1 requests, while not requiring the client to remember to read the entire contents of the response.

Since
4.0

Field Summary

Modifier and TypeField and Description
private final CharArrayBuffer
private static final int
private static final int
private static final int
private static final int
private static final int
private long
chunkSize

The chunk size

private boolean
closed

True if this stream is closed

private final MessageConstraints
private boolean
eof

True if we've reached the end of stream

private Header[]
private final SessionInputBuffer
in

The session input buffer

private long
pos

The current position within the current chunk

private int

Constructor Summary

AccessConstructor and Description
public
ChunkedInputStream(final SessionInputBuffer
The session input buffer
in
,
final MessageConstraints
Message constraints. If null MessageConstraints#DEFAULT will be used.
constraints
)

Wraps session input stream and reads chunk coded input.

public
ChunkedInputStream(final SessionInputBuffer
The session input buffer
in
)

Wraps session input stream and reads chunk coded input.

Method Summary

Modifier and TypeMethod and Description
public int
available()

Overrides java.io.InputStream.available.

Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking, which may be 0, or 0 when end of stream is detected.
public void
close()

Overrides java.io.InputStream.close.

Implements java.io.Closeable.close.

Upon close, this reads the remainder of the chunked message, leaving the underlying socket at a position to start reading the next response without scanning.
private long
getChunkSize()

Expects the stream to start with a chunksize in hex with optional comments after a semicolon.

public Header[]
private void
nextChunk()

Read the next chunk.

private void
parseTrailerHeaders()

Reads and stores the Trailer headers.

public int

Returns:

-1 of the end of the stream has been reached or the next data byte
read
()

Implements abstract java.io.InputStream.read.

Returns all the data in a chunked stream in coalesced form.

public int

Returns:

The number of bytes returned or -1 if the end of stream has been reached.
read
(final byte[]
The byte array that will hold the contents from the stream.
b
,
final int
The offset into the byte array at which bytes will start to be placed.
off
,
final int
the maximum number of bytes that can be returned.
len
)

Overrides java.io.InputStream.read.

Read some bytes from the stream.
public int

Returns:

The number of bytes returned or -1 if the end of stream has been reached.
read
(final byte[]
The byte array that will hold the contents from the stream.
b
)

Overrides java.io.InputStream.read.

Read some bytes from the stream.
Inherited from java.io.InputStream:
markmarkSupportednullInputStreamreadAllBytesreadNBytesreadNBytesresetskipskipNBytestransferTo

Field Detail

bufferback to summary
private final CharArrayBuffer buffer
BUFFER_SIZEback to summary
private static final int BUFFER_SIZE
CHUNK_CRLFback to summary
private static final int CHUNK_CRLF
CHUNK_DATAback to summary
private static final int CHUNK_DATA
CHUNK_INVALIDback to summary
private static final int CHUNK_INVALID
CHUNK_LENback to summary
private static final int CHUNK_LEN
chunkSizeback to summary
private long chunkSize

The chunk size

closedback to summary
private boolean closed

True if this stream is closed

constraintsback to summary
private final MessageConstraints constraints
eofback to summary
private boolean eof

True if we've reached the end of stream

footersback to summary
private Header[] footers
inback to summary
private final SessionInputBuffer in

The session input buffer

posback to summary
private long pos

The current position within the current chunk

stateback to summary
private int state

Constructor Detail

ChunkedInputStreamback to summary
public ChunkedInputStream(final SessionInputBuffer in, final MessageConstraints constraints)

Wraps session input stream and reads chunk coded input.

Parameters
in:SessionInputBuffer

The session input buffer

constraints:MessageConstraints

Message constraints. If null MessageConstraints#DEFAULT will be used.

Since
4.4
ChunkedInputStreamback to summary
public ChunkedInputStream(final SessionInputBuffer in)

Wraps session input stream and reads chunk coded input.

Parameters
in:SessionInputBuffer

The session input buffer

Method Detail

availableback to summary
public int available() throws IOException

Overrides java.io.InputStream.available.

Doc from java.io.InputStream.available.

Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking, which may be 0, or 0 when end of stream is detected. The read might be on the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes.

Note that while some implementations of InputStream will return the total number of bytes in the stream, many will not. It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream.

A subclass's implementation of this method may choose to throw an IOException if this input stream has been closed by invoking the close() method.

Returns:int

an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking or 0 when it reaches the end of the input stream.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
closeback to summary
public void close() throws IOException

Overrides java.io.InputStream.close.

Implements java.io.Closeable.close.

Upon close, this reads the remainder of the chunked message, leaving the underlying socket at a position to start reading the next response without scanning.

Annotations
@Override
Exceptions
IOException:
in case of an I/O error
getChunkSizeback to summary
private long getChunkSize() throws IOException

Expects the stream to start with a chunksize in hex with optional comments after a semicolon. The line must end with a CRLF: "a3; some comment\r\n" Positions the stream at the start of the next line.

getFootersback to summary
public Header[] getFooters()
nextChunkback to summary
private void nextChunk() throws IOException

Read the next chunk.

Exceptions
IOException:
in case of an I/O error
parseTrailerHeadersback to summary
private void parseTrailerHeaders() throws IOException

Reads and stores the Trailer headers.

Exceptions
IOException:
in case of an I/O error
readback to summary
public int read() throws IOException

Implements abstract java.io.InputStream.read.

Returns all the data in a chunked stream in coalesced form. A chunk is followed by a CRLF. The method returns -1 as soon as a chunksize of 0 is detected.

Trailer headers are read automatically at the end of the stream and can be obtained with the getResponseFooters() method.

Returns:int

-1 of the end of the stream has been reached or the next data byte

Annotations
@Override
Exceptions
IOException:
in case of an I/O error
readback to summary
public int read(final byte[] b, final int off, final int len) throws IOException

Overrides java.io.InputStream.read.

Read some bytes from the stream.

Parameters
b:byte[]

The byte array that will hold the contents from the stream.

off:int

The offset into the byte array at which bytes will start to be placed.

len:int

the maximum number of bytes that can be returned.

Returns:int

The number of bytes returned or -1 if the end of stream has been reached.

Annotations
@Override
Exceptions
IOException:
in case of an I/O error
readback to summary
public int read(final byte[] b) throws IOException

Overrides java.io.InputStream.read.

Read some bytes from the stream.

Parameters
b:byte[]

The byte array that will hold the contents from the stream.

Returns:int

The number of bytes returned or -1 if the end of stream has been reached.

Annotations
@Override
Exceptions
IOException:
in case of an I/O error