Home » HttpComponents-Core-4.0.1 » org.apache.http.io » [javadoc | source]
public class: ContentLengthInputStream [javadoc | source]

All Implemented Interfaces:

This class cuts the wrapped InputStream 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 invocation of subsequent HTTP 1.1 calls, 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 InputStream#mark 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 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 InputStream#mark and InputStream#reset . That choice has the added benefit of keeping this class very simple.

 public ContentLengthInputStream(HttpDataReceiver in,
    long contentLength) 
    Creates a new length limited stream
    in - The stream to wrap
    contentLength - The maximum number of bytes that can be read from the stream. Subsequent read operations will return -1.
    since: 3.0 -
Method from org.apache.http.io.ContentLengthInputStream Summary:
close,   read,   read,   read,   skip
Methods from java.io.InputStream:
available,   close,   mark,   markSupported,   read,   read,   read,   reset,   skip
Methods from java.lang.Object:
clone,   equals,   finalize,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from org.apache.http.io.ContentLengthInputStream Detail:
 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.

 public int read() throws IOException 
    Read the next byte from the stream
 public int read(byte[] b) throws IOException 
    Read more bytes from the stream.
 public int read(byte[] b,
    int off,
    int len) throws IOException 
 public long skip(long n) throws IOException 
    Skips and discards a number of bytes from the input stream.