public class ChunkedInputStream
extends java.io.InputStream
Transparently coalesces chunks of a HTTP stream that uses Transfer-Encoding chunked.
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 invocation of subsequent HTTP 1.1 calls, while not requiring the client to remember to read the entire contents of the response.
Modifier and Type | Field and Description |
---|---|
private boolean |
bof
True if we'are at the beginning of stream
|
private int |
chunkSize
The chunk size
|
private boolean |
closed
True if this stream is closed
|
private boolean |
eof
True if we've reached the end of stream
|
private java.io.InputStream |
in
The inputstream that we're wrapping
|
private int |
pos
The current position within the current chunk
|
Constructor and Description |
---|
ChunkedInputStream(java.io.InputStream in)
ChunkedInputStream constructor
|
Modifier and Type | Method and Description |
---|---|
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.
|
(package private) static void |
exhaustInputStream(java.io.InputStream inStream)
Exhaust an input stream, reading until EOF has been encountered.
|
private static java.lang.String |
getAsciiString(byte[] data)
Converts the byte array of ASCII characters to a string.
|
private static int |
getChunkSizeFromInputStream(java.io.InputStream in)
Expects the stream to start with a chunksize in hex with optional
comments after a semicolon.
|
private static java.lang.String |
getString(byte[] data,
int offset,
int length,
java.nio.charset.Charset charset)
Converts the byte array of HTTP content characters to a string.
|
private void |
nextChunk()
Read the next chunk.
|
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.
|
private void |
readCRLF()
Read the CRLF terminator.
|
private static java.lang.String |
readLine(java.io.InputStream inputStream,
java.nio.charset.Charset charset)
Read up to "\n" from an (unchunked) input stream.
|
private static byte[] |
readRawLine(java.io.InputStream inputStream)
Return byte array from an (unchunked) input stream.
|
private void |
skipTrailerHeaders()
Reads and stores the Trailer headers.
|
private final java.io.InputStream in
private int chunkSize
private int pos
private boolean bof
private boolean eof
private boolean closed
public ChunkedInputStream(java.io.InputStream in) throws java.io.IOException
in
- the raw input streamjava.io.IOException
- If an IO error occurspublic int read() throws java.io.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 automcatically at the end of the stream and can be obtained with the getResponseFooters() method.
read
in class java.io.InputStream
java.io.IOException
- If an IO problem occursHttpMethod#getResponseFooters()
public int read(byte[] b, int off, int len) throws java.io.IOException
read
in class java.io.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.java.io.IOException
- if an IO problem occurs.InputStream.read(byte[], int, int)
public int read(byte[] b) throws java.io.IOException
read
in class java.io.InputStream
b
- The byte array that will hold the contents from the stream.java.io.IOException
- if an IO problem occurs.InputStream.read(byte[])
private void readCRLF() throws java.io.IOException
java.io.IOException
- If an IO error occurs.private void nextChunk() throws java.io.IOException
java.io.IOException
- If an IO error occurs.private static int getChunkSizeFromInputStream(java.io.InputStream in) throws java.io.IOException
in
- The new input stream.required
- true if a valid chunk must be present,
false otherwise.java.io.IOException
- when the chunk size could not be parsedprivate static java.lang.String getAsciiString(byte[] data)
data
- the byte array to be encodedprivate void skipTrailerHeaders() throws java.io.IOException
java.io.IOException
- If an IO problem occursprivate static java.lang.String readLine(java.io.InputStream inputStream, java.nio.charset.Charset charset) throws java.io.IOException
null
is returned.inputStream
- the stream to read fromcharset
- charset of HTTP protocol elementsjava.io.IOException
- if an I/O problem occursprivate static java.lang.String getString(byte[] data, int offset, int length, java.nio.charset.Charset charset)
data
- the byte array to be encodedoffset
- the index of the first byte to encodelength
- the number of bytes to encodecharset
- the desired character encodingprivate static byte[] readRawLine(java.io.InputStream inputStream) throws java.io.IOException
null
is returned.inputStream
- the stream to read fromjava.io.IOException
- if an I/O problem occurspublic void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.InputStream
java.io.IOException
- If an IO problem occurs.static void exhaustInputStream(java.io.InputStream inStream) throws java.io.IOException
Note that this function is intended as a non-public utility. This is a little weird, but it seemed silly to make a utility class for this one function, so instead it is just static and shared that way.
inStream
- The InputStream
to exhaust.java.io.IOException
- If an IO problem occurs