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
ConstructorDescriptionConstructs a newBoundedInputStream
that wraps the given input stream and is unlimited.BoundedInputStream
(InputStream inputStream, long maxLength) Constructs a newBoundedInputStream
that wraps the given input stream and limits it to a certain size. -
Method Summary
Modifier and TypeMethodDescriptionint
void
close()
long
getCount()
Gets the count of bytes read.long
Gets the max count of bytes to read.boolean
Tests whether theclose()
method should propagate to the underlingInputStream
.void
mark
(int readLimit) Invokes the delegate'smark(int)
method.boolean
Invokes the delegate'smarkSupported()
method.protected void
onMaxLength
(long maxLength, long count) A caller has caused a request that would cross themaxLength
boundary.int
read()
Invokes the delegate'sread()
method if the current position is less than the limit.int
read
(byte[] b) Invokes the delegate'sread(byte[])
method.int
read
(byte[] b, int off, int len) Invokes the delegate'sread(byte[], int, int)
method.void
reset()
Invokes the delegate'sreset()
method.void
setPropagateClose
(boolean propagateClose) Sets whether theclose()
method should propagate to the underlingInputStream
.long
skip
(long n) Invokes the delegate'sskip(long)
method.toString()
Invokes the delegate'stoString()
method.
-
Constructor Details
-
BoundedInputStream
Constructs a newBoundedInputStream
that wraps the given input stream and is unlimited.- Parameters:
in
- The wrapped input stream.
-
BoundedInputStream
Constructs a newBoundedInputStream
that 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:
available
in classFilterInputStream
- Throws:
IOException
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in 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:
true
if callingclose()
propagates to theclose()
method of the underlying stream orfalse
if it does not.
-
mark
Invokes the delegate'smark(int)
method.- Overrides:
mark
in classFilterInputStream
- Parameters:
readLimit
- read ahead limit
-
markSupported
Invokes the delegate'smarkSupported()
method.- Overrides:
markSupported
in classFilterInputStream
- Returns:
- true if mark is supported, otherwise false
-
onMaxLength
A caller has caused a request that would cross themaxLength
boundary.- 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:
read
in 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:
read
in 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:
read
in 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:
reset
in classFilterInputStream
- Throws:
IOException
- if an I/O error occurs.
-
setPropagateClose
Sets whether theclose()
method should propagate to the underlingInputStream
.- Parameters:
propagateClose
-true
if callingclose()
propagates to theclose()
method of the underlying stream orfalse
if it does not.
-
skip
Invokes the delegate'sskip(long)
method.- Overrides:
skip
in 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.
-