org.apache.commons.httpclient

Class ContentLengthInputStream

Implemented Interfaces:
Closeable

public class ContentLengthInputStream
extends InputStream

Cuts the wrapped InputStream off after a specified number of bytes.

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.

Since:
2.0

Constructor Summary

ContentLengthInputStream(InputStream in, int contentLength)
Deprecated. use ContentLengthInputStream(InputStream,long) Creates a new length limited stream
ContentLengthInputStream(InputStream in, long contentLength)
Creates a new length limited stream

Method Summary

int
available()
Returns the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream.
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) 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, close, mark, markSupported, read, read, read, reset, skip

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Details

ContentLengthInputStream

public ContentLengthInputStream(InputStream in,
                                int contentLength)

Deprecated. use ContentLengthInputStream(InputStream,long) Creates a new length limited stream

Parameters:
in - The stream to wrap
contentLength - The maximum number of bytes that can be read from the stream. Subsequent read operations will return -1.
Usages and Demos :

View More Examples of ContentLengthInputStream(InputStream in,int contentLength)
   1:                     if (len >= 0) {
   2:                         in = new ContentLengthInputStream(in, len);
   3:                     }
   4:                 }
   5:                 this.entity = in;

View Full Code Here
   1:                 if (len >= 0) {
   2:                     in = new ContentLengthInputStream(in, len);
   3:                 }
   4:             }
   5:             this.entity = in;

View Full Code Here

ContentLengthInputStream

public ContentLengthInputStream(InputStream in,
                                long contentLength)
Creates a new length limited stream
Parameters:
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
Usages and Demos :

View More Examples of ContentLengthInputStream(InputStream in,long contentLength)
   1:                     if (len >= 0) {
   2:                         in = new ContentLengthInputStream(in, len);
   3:                     }
   4:                 }
   5:                 this.entity = in;

View Full Code Here
   1:                 if (len >= 0) {
   2:                     in = new ContentLengthInputStream(in, len);
   3:                 }
   4:             }
   5:             this.entity = in;

View Full Code Here

Method Details

available

public int available()
            throws IOException
Returns the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream. The next caller might be the same thread or another thread.

The available method for class InputStream always returns 0.

This method should be overridden by subclasses.

Overrides:
available in interface InputStream
Returns:
the number of bytes that can be read from this input stream without blocking.
Throws:
IOException - if an I/O error occurs.

close

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.

Specified by:
close in interface Closeable
Overrides:
close in interface InputStream
Throws:
IOException - If an IO problem occurs.

read

public int read()
            throws IOException
Read the next byte from the stream
Overrides:
read in interface InputStream
Returns:
The next byte or -1 if the end of stream has been reached.
Throws:
IOException - If an IO problem occurs

read

public int read(byte[] b)
            throws IOException
Read more bytes from the stream.
Parameters:
b - The byte array to put the new data in.
Returns:
The number of bytes read into the buffer.
Throws:
IOException - If an IO problem occurs
See Also:
java.io.InputStream.read(byte[])

read

public int read(byte[] b,
                int off,
                int len)
            throws IOException
Does standard InputStream.read(byte[], int, int) behavior, but also notifies the watcher when the contents have been consumed.
Parameters:
b - The byte array to fill.
off - Start filling at this position.
len - The number of bytes to attempt to read.
Returns:
The number of bytes read, or -1 if the end of content has been reached.
Throws:
IOException - Should an error occur on the wrapped stream.

skip

public long skip(long n)
            throws IOException
Skips and discards a number of bytes from the input stream.
Overrides:
skip in interface InputStream
Parameters:
n - The number of bytes to skip.
Returns:
The actual number of bytes skipped. <= 0 if no bytes are skipped.
Throws:
IOException - If an error occurs while skipping bytes.