public class ContentLengthInputStream extends InputStream
Content-Length header
.
Entities transferred using this stream can be maximum Long.MAX_VALUE
long.
Note that this class NEVER closes the underlying stream, even when close gets called. Instead, it will read until the "end" of its limit 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.
Constructor and Description |
---|
ContentLengthInputStream(SessionInputBuffer buffer,
InputStream inputStream,
long contentLength)
Default constructor.
|
Modifier and Type | Method and Description |
---|---|
int |
available()
Returns an estimate of the number of bytes that can be read (or skipped over) from this input
stream without blocking by the next invocation of a method for this input stream.
|
void |
close()
Reads until the end of the known length of content.
|
int |
read()
Read the next byte from the stream
|
int |
read(byte[] b)
Read more bytes from the stream.
|
int |
read(byte[] b,
int off,
int len)
Does standard
InputStream.read(byte[], int, int) behavior, but
also notifies the watcher when the contents have been consumed. |
long |
skip(long n)
Skips and discards a number of bytes from the input stream.
|
mark, markSupported, reset
public ContentLengthInputStream(SessionInputBuffer buffer, InputStream inputStream, long contentLength)
buffer
- Session input bufferinputStream
- Input streamcontentLength
- The maximum number of bytes that can be read from
the stream. Subsequent read operations will return -1.public int available() throws IOException
InputStream
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' implementation of this method may choose to throw an IOException
if this
input stream has been closed by invoking the InputStream.close()
method.
The available
method for class InputStream
always returns 0
.
This method should be overridden by subclasses.
available
in class InputStream
0
when it reaches the end of the input stream.IOException
- if an I/O error occurs.public void close() throws IOException
Reads until the end of the known length of content.
Does not close the underlying socket input, but instead leaves it primed to parse the next response.
close
in interface Closeable
close
in interface AutoCloseable
close
in class InputStream
IOException
- If an IO problem occurs.public int read() throws IOException
read
in class InputStream
IOException
- If an IO problem occursInputStream.read()
public int read(byte[] b) throws IOException
read
in class InputStream
b
- The byte array to put the new data in.IOException
- If an IO problem occursInputStream.read(byte[])
public int read(byte[] b, int off, int len) throws IOException
InputStream.read(byte[], int, int)
behavior, but
also notifies the watcher when the contents have been consumed.read
in class InputStream
b
- The byte array to fill.off
- Start filling at this position.len
- The number of bytes to attempt to read.IOException
- Should an error occur on the wrapped stream.InputStream.read()
public long skip(long n) throws IOException
skip
in class InputStream
n
- The number of bytes to skip.IOException
- If an error occurs while skipping bytes.InputStream.skip(long)