public final class LimitedInputStream extends FilterInputStream implements Closeable
InputStream which limits how many bytes which can be read.
This class is based on the
LimitedInputStream from Apache Commons Fileupload (v1.3.2),
which has the license as the Digg library, Apache License 2.0, but also supports
to silently treat the limit as EOF without any signal to distinguish between the EOF of the wrapped stream and
the limited stream.
| Modifier and Type | Field and Description |
|---|---|
static Supplier<Exception> |
SILENTLY_EOF_ON_REACHING_LIMIT
Supply this instead of an
exception supplier as parameter when contructing
a new LimitedInputStream to instruct it to
treat the limit as an ordinary EOF, and not throw any exception to signal that the
limit was reached during consumption of the stream. |
in| Constructor and Description |
|---|
LimitedInputStream(InputStream inputStream,
DataSize maxDataToRead,
Supplier<? extends Exception> throwIfTooManyBytes) |
| Modifier and Type | Method and Description |
|---|---|
int |
read()
Reads the next byte of data from this input stream.
|
int |
read(byte[] b,
int off,
int len)
Reads up to
len bytes of data from this input stream
into an array of bytes. |
available, close, mark, markSupported, read, reset, skippublic static final Supplier<Exception> SILENTLY_EOF_ON_REACHING_LIMIT
exception supplier as parameter when contructing
a new LimitedInputStream to instruct it to
treat the limit as an ordinary EOF, and not throw any exception to signal that the
limit was reached during consumption of the stream.
Invoking get() on this will throw an exception.
public LimitedInputStream(InputStream inputStream, DataSize maxDataToRead, Supplier<? extends Exception> throwIfTooManyBytes)
public int read()
throws IOException
int in the range
0 to 255. If no byte is available
because the end of the stream has been reached, the value
-1 is returned. This method blocks until input data
is available, the end of the stream is detected, or an exception
is thrown.
This method
simply performs in.read() and returns the result.
read in class FilterInputStream-1 if the end of the
stream is reached.IOException - if an I/O error occurs.FilterInputStream.inpublic int read(byte[] b,
int off,
int len)
throws IOException
len bytes of data from this input stream
into an array of bytes. If len is not zero, the method
blocks until some input is available; otherwise, no
bytes are read and 0 is returned.
This method simply performs in.read(b, off, len)
and returns the result.
read in class FilterInputStreamb - the buffer into which the data is read.off - The start offset in the destination array
b.len - the maximum number of bytes read.-1 if there is no more data because the end of
the stream has been reached.NullPointerException - If b is null.IndexOutOfBoundsException - If off is negative,
len is negative, or len is greater than
b.length - offIOException - if an I/O error occurs.FilterInputStream.inCopyright © 2018 Digipost. All rights reserved.