Package org.apache.hc.core5.http.impl.io
Class ContentLengthInputStream
java.lang.Object
java.io.InputStream
org.apache.hc.core5.http.impl.io.ContentLengthInputStream
- All Implemented Interfaces:
Closeable
,AutoCloseable
Input 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 the
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.
- Since:
- 4.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final SessionInputBuffer
private static final int
private boolean
True if the stream is closed.private final long
The maximum number of bytes that can be read from the stream.private final InputStream
private long
The current position -
Constructor Summary
ConstructorsConstructorDescriptionContentLengthInputStream
(SessionInputBuffer buffer, InputStream inputStream, long contentLength) Default constructor. -
Method Summary
Modifier and TypeMethodDescriptionint
void
close()
Reads until the end of the known length of content.int
read()
Read the next byte from the streamint
read
(byte[] b) Read more bytes from the stream.int
read
(byte[] b, int off, int len) Does standardInputStream.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.Methods inherited from class java.io.InputStream
mark, markSupported, reset
-
Field Details
-
BUFFER_SIZE
private static final int BUFFER_SIZE- See Also:
-
buffer
-
inputStream
-
contentLength
private final long contentLengthThe maximum number of bytes that can be read from the stream. Subsequent read operations will return -1. -
pos
private long posThe current position -
closed
private boolean closedTrue if the stream is closed.
-
-
Constructor Details
-
ContentLengthInputStream
public ContentLengthInputStream(SessionInputBuffer buffer, InputStream inputStream, long contentLength) Default constructor.- Parameters:
buffer
- Session input bufferinputStream
- Input streamcontentLength
- The maximum number of bytes that can be read from the stream. Subsequent read operations will return -1.
-
-
Method Details
-
close
Reads until the end of the known length of content.
Does NOT close the underlying stream, but instead leaves it primed to parse the next response.
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classInputStream
- Throws:
IOException
- If an IO problem occurs.
-
available
- Overrides:
available
in classInputStream
- Throws:
IOException
-
read
Read the next byte from the stream- Specified by:
read
in classInputStream
- Returns:
- The next byte or -1 if the end of stream has been reached.
- Throws:
IOException
- If an IO problem occurs- See Also:
-
read
Does standardInputStream.read(byte[], int, int)
behavior, but also notifies the watcher when the contents have been consumed.- Overrides:
read
in classInputStream
- 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:
IOException
- Should an error occur on the wrapped stream.
-
read
Read more bytes from the stream.- Overrides:
read
in classInputStream
- Parameters:
b
- The byte array to put the new data in.- Returns:
- The number of bytes read into the buffer.
- Throws:
IOException
- If an IO problem occurs- See Also:
-
skip
Skips and discards a number of bytes from the input stream.- Overrides:
skip
in classInputStream
- Parameters:
n
- The number of bytes to skip.- Returns:
- The actual number of bytes skipped. ≤ 0 if no bytes are skipped.
- Throws:
IOException
- If an error occurs while skipping bytes.- See Also:
-