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<T,B>
-
- org.apache.commons.io.input.ProxyInputStream.AbstractBuilder<BoundedInputStream,T>
-
- org.apache.commons.io.input.BoundedInputStream.Builder
-
- All Implemented Interfaces:
IOSupplier<BoundedInputStream>
- Enclosing class:
- BoundedInputStream
public static class BoundedInputStream.Builder extends ProxyInputStream.AbstractBuilder<BoundedInputStream,T>
Builds a newBoundedInputStream.By default, a
BoundedInputStreamis 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
ServletInputStreamcan 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 anBoundedInputStreamwith theContent-Lengthsent 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()Constructs a new builder ofBoundedInputStream.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description BoundedInputStreamget()Builds a newBoundedInputStream.TsetCount(long count)Sets the current number of bytes counted.TsetMaxCount(long maxCount)Sets the maximum number of bytes to return.TsetOnMaxCount(IOBiConsumer<java.lang.Long,java.lang.Long> onMaxCount)Sets the defaultBoundedInputStream.onMaxLength(long, long)behavior,nullresets to a NOOP.TsetPropagateClose(boolean propagateClose)Sets whether theProxyInputStream.close()method should propagate to the underlingInputStream.-
Methods inherited from class org.apache.commons.io.input.ProxyInputStream.AbstractBuilder
getAfterRead, setAfterRead
-
Methods inherited from class org.apache.commons.io.build.AbstractStreamBuilder
getBufferSize, getBufferSizeDefault, getChannel, getCharSequence, getCharset, getCharsetDefault, getFile, getInputStream, getOpenOptions, getOutputStream, getPath, getRandomAccessFile, getReader, getWriter, setBufferSize, setBufferSize, setBufferSizeChecker, setBufferSizeDefault, setBufferSizeMax, setCharset, setCharset, setCharsetDefault, setOpenOptions
-
Methods inherited from class org.apache.commons.io.build.AbstractOriginSupplier
checkOrigin, getOrigin, hasOrigin, newByteArrayOrigin, newChannelOrigin, newCharSequenceOrigin, newFileOrigin, newFileOrigin, newInputStreamOrigin, newOutputStreamOrigin, newPathOrigin, newPathOrigin, newRandomAccessFileOrigin, newRandomAccessFileOrigin, newReaderOrigin, newURIOrigin, newWriterOrigin, setByteArray, setChannel, setCharSequence, setFile, setFile, setInputStream, setOrigin, setOutputStream, setPath, setPath, setRandomAccessFile, setRandomAccessFile, setReader, setURI, setWriter
-
Methods inherited from class org.apache.commons.io.build.AbstractSupplier
asThis
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.apache.commons.io.function.IOSupplier
asSupplier, getUnchecked
-
-
-
-
Constructor Detail
-
Builder
public Builder()
Constructs a new builder ofBoundedInputStream.
-
-
Method Detail
-
get
public BoundedInputStream get() throws java.io.IOException
Builds a newBoundedInputStream.You must set an aspect that supports
AbstractStreamBuilder.getInputStream(), otherwise, this method throws an exception.If you start from an input stream, an exception can't be thrown, and you can call
IOSupplier.getUnchecked()instead.This builder uses the following aspects:
AbstractStreamBuilder.getInputStream()gets the target aspect.ProxyInputStream.AbstractBuilder.getAfterRead()#getCount()#getMaxCount()#getOnMaxCount()#isPropagateClose()
- Returns:
- a new instance.
- Throws:
java.lang.IllegalStateException- if theoriginisnull.java.lang.UnsupportedOperationException- if the origin cannot be converted to anInputStream.java.io.IOException- if an I/O error occurs converting to anInputStreamusingAbstractStreamBuilder.getInputStream().- See Also:
AbstractStreamBuilder.getInputStream(),IOSupplier.getUnchecked()
-
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:
thisinstance.
-
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, negative means unbound.- Returns:
thisinstance.
-
setOnMaxCount
public T setOnMaxCount(IOBiConsumer<java.lang.Long,java.lang.Long> onMaxCount)
Sets the defaultBoundedInputStream.onMaxLength(long, long)behavior,nullresets to a NOOP.The first Long is the number of bytes remaining to read before the maximum is reached count of bytes to read. The second Long is the count of bytes read.
This does not override a
BoundedInputStreamsubclass' implementation of theBoundedInputStream.onMaxLength(long, long)method.- Parameters:
onMaxCount- theProxyInputStream.afterRead(int)behavior.- Returns:
thisinstance.- Since:
- 2.18.0
-
setPropagateClose
public T setPropagateClose(boolean propagateClose)
Sets whether theProxyInputStream.close()method should propagate to the underlingInputStream.Default is
true.- Parameters:
propagateClose-trueif callingProxyInputStream.close()propagates to theclose()method of the underlying stream orfalseif it does not.- Returns:
thisinstance.
-
-