Class BoundedInputStream
- java.lang.Object
-
- java.io.InputStream
-
- java.io.FilterInputStream
-
- org.apache.commons.io.input.ProxyInputStream
-
- org.apache.commons.io.input.BoundedInputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public class BoundedInputStream extends ProxyInputStream
Reads bytes up to a maximum count and stops once reached.To build an instance: Use the
builder()to access all features.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
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();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:
BoundedInputStream.Builder
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classBoundedInputStream.AbstractBuilder<T extends BoundedInputStream.AbstractBuilder<T>>For subclassing builders fromBoundedInputStreamsubclassses.static classBoundedInputStream.BuilderBuilds a newBoundedInputStream.
-
Field Summary
Fields Modifier and Type Field Description private longcountThe current count of bytes counted.private longmarkThe current mark.private longmaxCountThe max count of bytes to read.private IOBiConsumer<java.lang.Long,java.lang.Long>onMaxCountprivate booleanpropagateCloseFlag if close should be propagated.
-
Constructor Summary
Constructors Constructor Description BoundedInputStream(java.io.InputStream in)Deprecated.UseIOSupplier.get().BoundedInputStream(java.io.InputStream inputStream, long maxCount)Deprecated.UseIOSupplier.get().BoundedInputStream(java.io.InputStream inputStream, BoundedInputStream.Builder builder)BoundedInputStream(BoundedInputStream.Builder builder)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected voidafterRead(int n)Adds the number of read bytes to the count.intavailable()Invokes the delegate'sInputStream.available()method.static BoundedInputStream.Builderbuilder()Constructs a newBoundedInputStream.AbstractBuilder.voidclose()longgetCount()Gets the count of bytes read.longgetMaxCount()Gets the max count of bytes to read.longgetMaxLength()Deprecated.UsegetMaxCount().longgetRemaining()Gets how many bytes remain to read.private booleanisMaxCount()booleanisPropagateClose()Tests whether theclose()method should propagate to the underlingInputStream.voidmark(int readLimit)Invokes the delegate'sInputStream.mark(int)method.booleanmarkSupported()Invokes 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.longskip(long n)Invokes the delegate'sInputStream.skip(long)method.private longtoReadLen(long len)java.lang.StringtoString()Invokes the delegate'sObject.toString()method.-
Methods inherited from class org.apache.commons.io.input.ProxyInputStream
beforeRead, checkOpen, handleIOException, isClosed, setReference, unwrap
-
-
-
-
Field Detail
-
count
private long count
The current count of bytes counted.
-
mark
private long mark
The current mark.
-
maxCount
private final long maxCount
The max count of bytes to read.
-
onMaxCount
private final IOBiConsumer<java.lang.Long,java.lang.Long> onMaxCount
-
propagateClose
private boolean propagateClose
Flag if close should be propagated. TODO Make final in 3.0.
-
-
Constructor Detail
-
BoundedInputStream
BoundedInputStream(BoundedInputStream.Builder builder) throws java.io.IOException
- Throws:
java.io.IOException
-
BoundedInputStream
@Deprecated public BoundedInputStream(java.io.InputStream in)
Deprecated.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.
-
BoundedInputStream
BoundedInputStream(java.io.InputStream inputStream, BoundedInputStream.Builder builder)
-
BoundedInputStream
@Deprecated public BoundedInputStream(java.io.InputStream inputStream, long maxCount)Deprecated.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 Detail
-
builder
public static BoundedInputStream.Builder builder()
Constructs a newBoundedInputStream.AbstractBuilder.- Returns:
- a new
BoundedInputStream.AbstractBuilder. - Since:
- 2.16.0
-
afterRead
protected void afterRead(int n) throws java.io.IOExceptionAdds the number of read bytes to the count.- Overrides:
afterReadin classProxyInputStream- Parameters:
n- number of bytes read, or -1 if no more bytes are available- Throws:
java.io.IOException- Not thrown here but subclasses may throw.- Since:
- 2.0
-
available
public int available() throws java.io.IOExceptionInvokes the delegate'sInputStream.available()method.- Overrides:
availablein classProxyInputStream- Returns:
- the number of available bytes, 0 if the stream is closed.
- Throws:
java.io.IOException- if an I/O error occurs.
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classProxyInputStream- Throws:
java.io.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 public long 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:
trueif callingclose()propagates to theclose()method of the underlying stream orfalseif it does not.
-
mark
public void mark(int readLimit)
Invokes the delegate'sInputStream.mark(int)method.- Overrides:
markin classProxyInputStream- Parameters:
readLimit- read ahead limit
-
markSupported
public boolean markSupported()
Invokes the delegate'sInputStream.markSupported()method.- Overrides:
markSupportedin classProxyInputStream- Returns:
- true if mark is supported, otherwise false
- See Also:
ProxyInputStream.mark(int),ProxyInputStream.reset()
-
onMaxLength
protected void onMaxLength(long max, long count) throws java.io.IOExceptionA 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:
java.io.IOException- Subclasses may throw.- Since:
- 2.12.0
-
read
public int read() throws java.io.IOExceptionInvokes the delegate'sInputStream.read()method if the current position is less than the limit.- Overrides:
readin classProxyInputStream- Returns:
- the byte read or -1 if the end of stream or the limit has been reached.
- Throws:
java.io.IOException- if an I/O error occurs.
-
read
public int read(byte[] b) throws java.io.IOExceptionInvokes the delegate'sInputStream.read(byte[])method.- Overrides:
readin 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:
java.io.IOException- if an I/O error occurs.
-
read
public int read(byte[] b, int off, int len) throws java.io.IOExceptionInvokes the delegate'sInputStream.read(byte[], int, int)method.- Overrides:
readin 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:
java.io.IOException- if an I/O error occurs.
-
reset
public void reset() throws java.io.IOExceptionInvokes the delegate'sInputStream.reset()method.- Overrides:
resetin classProxyInputStream- Throws:
java.io.IOException- if an I/O error occurs.
-
setPropagateClose
@Deprecated public void setPropagateClose(boolean propagateClose)
Deprecated.Sets whether theclose()method should propagate to the underlingInputStream.- Parameters:
propagateClose-trueif callingclose()propagates to theclose()method of the underlying stream orfalseif it does not.
-
skip
public long skip(long n) throws java.io.IOExceptionInvokes the delegate'sInputStream.skip(long)method.- Overrides:
skipin classProxyInputStream- Parameters:
n- the number of bytes to skip- Returns:
- the actual number of bytes skipped
- Throws:
java.io.IOException- if an I/O error occurs.
-
toReadLen
private long toReadLen(long len)
-
toString
public java.lang.String toString()
Invokes the delegate'sObject.toString()method.- Overrides:
toStringin classjava.lang.Object- Returns:
- the delegate's
Object.toString()
-
-