Top Description Inners Fields Constructors Methods
org.apache.hc.core5.http.impl.io

public Class ChunkedInputStream

extends InputStream
Class Inheritance
Imports
java.io.IOException, .InputStream, org.apache.hc.core5.http.ConnectionClosedException, .Header, .HttpException, .MalformedChunkCodingException, .StreamClosedException, .TruncatedChunkException, org.apache.hc.core5.http.config.Http1Config, org.apache.hc.core5.http.io.SessionInputBuffer, org.apache.hc.core5.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

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static enum

Field Summary

Modifier and TypeField and Description
private final SessionInputBuffer
buffer

The session input buffer

private static final int
private long
chunkSize

The chunk size

private boolean
closed

True if this stream is closed

private static final Header[]
private boolean
eof

True if we've reached the end of stream

private Header[]
private final Http1Config
private final InputStream
private final CharArrayBuffer
private long
pos

The current position within the current chunk

private ChunkedInputStream.State

Constructor Summary

AccessConstructor and Description
public
ChunkedInputStream(final SessionInputBuffer
Session input buffer
buffer
,
final InputStream
Input stream
inputStream
,
final Http1Config
Message http1Config. If null Http1Config#DEFAULT will be used.
http1Config
)

Default constructor.

public
ChunkedInputStream(final SessionInputBuffer
Session input buffer
buffer
,
final InputStream
Input stream
inputStream
)

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.

Reads the remainder of the chunked message, leaving the underlying stream 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 SessionInputBuffer buffer

The session input buffer

BUFFER_SIZEback to summary
private static final int BUFFER_SIZE
chunkSizeback to summary
private long chunkSize

The chunk size

closedback to summary
private boolean closed

True if this stream is closed

EMPTY_FOOTERSback to summary
private static final Header[] EMPTY_FOOTERS
eofback to summary
private boolean eof

True if we've reached the end of stream

footersback to summary
private Header[] footers
http1Configback to summary
private final Http1Config http1Config
inputStreamback to summary
private final InputStream inputStream
lineBufferback to summary
private final CharArrayBuffer lineBuffer
posback to summary
private long pos

The current position within the current chunk

stateback to summary
private ChunkedInputStream.State state

Constructor Detail

ChunkedInputStreamback to summary
public ChunkedInputStream(final SessionInputBuffer buffer, final InputStream inputStream, final Http1Config http1Config)

Default constructor.

Parameters
buffer:SessionInputBuffer

Session input buffer

inputStream:InputStream

Input stream

http1Config:Http1Config

Message http1Config. If null Http1Config#DEFAULT will be used.

Since
4.4
ChunkedInputStreamback to summary
public ChunkedInputStream(final SessionInputBuffer buffer, final InputStream inputStream)

Wraps session input stream and reads chunk coded input.

Parameters
buffer:SessionInputBuffer

Session input buffer

inputStream:InputStream

Input stream

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.

Reads the remainder of the chunked message, leaving the underlying stream at a position to start reading the next response without scanning. But does NOT close the underlying stream.

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
org.apache.hc.core5.http.impl.io back to summary

private final Enum ChunkedInputStream.State

extends Enum<ChunkedInputStream.State>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final ChunkedInputStream.State
public static final ChunkedInputStream.State
public static final ChunkedInputStream.State
public static final ChunkedInputStream.State

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static ChunkedInputStream.State
public static ChunkedInputStream.State[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

CHUNK_CRLFback to summary
public static final ChunkedInputStream.State CHUNK_CRLF
CHUNK_DATAback to summary
public static final ChunkedInputStream.State CHUNK_DATA
CHUNK_INVALIDback to summary
public static final ChunkedInputStream.State CHUNK_INVALID
CHUNK_LENback to summary
public static final ChunkedInputStream.State CHUNK_LEN

Constructor Detail

Stateback to summary
private State()

Method Detail

valueOfback to summary
public static ChunkedInputStream.State valueOf(String name)
valuesback to summary
public static ChunkedInputStream.State[] values()