Class BoundedInputStream
- All Implemented Interfaces:
Closeable
,AutoCloseable
To build an instance, see BoundedInputStream.AbstractBuilder
.
By default, a BoundedInputStream
is unbound; so make sure to call BoundedInputStream.AbstractBuilder.setMaxCount(long)
.
You can find out how many bytes this stream has seen so far by calling getCount()
. This value reflects bytes read and skipped.
Using a ServletInputStream
A ServletInputStream
can block if you try to read content that isn't there
because it doesn't know whether the content hasn't arrived yet or whether the content has finished. Initialize an BoundedInputStream
with the
Content-Length
sent in the ServletInputStream
's header, this stop it from blocking, providing it's been sent with a correct content
length in the first place.
Using NIO
BoundedInputStream s = BoundedInputStream.builder()
.setPath(Paths.get("MyFile.xml"))
.setMaxCount(1024)
.setPropagateClose(false)
.get();
Using IO
BoundedInputStream s = BoundedInputStream.builder()
.setFile(new File("MyFile.xml"))
.setMaxCount(1024)
.setPropagateClose(false)
.get();
Counting Bytes
You can set the running count when building, which is most useful when starting from another stream:
InputStream in = ...;
BoundedInputStream s = BoundedInputStream.builder()
.setInputStream(in)
.setCount(12)
.setMaxCount(1024)
.setPropagateClose(false)
.get();
- Since:
- 2.0
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static class
For subclassing builders fromBoundedInputStream
subclassses.static class
Builds a newBoundedInputStream
. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate long
The current count of bytes counted.private long
The current mark.private final long
The max count of bytes to read.private boolean
Flag if close should be propagated.Fields inherited from class java.io.FilterInputStream
in
-
Constructor Summary
ConstructorsConstructorDescriptionDeprecated.BoundedInputStream
(InputStream inputStream, long maxCount) Deprecated.UseIOSupplier.get()
.BoundedInputStream
(InputStream inputStream, long count, long maxCount, boolean propagateClose) Constructs a newBoundedInputStream
that wraps the given input stream and limits it to a certain size. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
afterRead
(int n) Adds the number of read bytes to the count.int
Invokes the delegate'savailable()
method.static BoundedInputStream.Builder
builder()
Constructs a newBoundedInputStream.AbstractBuilder
.void
close()
long
getCount()
Gets the count of bytes read.long
Gets the max count of bytes to read.long
Deprecated.UsegetMaxCount()
.long
Gets how many bytes remain to read.private boolean
boolean
Tests whether theclose()
method should propagate to the underlingInputStream
.void
mark
(int readLimit) Invokes the delegate'sInputStream.mark(int)
method.boolean
Invokes the delegate'sInputStream.markSupported()
method.protected void
onMaxLength
(long maxLength, long count) A caller has caused a request that would cross themaxLength
boundary.int
read()
Invokes the delegate'sInputStream.read()
method if the current position is less than the limit.int
read
(byte[] b) Invokes the delegate'sInputStream.read(byte[])
method.int
read
(byte[] b, int off, int len) Invokes the delegate'sInputStream.read(byte[], int, int)
method.void
reset()
Invokes the delegate'sInputStream.reset()
method.void
setPropagateClose
(boolean propagateClose) Deprecated.long
skip
(long n) Invokes the delegate'sInputStream.skip(long)
method.private long
toReadLen
(long len) toString()
Invokes the delegate'sObject.toString()
method.Methods inherited from class org.apache.commons.io.input.ProxyInputStream
beforeRead, handleIOException, unwrap
-
Field Details
-
count
private long countThe current count of bytes counted. -
mark
private long markThe current mark. -
maxCount
private final long maxCountThe max count of bytes to read. -
propagateClose
private boolean propagateCloseFlag if close should be propagated. TODO Make final in 3.0.
-
-
Constructor Details
-
BoundedInputStream
Deprecated.UseIOSupplier.get()
.Constructs a newBoundedInputStream
that wraps the given input stream and is unlimited.- Parameters:
in
- The wrapped input stream.
-
BoundedInputStream
Deprecated.UseIOSupplier.get()
.Constructs a newBoundedInputStream
that wraps the given input stream and limits it to a certain size.- Parameters:
inputStream
- The wrapped input stream.maxCount
- The maximum number of bytes to return.
-
BoundedInputStream
BoundedInputStream(InputStream inputStream, long count, long maxCount, boolean propagateClose) Constructs a newBoundedInputStream
that wraps the given input stream and limits it to a certain size.- Parameters:
inputStream
- The wrapped input stream.count
- The current number of bytes read.maxCount
- The maximum number of bytes to return.propagateClose
-true
if callingclose()
propagates to theclose()
method of the underlying stream orfalse
if it does not.
-
-
Method Details
-
builder
Constructs a newBoundedInputStream.AbstractBuilder
.- Returns:
- a new
BoundedInputStream.AbstractBuilder
. - Since:
- 2.16.0
-
afterRead
Adds the number of read bytes to the count.- Overrides:
afterRead
in classProxyInputStream
- Parameters:
n
- number of bytes read, or -1 if no more bytes are available- Throws:
IOException
- Not thrown here but subclasses may throw.- Since:
- 2.0
-
available
Invokes the delegate'savailable()
method.- Overrides:
available
in classProxyInputStream
- Returns:
- the number of available bytes
- Throws:
IOException
- if an I/O error occurs.
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classProxyInputStream
- Throws:
IOException
- if an I/O error occurs.
-
getCount
public long getCount()Gets the count of bytes read.- Returns:
- The count of bytes read.
- Since:
- 2.12.0
-
getMaxCount
public long getMaxCount()Gets the max count of bytes to read.- Returns:
- The max count of bytes to read.
- Since:
- 2.16.0
-
getMaxLength
Deprecated.UsegetMaxCount()
.Gets the max count of bytes to read.- Returns:
- The max count of bytes to read.
- Since:
- 2.12.0
-
getRemaining
public long getRemaining()Gets how many bytes remain to read.- Returns:
- bytes how many bytes remain to read.
- Since:
- 2.16.0
-
isMaxCount
private boolean isMaxCount() -
isPropagateClose
public boolean 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
public void mark(int readLimit) Invokes the delegate'sInputStream.mark(int)
method.- Overrides:
mark
in classProxyInputStream
- Parameters:
readLimit
- read ahead limit
-
markSupported
public boolean markSupported()Invokes the delegate'sInputStream.markSupported()
method.- Overrides:
markSupported
in classProxyInputStream
- 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'sInputStream.read()
method if the current position is less than the limit.- Overrides:
read
in classProxyInputStream
- 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'sInputStream.read(byte[])
method.- Overrides:
read
in classProxyInputStream
- 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'sInputStream.read(byte[], int, int)
method.- Overrides:
read
in classProxyInputStream
- 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'sInputStream.reset()
method.- Overrides:
reset
in classProxyInputStream
- Throws:
IOException
- if an I/O error occurs.
-
setPropagateClose
Deprecated.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'sInputStream.skip(long)
method.- Overrides:
skip
in classProxyInputStream
- Parameters:
n
- the number of bytes to skip- Returns:
- the actual number of bytes skipped
- Throws:
IOException
- if an I/O error occurs.
-
toReadLen
private long toReadLen(long len) -
toString
Invokes the delegate'sObject.toString()
method.- Overrides:
toString
in classObject
- Returns:
- the delegate's
Object.toString()
-
IOSupplier.get()
.