Class BoundedInputStream.Builder
- java.lang.Object
-
- org.apache.commons.io.build.AbstractSupplier<T,B>
-
- org.apache.commons.io.build.AbstractOriginSupplier<T,B>
-
- org.apache.commons.io.build.AbstractStreamBuilder<BoundedInputStream,T>
-
- org.apache.commons.io.input.BoundedInputStream.Builder
-
- All Implemented Interfaces:
IOSupplier<BoundedInputStream>
- Enclosing class:
- BoundedInputStream
public static class BoundedInputStream.Builder extends AbstractStreamBuilder<BoundedInputStream,T>
Builds a newBoundedInputStream
.By default, a
BoundedInputStream
is unbound; so make sure to callBoundedInputStream.AbstractBuilder.setMaxCount(long)
.You can find out how many bytes this stream has seen so far by calling
BoundedInputStream.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 anBoundedInputStream
with theContent-Length
sent in theServletInputStream
'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.16.0
- See Also:
get()
-
-
Constructor Summary
Constructors Constructor Description Builder()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description BoundedInputStream
get()
Builds a newBoundedInputStream
.T
setCount(long count)
Sets the current number of bytes counted.T
setMaxCount(long maxCount)
Sets the maximum number of bytes to return.T
setPropagateClose(boolean propagateClose)
Sets whether theBoundedInputStream.close()
method should propagate to the underlingInputStream
.-
Methods inherited from class org.apache.commons.io.build.AbstractStreamBuilder
getCharset, setBufferSize, setBufferSize, setBufferSizeChecker, setBufferSizeMax, setCharset, setCharset, setOpenOptions
-
Methods inherited from class org.apache.commons.io.build.AbstractOriginSupplier
setByteArray, setCharSequence, setFile, setFile, setInputStream, setOutputStream, setPath, setPath, setReader, setURI, setWriter
-
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.apache.commons.io.function.IOSupplier
asSupplier
-
-
-
-
Method Detail
-
get
public BoundedInputStream get() throws java.io.IOException
Builds a newBoundedInputStream
.You must set input that supports
AbstractStreamBuilder.getInputStream()
, otherwise, this method throws an exception.This builder use the following aspects:
AbstractStreamBuilder.getInputStream()
- maxCount
- propagateClose
- Returns:
- a new instance.
- Throws:
java.lang.IllegalStateException
- if theorigin
isnull
.java.lang.UnsupportedOperationException
- if the origin cannot be converted to anInputStream
.java.io.IOException
- if an I/O error occurs.- See Also:
AbstractStreamBuilder.getInputStream()
-
setCount
public T setCount(long count)
Sets the current number of bytes counted.Useful when building from another stream to carry forward a read count.
Default is
0
, negative means 0.- Parameters:
count
- The current number of bytes counted.- Returns:
- this.
-
setMaxCount
public T setMaxCount(long maxCount)
Sets the maximum number of bytes to return.Default is -1, negative means unbound.
- Parameters:
maxCount
- The maximum number of bytes to return.- Returns:
- this.
-
setPropagateClose
public T setPropagateClose(boolean propagateClose)
Sets whether theBoundedInputStream.close()
method should propagate to the underlingInputStream
.Default is
true
.- Parameters:
propagateClose
-true
if callingBoundedInputStream.close()
propagates to theclose()
method of the underlying stream orfalse
if it does not.- Returns:
- this.
-
-