Package org.apache.http.impl.io
Class ContentLengthInputStream
- java.lang.Object
-
- java.io.InputStream
-
- org.apache.http.impl.io.ContentLengthInputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public class ContentLengthInputStream extends java.io.InputStreamInput stream that cuts off after a defined number of bytes. This class is used to receive content of HTTP messages where the end of the content entity is determined by the value of theContent-Length header. Entities transferred using this stream can be maximumLong.MAX_VALUElong.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.
- Since:
- 4.0
-
-
Constructor Summary
Constructors Constructor Description ContentLengthInputStream(SessionInputBuffer in, long contentLength)Wraps a session input buffer and cuts off output after a defined number of bytes.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intavailable()voidclose()Reads until the end of the known length of content.intread()Read the next byte from the streamintread(byte[] b)Read more bytes from the stream.intread(byte[] b, int off, int len)Does standardInputStream.read(byte[], int, int)behavior, but also notifies the watcher when the contents have been consumed.longskip(long n)Skips and discards a number of bytes from the input stream.
-
-
-
Constructor Detail
-
ContentLengthInputStream
public ContentLengthInputStream(SessionInputBuffer in, long contentLength)
Wraps a session input buffer and cuts off output after a defined number of bytes.- Parameters:
in- The session input buffercontentLength- The maximum number of bytes that can be read from the stream. Subsequent read operations will return -1.
-
-
Method Detail
-
close
public void close() throws java.io.IOExceptionReads 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.
- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classjava.io.InputStream- Throws:
java.io.IOException- If an IO problem occurs.
-
available
public int available() throws java.io.IOException- Overrides:
availablein classjava.io.InputStream- Throws:
java.io.IOException
-
read
public int read() throws java.io.IOExceptionRead the next byte from the stream- Specified by:
readin classjava.io.InputStream- Returns:
- The next byte or -1 if the end of stream has been reached.
- Throws:
java.io.IOException- If an IO problem occurs- See Also:
InputStream.read()
-
read
public int read(byte[] b, int off, int len) throws java.io.IOExceptionDoes standardInputStream.read(byte[], int, int)behavior, but also notifies the watcher when the contents have been consumed.- Overrides:
readin classjava.io.InputStream- Parameters:
b- The byte array to fill.off- Start filling at this position.len- The number of bytes to attempt to read.- Returns:
- The number of bytes read, or -1 if the end of content has been reached.
- Throws:
java.io.IOException- Should an error occur on the wrapped stream.
-
read
public int read(byte[] b) throws java.io.IOExceptionRead more bytes from the stream.- Overrides:
readin classjava.io.InputStream- Parameters:
b- The byte array to put the new data in.- Returns:
- The number of bytes read into the buffer.
- Throws:
java.io.IOException- If an IO problem occurs- See Also:
InputStream.read(byte[])
-
skip
public long skip(long n) throws java.io.IOExceptionSkips and discards a number of bytes from the input stream.- Overrides:
skipin classjava.io.InputStream- Parameters:
n- The number of bytes to skip.- Returns:
- The actual number of bytes skipped. ≤ 0 if no bytes are skipped.
- Throws:
java.io.IOException- If an error occurs while skipping bytes.- See Also:
InputStream.skip(long)
-
-