ContentLengthInputStreampublic class ContentLengthInputStream extends InputStream Stream that cuts off after a specified number of bytes.
Note that this class NEVER closes the underlying stream, even when close
gets called. Instead, it will read until the "end" of its chunking 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.
Implementation note: Choices abound. One approach would pass
through the {@link InputStream#mark} and {@link 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
{@link java.io.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 {@link InputStream#mark} and {@link InputStream#reset}. That choice
has the added benefit of keeping this class very simple. |
Fields Summary |
---|
private static final int | BUFFER_SIZE | private long | contentLengthThe maximum number of bytes that can be read from the stream. Subsequent
read operations will return -1. | private long | posThe current position | private boolean | closedTrue if the stream is closed. | private SessionInputBuffer | inWrapped input stream that all calls are delegated to. |
Constructors Summary |
---|
public ContentLengthInputStream(SessionInputBuffer in, long contentLength)Creates a new length limited stream
super();
if (in == null) {
throw new IllegalArgumentException("Input stream may not be null");
}
if (contentLength < 0) {
throw new IllegalArgumentException("Content length may not be negative");
}
this.in = in;
this.contentLength = contentLength;
|
Methods Summary |
---|
public void | 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.
if (!closed) {
try {
byte buffer[] = new byte[BUFFER_SIZE];
while (read(buffer) >= 0) {
}
} finally {
// close after above so that we don't throw an exception trying
// to read after closed!
closed = true;
}
}
| public int | read()Read the next byte from the stream
if (closed) {
throw new IOException("Attempted read from closed stream.");
}
if (pos >= contentLength) {
return -1;
}
pos++;
return this.in.read();
| public int | read(byte[] b, int off, int len)Does standard {@link InputStream#read(byte[], int, int)} behavior, but
also notifies the watcher when the contents have been consumed.
if (closed) {
throw new IOException("Attempted read from closed stream.");
}
if (pos >= contentLength) {
return -1;
}
if (pos + len > contentLength) {
len = (int) (contentLength - pos);
}
int count = this.in.read(b, off, len);
pos += count;
return count;
| public int | read(byte[] b)Read more bytes from the stream.
return read(b, 0, b.length);
| public long | skip(long n)Skips and discards a number of bytes from the input stream.
if (n <= 0) {
return 0;
}
byte[] buffer = new byte[BUFFER_SIZE];
// make sure we don't skip more bytes than are
// still available
long remaining = Math.min(n, this.contentLength - this.pos);
// skip and keep track of the bytes actually skipped
long count = 0;
while (remaining > 0) {
int l = read(buffer, 0, (int)Math.min(BUFFER_SIZE, remaining));
if (l == -1) {
break;
}
count += l;
remaining -= l;
}
this.pos += count;
return count;
|
|