Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

org.apache.http.io
Class ContentLengthInputStream  view ContentLengthInputStream download ContentLengthInputStream.java

java.lang.Object
  extended byjava.io.InputStream
      extended byorg.apache.http.io.ContentLengthInputStream
All Implemented Interfaces:
java.io.Closeable

public class ContentLengthInputStream
extends java.io.InputStream

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(int)>InputStream.mark(int) 55 and InputStream.reset()>InputStream.reset() 55 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(int)>InputStream.mark(int) 55 and InputStream.reset()>InputStream.reset() 55 . That choice has the added benefit of keeping this class very simple.

Since:
2.0

Field Summary
private static int BUFFER_SIZE
           
private  boolean closed
          True if the stream is closed.
private  long contentLength
          The maximum number of bytes that can be read from the stream.
private  HttpDataReceiver in
          Wrapped input stream that all calls are delegated to.
private  long pos
          The current position
 
Constructor Summary
ContentLengthInputStream(HttpDataReceiver in, long contentLength)
          Creates a new length limited stream
 
Method Summary
 void close()
          Reads until the end of the known length of content.
 int read()
          Read the next byte from the stream
 int read(byte[] b)
          Read more bytes from the stream.
 int read(byte[] b, int off, int len)
          Does standard InputStream.read(byte[], int, int)>InputStream.read(byte[], int, int) 55 behavior, but also notifies the watcher when the contents have been consumed.
 long skip(long n)
          Skips and discards a number of bytes from the input stream.
 
Methods inherited from class java.io.InputStream
available, mark, markSupported, reset
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BUFFER_SIZE

private static int BUFFER_SIZE

contentLength

private long contentLength
The maximum number of bytes that can be read from the stream. Subsequent read operations will return -1.


pos

private long pos
The current position


closed

private boolean closed
True if the stream is closed.


in

private HttpDataReceiver in
Wrapped input stream that all calls are delegated to.

Constructor Detail

ContentLengthInputStream

public ContentLengthInputStream(HttpDataReceiver in,
                                long contentLength)
Creates a new length limited stream

Since:
3.0
Method Detail

close

public void close()
           throws java.io.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.


read

public int read()
         throws java.io.IOException
Read the next byte from the stream


read

public int read(byte[] b,
                int off,
                int len)
         throws java.io.IOException
Does standard InputStream.read(byte[], int, int)>InputStream.read(byte[], int, int) 55 behavior, but also notifies the watcher when the contents have been consumed.


read

public int read(byte[] b)
         throws java.io.IOException
Read more bytes from the stream.


skip

public long skip(long n)
          throws java.io.IOException
Skips and discards a number of bytes from the input stream.