Class BoundedInputStream
- All Implemented Interfaces:
- Closeable,- AutoCloseable
 To build an instance: Use the builder() to access all features.
 
 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();
 
 Listening for the max count reached
 BoundedInputStream s = BoundedInputStream.builder()
   .setPath(Paths.get("MyFile.xml"))
   .setMaxCount(1024)
   .setOnMaxCount((max, count) -> System.out.printf("Max count %,d reached with a last read count of %,d%n", max, count))
   .get();
 
 - Since:
- 2.0
- See Also:
- 
Nested Class SummaryNested Classes
- 
Field SummaryFields inherited from class java.io.FilterInputStreamin
- 
Constructor SummaryConstructorsConstructorDescriptionDeprecated.BoundedInputStream(InputStream inputStream, long maxCount) Deprecated.UseIOSupplier.get().
- 
Method SummaryModifier and TypeMethodDescriptionprotected voidafterRead(int n) Adds the number of read bytes to the count.intInvokes the delegate'sInputStream.available()method.static BoundedInputStream.Builderbuilder()Constructs a newBoundedInputStream.AbstractBuilder.voidclose()longgetCount()Gets the count of bytes read.longGets the max count of bytes to read.longDeprecated.UsegetMaxCount().longGets how many bytes remain to read.booleanTests whether theclose()method should propagate to the underlingInputStream.voidmark(int readLimit) Invokes the delegate'sInputStream.mark(int)method.booleanInvokes the delegate'sInputStream.markSupported()method.protected voidonMaxLength(long max, long count) A caller has caused a request that would cross themaxLengthboundary.intread()Invokes the delegate'sInputStream.read()method if the current position is less than the limit.intread(byte[] b) Invokes the delegate'sInputStream.read(byte[])method.intread(byte[] b, int off, int len) Invokes the delegate'sInputStream.read(byte[], int, int)method.voidreset()Invokes the delegate'sInputStream.reset()method.voidsetPropagateClose(boolean propagateClose) Deprecated.UseBoundedInputStream.AbstractBuilder.setPropagateClose(boolean).longskip(long n) Invokes the delegate'sInputStream.skip(long)method.toString()Invokes the delegate'sObject.toString()method.Methods inherited from class org.apache.commons.io.input.ProxyInputStreambeforeRead, handleIOException, setReference, unwrap
- 
Constructor Details- 
BoundedInputStreamDeprecated.UseIOSupplier.get().Constructs a newBoundedInputStreamthat wraps the given input stream and is unbounded.To build an instance: Use the builder()to access all features.- Parameters:
- in- The wrapped input stream.
 
- 
BoundedInputStreamDeprecated.UseIOSupplier.get().Constructs a newBoundedInputStreamthat 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, negative means unbound.
 
 
- 
- 
Method Details- 
builderConstructs a newBoundedInputStream.AbstractBuilder.- Returns:
- a new BoundedInputStream.AbstractBuilder.
- Since:
- 2.16.0
 
- 
afterReadAdds the number of read bytes to the count.- Overrides:
- afterReadin class- ProxyInputStream
- 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
 
- 
availableInvokes the delegate'sInputStream.available()method.- Overrides:
- availablein class- ProxyInputStream
- Returns:
- the number of available bytes, 0 if the stream is closed.
- Throws:
- IOException- if an I/O error occurs.
 
- 
close- Specified by:
- closein interface- AutoCloseable
- Specified by:
- closein interface- Closeable
- Overrides:
- closein class- ProxyInputStream
- Throws:
- IOException- if an I/O error occurs.
 
- 
getCountGets the count of bytes read.- Returns:
- The count of bytes read.
- Since:
- 2.12.0
 
- 
getMaxCountGets the max count of bytes to read.- Returns:
- The max count of bytes to read.
- Since:
- 2.16.0
 
- 
getMaxLengthDeprecated.UsegetMaxCount().Gets the max count of bytes to read.- Returns:
- The max count of bytes to read.
- Since:
- 2.12.0
 
- 
getRemainingGets how many bytes remain to read.- Returns:
- bytes how many bytes remain to read.
- Since:
- 2.16.0
 
- 
isPropagateCloseTests whether theclose()method should propagate to the underlingInputStream.- Returns:
- trueif calling- close()propagates to the- close()method of the underlying stream or- falseif it does not.
 
- 
markInvokes the delegate'sInputStream.mark(int)method.- Overrides:
- markin class- ProxyInputStream
- Parameters:
- readLimit- read ahead limit
 
- 
markSupportedInvokes the delegate'sInputStream.markSupported()method.- Overrides:
- markSupportedin class- ProxyInputStream
- Returns:
- true if mark is supported, otherwise false
- See Also:
 
- 
onMaxLengthA caller has caused a request that would cross themaxLengthboundary.Delegates to the consumer set in BoundedInputStream.AbstractBuilder.setOnMaxCount(IOBiConsumer).- Parameters:
- max- The max count of bytes to read.
- count- The count of bytes read.
- Throws:
- IOException- Subclasses may throw.
- Since:
- 2.12.0
 
- 
readInvokes the delegate'sInputStream.read()method if the current position is less than the limit.- Overrides:
- readin class- ProxyInputStream
- 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.
 
- 
readInvokes the delegate'sInputStream.read(byte[])method.- Overrides:
- readin class- ProxyInputStream
- 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.
 
- 
readInvokes the delegate'sInputStream.read(byte[], int, int)method.- Overrides:
- readin class- ProxyInputStream
- Parameters:
- b- the buffer to read the bytes into
- off- The start offset
- len- 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.
 
- 
resetInvokes the delegate'sInputStream.reset()method.- Overrides:
- resetin class- ProxyInputStream
- Throws:
- IOException- if an I/O error occurs.
 
- 
setPropagateCloseDeprecated.UseBoundedInputStream.AbstractBuilder.setPropagateClose(boolean).Sets whether theclose()method should propagate to the underlingInputStream.- Parameters:
- propagateClose-- trueif calling- close()propagates to the- close()method of the underlying stream or- falseif it does not.
 
- 
skipInvokes the delegate'sInputStream.skip(long)method.- Overrides:
- skipin class- ProxyInputStream
- Parameters:
- n- the number of bytes to skip
- Returns:
- the actual number of bytes skipped
- Throws:
- IOException- if an I/O error occurs.
 
- 
toStringInvokes the delegate'sObject.toString()method.- Overrides:
- toStringin class- Object
- Returns:
- the delegate's Object.toString()
 
 
- 
IOSupplier.get().