Top Description Fields Constructors Methods
org.apache.hc.core5.http.impl.io

public Class ContentLengthInputStream

extends InputStream
Class Inheritance
Imports
java.io.IOException, .InputStream, org.apache.hc.core5.http.ConnectionClosedException, .StreamClosedException, org.apache.hc.core5.http.io.SessionInputBuffer, org.apache.hc.core5.util.Args

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

Modifier and TypeField and Description
private final SessionInputBuffer
private static final int
private boolean
closed

True if the stream is closed.

private final long
contentLength

The maximum number of bytes that can be read from the stream.

private final InputStream
private long
pos

The current position

Constructor Summary

AccessConstructor and Description
public
ContentLengthInputStream(final SessionInputBuffer
Session input buffer
buffer
,
final InputStream
Input stream
inputStream
,
final long
The maximum number of bytes that can be read from the stream. Subsequent read operations will return -1.
contentLength
)

Default constructor.

Method Summary

Modifier and TypeMethod and Description
public int
available()

Overrides java.io.InputStream.available.

Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking, which may be 0, or 0 when end of stream is detected.
public void
close()

Overrides java.io.InputStream.close.

Implements java.io.Closeable.close.

Reads until the end of the known length of content.

public int

Returns:

The next byte or -1 if the end of stream has been reached.
read
()

Implements abstract java.io.InputStream.read.

Read the next byte from the stream
public int

Returns:

The number of bytes read, or -1 if the end of content has been reached.
read
(final byte[]
The byte array to fill.
b
,
final int
Start filling at this position.
off
,
final int
The number of bytes to attempt to read.
len
)

Overrides java.io.InputStream.read.

Does standard InputStream#read(byte[], int, int) behavior, but also notifies the watcher when the contents have been consumed.
public int

Returns:

The number of bytes read into the buffer.
read
(final byte[]
The byte array to put the new data in.
b
)

Overrides java.io.InputStream.read.

Read more bytes from the stream.
public long

Returns:

The actual number of bytes skipped. ≤ 0 if no bytes are skipped.
skip
(final long
The number of bytes to skip.
n
)

Overrides java.io.InputStream.skip.

Skips and discards a number of bytes from the input stream.
Inherited from java.io.InputStream:
markmarkSupportednullInputStreamreadAllBytesreadNBytesreadNBytesresetskipNBytestransferTo

Field Detail

bufferback to summary
private final SessionInputBuffer buffer
BUFFER_SIZEback to summary
private static final int BUFFER_SIZE
closedback to summary
private boolean closed

True if the stream is closed.

contentLengthback to summary
private final long contentLength

The maximum number of bytes that can be read from the stream. Subsequent read operations will return -1.

inputStreamback to summary
private final InputStream inputStream
posback to summary
private long pos

The current position

Constructor Detail

ContentLengthInputStreamback to summary
public ContentLengthInputStream(final SessionInputBuffer buffer, final InputStream inputStream, final long contentLength)

Default constructor.

Parameters
buffer:SessionInputBuffer

Session input buffer

inputStream:InputStream

Input stream

contentLength:long

The maximum number of bytes that can be read from the stream. Subsequent read operations will return -1.

Method Detail

availableback to summary
public int available() throws IOException

Overrides java.io.InputStream.available.

Doc from java.io.InputStream.available.

Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking, which may be 0, or 0 when end of stream is detected. The read might be on the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes.

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's implementation of this method may choose to throw an IOException if this input stream has been closed by invoking the close() method.

Returns:int

an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking or 0 when it reaches the end of the input stream.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
closeback to summary
public void close() throws IOException

Overrides java.io.InputStream.close.

Implements java.io.Closeable.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.

Annotations
@Override
Exceptions
IOException:
If an IO problem occurs.
readback to summary
public int read() throws IOException

Implements abstract java.io.InputStream.read.

Read the next byte from the stream

Returns:int

The next byte or -1 if the end of stream has been reached.

Annotations
@Override
Exceptions
IOException:
If an IO problem occurs
See Also
java.io.InputStream#read()
readback to summary
public int read(final byte[] b, final int off, final int len) throws IOException

Overrides java.io.InputStream.read.

Does standard InputStream#read(byte[], int, int) behavior, but also notifies the watcher when the contents have been consumed.

Parameters
b:byte[]

The byte array to fill.

off:int

Start filling at this position.

len:int

The number of bytes to attempt to read.

Returns:int

The number of bytes read, or -1 if the end of content has been reached.

Annotations
@Override
Exceptions
IOException:
Should an error occur on the wrapped stream.
readback to summary
public int read(final byte[] b) throws IOException

Overrides java.io.InputStream.read.

Read more bytes from the stream.

Parameters
b:byte[]

The byte array to put the new data in.

Returns:int

The number of bytes read into the buffer.

Annotations
@Override
Exceptions
IOException:
If an IO problem occurs
See Also
java.io.InputStream#read(byte[])
skipback to summary
public long skip(final long n) throws IOException

Overrides java.io.InputStream.skip.

Skips and discards a number of bytes from the input stream.

Parameters
n:long

The number of bytes to skip.

Returns:long

The actual number of bytes skipped. ≤ 0 if no bytes are skipped.

Annotations
@Override
Exceptions
IOException:
If an error occurs while skipping bytes.
See Also
InputStream#skip(long)