Class ContentLengthInputStream
- All Implemented Interfaces:
Closeable,AutoCloseable
public class ContentLengthInputStream extends InputStream
Implementation note: Choices abound. One approach would pass
through the InputStream.mark(int) and InputStream.reset() calls to
the underlying stream. That's tricky, though, because you then have to
start duplicating the work of keeping track of how much a reset rewinds.
Further, you have to watch out for the "readLimit", and since the semantics
for the readLimit leave room for differing implementations, you might get
into a lot of trouble.
Alternatively, you could make this class extend
BufferedInputStream
and then use the protected members of that class to avoid duplicated effort.
That solution has the side effect of adding yet another possible layer of
buffering.
Then, there is the simple choice, which this takes - simply don't
support InputStream.mark(int) and InputStream.reset(). That choice
has the added benefit of keeping this class very simple.
- Since:
- 4.0
- Author:
- Ortwin Glueck, Eric Johnson, Mike Bowler
-
Constructor Summary
Constructors Constructor Description ContentLengthInputStream(SessionInputBuffer in, long contentLength)Creates a new length limited stream -
Method Summary
Modifier and Type Method Description 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.Methods inherited from class java.io.InputStream
available, mark, markSupported, reset
-
Constructor Details
-
ContentLengthInputStream
Creates a new length limited stream- Parameters:
in- The session input buffer to wrapcontentLength- 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 socket input, but instead leaves it primed to parse the next response.
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classInputStream- Throws:
IOException- If an IO problem occurs.
-
read
Read the next byte from the stream- Specified by:
readin classInputStream- Returns:
- The next byte or -1 if the end of stream has been reached.
- Throws:
IOException- If an IO problem occurs- See Also:
InputStream.read()
-
read
Does standardInputStream.read(byte[], int, int)behavior, but also notifies the watcher when the contents have been consumed.- Overrides:
readin 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:
readin 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:
InputStream.read(byte[])
-
skip
Skips and discards a number of bytes from the input stream.- Overrides:
skipin 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:
InputStream.skip(long)
-