public class BoundedInputStream
extends java.io.FilterInputStream
This is useful to wrap ServletInputStreams. The ServletInputStream will block if you try to read content from it that isn't there, because it doesn't know whether the content hasn't arrived yet or whether the content has finished. So, one of these, initialized with the Content-length sent in the ServletInputStream's header, will stop it blocking, providing it's been sent with a correct content length.
| Modifier and Type | Field and Description |
|---|---|
private long |
count
The count of bytes read.
|
private long |
mark
The marked position.
|
private long |
maxCount
The max count of bytes to read.
|
private boolean |
propagateClose
Flag if close should be propagated.
|
| Constructor and Description |
|---|
BoundedInputStream(java.io.InputStream in)
Constructs a new
BoundedInputStream that wraps the given input
stream and is unlimited. |
BoundedInputStream(java.io.InputStream inputStream,
long maxLength)
Constructs a new
BoundedInputStream that wraps the given input
stream and limits it to a certain size. |
| Modifier and Type | Method and Description |
|---|---|
int |
available() |
void |
close()
|
long |
getCount()
Gets the count of bytes read.
|
long |
getMaxLength()
Gets the max count of bytes to read.
|
private boolean |
isMaxLength() |
boolean |
isPropagateClose()
Tests whether the
close() method
should propagate to the underling InputStream. |
void |
mark(int readLimit)
Invokes the delegate's
mark(int) method. |
boolean |
markSupported()
Invokes the delegate's
markSupported() method. |
protected void |
onMaxLength(long maxLength,
long count)
A caller has caused a request that would cross the
maxLength boundary. |
int |
read()
Invokes the delegate's
read() method if
the current position is less than the limit. |
int |
read(byte[] b)
Invokes the delegate's
read(byte[]) method. |
int |
read(byte[] b,
int off,
int len)
Invokes the delegate's
read(byte[], int, int) method. |
void |
reset()
Invokes the delegate's
reset() method. |
void |
setPropagateClose(boolean propagateClose)
Sets whether the
close() method
should propagate to the underling InputStream. |
long |
skip(long n)
Invokes the delegate's
skip(long) method. |
java.lang.String |
toString()
Invokes the delegate's
toString() method. |
private final long maxCount
private long count
private long mark
private boolean propagateClose
public BoundedInputStream(java.io.InputStream in)
BoundedInputStream that wraps the given input
stream and is unlimited.in - The wrapped input stream.public BoundedInputStream(java.io.InputStream inputStream,
long maxLength)
BoundedInputStream that wraps the given input
stream and limits it to a certain size.inputStream - The wrapped input stream.maxLength - The maximum number of bytes to return.public int available()
throws java.io.IOException
available in class java.io.FilterInputStreamjava.io.IOExceptionpublic void close()
throws java.io.IOException
close in interface java.io.Closeableclose in interface java.lang.AutoCloseableclose in class java.io.FilterInputStreamjava.io.IOException - if an I/O error occurs.public long getCount()
public long getMaxLength()
private boolean isMaxLength()
public boolean isPropagateClose()
close() method
should propagate to the underling InputStream.true if calling close()
propagates to the close() method of the
underlying stream or false if it does not.public void mark(int readLimit)
mark(int) method.mark in class java.io.FilterInputStreamreadLimit - read ahead limitpublic boolean markSupported()
markSupported() method.markSupported in class java.io.FilterInputStreamprotected void onMaxLength(long maxLength,
long count)
throws java.io.IOException
maxLength boundary.maxLength - The max count of bytes to read.count - The count of bytes read.java.io.IOException - Subclasses may throw.public int read()
throws java.io.IOException
read() method if
the current position is less than the limit.read in class java.io.FilterInputStreamjava.io.IOException - if an I/O error occurs.public int read(byte[] b)
throws java.io.IOException
read(byte[]) method.read in class java.io.FilterInputStreamb - the buffer to read the bytes intojava.io.IOException - if an I/O error occurs.public int read(byte[] b,
int off,
int len)
throws java.io.IOException
read(byte[], int, int) method.read in class java.io.FilterInputStreamb - the buffer to read the bytes intooff - The start offsetlen - The number of bytes to readjava.io.IOException - if an I/O error occurs.public void reset()
throws java.io.IOException
reset() method.reset in class java.io.FilterInputStreamjava.io.IOException - if an I/O error occurs.public void setPropagateClose(boolean propagateClose)
close() method
should propagate to the underling InputStream.propagateClose - true if calling
close() propagates to the close()
method of the underlying stream or
false if it does not.public long skip(long n)
throws java.io.IOException
skip(long) method.skip in class java.io.FilterInputStreamn - the number of bytes to skipjava.io.IOException - if an I/O error occurs.public java.lang.String toString()
toString() method.toString in class java.lang.ObjecttoString()