FileDocCategorySizeDatePackage
ContentLengthInputStream.javaAPI DocAndroid 1.5 API7778Wed May 06 22:41:10 BST 2009org.apache.http.impl.io

ContentLengthInputStream

public 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.

author
Ortwin Glueck
author
Eric Johnson
author
Mike Bowler
since
4.0

Fields Summary
private static final int
BUFFER_SIZE
private long
contentLength
The maximum number of bytes that can be read from the stream. Subsequent read operations will return -1.
private long
pos
The current position
private boolean
closed
True if the stream is closed.
private SessionInputBuffer
in
Wrapped input stream that all calls are delegated to.
Constructors Summary
public ContentLengthInputStream(SessionInputBuffer in, long contentLength)
Creates a new length limited stream

param
in The session input buffer to wrap
param
contentLength The maximum number of bytes that can be read from the stream. Subsequent read operations will return -1.


                                           
          
        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 voidclose()

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.

throws
IOException If an IO problem occurs.

        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 intread()
Read the next byte from the stream

return
The next byte or -1 if the end of stream has been reached.
throws
IOException If an IO problem occurs
see
java.io.InputStream#read()

        if (closed) {
            throw new IOException("Attempted read from closed stream.");
        }

        if (pos >= contentLength) {
            return -1;
        }
        pos++;
        return this.in.read();
    
public intread(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.

param
b The byte array to fill.
param
off Start filling at this position.
param
len The number of bytes to attempt to read.
return
The number of bytes read, or -1 if the end of content has been reached.
throws
java.io.IOException Should an error occur on the wrapped stream.

        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 intread(byte[] b)
Read more bytes from the stream.

param
b The byte array to put the new data in.
return
The number of bytes read into the buffer.
throws
IOException If an IO problem occurs
see
java.io.InputStream#read(byte[])

        return read(b, 0, b.length);
    
public longskip(long n)
Skips and discards a number of bytes from the input stream.

param
n The number of bytes to skip.
return
The actual number of bytes skipped. <= 0 if no bytes are skipped.
throws
IOException If an error occurs while skipping bytes.
see
InputStream#skip(long)

        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;