public class ChunkedInputStream extends InputStream
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.
Modifier and Type | Field and Description |
---|---|
private CharArrayBuffer |
buffer |
private static int |
BUFFER_SIZE |
private static int |
CHUNK_CRLF |
private static int |
CHUNK_DATA |
private static int |
CHUNK_INVALID |
private static int |
CHUNK_LEN |
private long |
chunkSize
The chunk size
|
private boolean |
closed
True if this stream is closed
|
private MessageConstraints |
constraints |
private boolean |
eof
True if we've reached the end of stream
|
private Header[] |
footers |
private SessionInputBuffer |
in
The session input buffer
|
private long |
pos
The current position within the current chunk
|
private int |
state |
Constructor and Description |
---|
ChunkedInputStream(SessionInputBuffer in)
Wraps session input stream and reads chunk coded input.
|
ChunkedInputStream(SessionInputBuffer in,
MessageConstraints constraints)
Wraps session input stream and reads chunk coded input.
|
Modifier and Type | Method and Description |
---|---|
int |
available() |
void |
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.
|
Header[] |
getFooters() |
private void |
nextChunk()
Read the next chunk.
|
private void |
parseTrailerHeaders()
Reads and stores the Trailer headers.
|
int |
read()
Returns all the data in a chunked stream in coalesced form.
|
int |
read(byte[] b)
Read some bytes from the stream.
|
int |
read(byte[] b,
int off,
int len)
Read some bytes from the stream.
|
mark, markSupported, reset, skip
private static final int CHUNK_LEN
private static final int CHUNK_DATA
private static final int CHUNK_CRLF
private static final int CHUNK_INVALID
private static final int BUFFER_SIZE
private final SessionInputBuffer in
private final CharArrayBuffer buffer
private final MessageConstraints constraints
private int state
private long chunkSize
private long pos
private boolean eof
private boolean closed
public ChunkedInputStream(SessionInputBuffer in, MessageConstraints constraints)
in
- The session input bufferconstraints
- Message constraints. If null
MessageConstraints.DEFAULT
will be used.public ChunkedInputStream(SessionInputBuffer in)
in
- The session input bufferpublic int available() throws IOException
available
in class InputStream
IOException
public int read() throws IOException
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.
read
in class InputStream
IOException
- in case of an I/O errorpublic int read(byte[] b, int off, int len) throws IOException
read
in class InputStream
b
- The byte array that will hold the contents from the stream.off
- The offset into the byte array at which bytes will start to be
placed.len
- the maximum number of bytes that can be returned.IOException
- in case of an I/O errorpublic int read(byte[] b) throws IOException
read
in class InputStream
b
- The byte array that will hold the contents from the stream.IOException
- in case of an I/O errorprivate void nextChunk() throws IOException
IOException
- in case of an I/O errorprivate long getChunkSize() throws IOException
IOException
private void parseTrailerHeaders() throws IOException
IOException
- in case of an I/O errorpublic void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
close
in class InputStream
IOException
- in case of an I/O errorpublic Header[] getFooters()
WebARTS Library Licensed Under the GNU - General Public License. Other Libraries licensed under their respective Open Source Licenses