@Deprecated 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.
Constructor and Description |
---|
ContentLengthInputStream(InputStream in,
int contentLength)
Deprecated.
use
ContentLengthInputStream(InputStream, long)
Creates a new length limited stream |
ContentLengthInputStream(InputStream in,
long contentLength)
Deprecated.
Creates a new length limited stream
|
Modifier and Type | Method and Description |
---|---|
int |
available()
Deprecated.
|
void |
close()
Deprecated.
Reads until the end of the known length of content.
|
int |
read()
Deprecated.
Read the next byte from the stream
|
int |
read(byte[] b)
Deprecated.
Read more bytes from the stream.
|
int |
read(byte[] b,
int off,
int len)
Deprecated.
Does standard
InputStream.read(byte[], int, int) behavior, but
also notifies the watcher when the contents have been consumed. |
long |
skip(long n)
Deprecated.
Skips and discards a number of bytes from the input stream.
|
mark, markSupported, reset
public ContentLengthInputStream(InputStream in, int contentLength)
ContentLengthInputStream(InputStream, long)
Creates a new length limited streamin
- The stream to wrapcontentLength
- The maximum number of bytes that can be read from
the stream. Subsequent read operations will return -1.public ContentLengthInputStream(InputStream in, long contentLength)
in
- The stream to wrapcontentLength
- The maximum number of bytes that can be read from
the stream. Subsequent read operations will return -1.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, 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.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 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)
public int available() throws IOException
available
in class InputStream
IOException
Copyright © 2004–2017. All rights reserved.