Package no.digipost.io
Class LimitedInputStream
java.lang.Object
java.io.InputStream
java.io.FilterInputStream
no.digipost.io.LimitedInputStream
- All Implemented Interfaces:
Closeable,AutoCloseable
An
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.
-
Field Summary
FieldsModifier and TypeFieldDescriptionSupply this instead of anexception supplieras parameter when contructing a newLimitedInputStreamto 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.Fields inherited from class java.io.FilterInputStream
in -
Constructor Summary
ConstructorsConstructorDescriptionLimitedInputStream(InputStream inputStream, DataSize maxDataToRead, Supplier<? extends Exception> throwIfTooManyBytes) -
Method Summary
Methods inherited from class java.io.FilterInputStream
available, close, mark, markSupported, read, reset, skipMethods inherited from class java.io.InputStream
nullInputStream, readAllBytes, readNBytes, readNBytes, skipNBytes, transferTo
-
Field Details
-
SILENTLY_EOF_ON_REACHING_LIMIT
Supply this instead of anexception supplieras parameter when contructing a newLimitedInputStreamto 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.
-
-
Constructor Details
-
LimitedInputStream
public LimitedInputStream(InputStream inputStream, DataSize maxDataToRead, Supplier<? extends Exception> throwIfTooManyBytes)
-
-
Method Details
-
read
Reads the next byte of data from this input stream. The value byte is returned as anintin the range0to255. If no byte is available because the end of the stream has been reached, the value-1is 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.- Overrides:
readin classFilterInputStream- Returns:
- the next byte of data, or
-1if the end of the stream is reached. - Throws:
IOException- if an I/O error occurs.- See Also:
-
read
Reads up tolenbytes of data from this input stream into an array of bytes. Iflenis not zero, the method blocks until some input is available; otherwise, no bytes are read and0is returned.This method simply performs
in.read(b, off, len)and returns the result.- Overrides:
readin classFilterInputStream- Parameters:
b- the buffer into which the data is read.off- The start offset in the destination arrayb.len- the maximum number of bytes read.- Returns:
- the total number of bytes read into the buffer, or
-1if there is no more data because the end of the stream has been reached. - Throws:
NullPointerException- Ifbisnull.IndexOutOfBoundsException- Ifoffis negative,lenis negative, orlenis greater thanb.length - offIOException- if an I/O error occurs.- See Also:
-