Package org.apache.commons.io.input
Class BoundedInputStream
java.lang.Object
java.io.InputStream
java.io.FilterInputStream
org.apache.commons.io.input.BoundedInputStream
- All Implemented Interfaces:
Closeable,AutoCloseable
Reads bytes up to a maximum length, if its count goes above that, it stops.
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.
- Since:
- 2.0
-
Field Summary
Fields inherited from class java.io.FilterInputStream
in -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a newBoundedInputStreamthat wraps the given input stream and is unlimited.BoundedInputStream(InputStream inputStream, long maxLength) Constructs a newBoundedInputStreamthat wraps the given input stream and limits it to a certain size. -
Method Summary
Modifier and TypeMethodDescriptionintvoidclose()longgetCount()Gets the count of bytes read.longGets the max count of bytes to read.booleanTests whether theclose()method should propagate to the underlingInputStream.voidmark(int readLimit) Invokes the delegate'smark(int)method.booleanInvokes the delegate'smarkSupported()method.protected voidonMaxLength(long maxLength, long count) A caller has caused a request that would cross themaxLengthboundary.intread()Invokes the delegate'sread()method if the current position is less than the limit.intread(byte[] b) Invokes the delegate'sread(byte[])method.intread(byte[] b, int off, int len) Invokes the delegate'sread(byte[], int, int)method.voidreset()Invokes the delegate'sreset()method.voidsetPropagateClose(boolean propagateClose) Sets whether theclose()method should propagate to the underlingInputStream.longskip(long n) Invokes the delegate'sskip(long)method.toString()Invokes the delegate'stoString()method.
-
Constructor Details
-
BoundedInputStream
Constructs a newBoundedInputStreamthat wraps the given input stream and is unlimited.- Parameters:
in- The wrapped input stream.
-
BoundedInputStream
Constructs a newBoundedInputStreamthat wraps the given input stream and limits it to a certain size.- Parameters:
inputStream- The wrapped input stream.maxLength- The maximum number of bytes to return.
-
-
Method Details
-
available
- Overrides:
availablein classFilterInputStream- Throws:
IOException
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classFilterInputStream- Throws:
IOException- if an I/O error occurs.
-
getCount
Gets the count of bytes read.- Returns:
- The count of bytes read.
- Since:
- 2.12.0
-
getMaxLength
Gets the max count of bytes to read.- Returns:
- The max count of bytes to read.
- Since:
- 2.12.0
-
isPropagateClose
Tests whether theclose()method should propagate to the underlingInputStream.- Returns:
trueif callingclose()propagates to theclose()method of the underlying stream orfalseif it does not.
-
mark
Invokes the delegate'smark(int)method.- Overrides:
markin classFilterInputStream- Parameters:
readLimit- read ahead limit
-
markSupported
Invokes the delegate'smarkSupported()method.- Overrides:
markSupportedin classFilterInputStream- Returns:
- true if mark is supported, otherwise false
-
onMaxLength
A caller has caused a request that would cross themaxLengthboundary.- Parameters:
maxLength- The max count of bytes to read.count- The count of bytes read.- Throws:
IOException- Subclasses may throw.- Since:
- 2.12.0
-
read
Invokes the delegate'sread()method if the current position is less than the limit.- Overrides:
readin classFilterInputStream- Returns:
- the byte read or -1 if the end of stream or the limit has been reached.
- Throws:
IOException- if an I/O error occurs.
-
read
Invokes the delegate'sread(byte[])method.- Overrides:
readin classFilterInputStream- Parameters:
b- the buffer to read the bytes into- Returns:
- the number of bytes read or -1 if the end of stream or the limit has been reached.
- Throws:
IOException- if an I/O error occurs.
-
read
Invokes the delegate'sread(byte[], int, int)method.- Overrides:
readin classFilterInputStream- Parameters:
b- the buffer to read the bytes intooff- The start offsetlen- The number of bytes to read- Returns:
- the number of bytes read or -1 if the end of stream or the limit has been reached.
- Throws:
IOException- if an I/O error occurs.
-
reset
Invokes the delegate'sreset()method.- Overrides:
resetin classFilterInputStream- Throws:
IOException- if an I/O error occurs.
-
setPropagateClose
Sets whether theclose()method should propagate to the underlingInputStream.- Parameters:
propagateClose-trueif callingclose()propagates to theclose()method of the underlying stream orfalseif it does not.
-
skip
Invokes the delegate'sskip(long)method.- Overrides:
skipin classFilterInputStream- Parameters:
n- the number of bytes to skip- Returns:
- the actual number of bytes skipped
- Throws:
IOException- if an I/O error occurs.
-
toString
Invokes the delegate'stoString()method.
-