Package no.digipost
Class DiggIO
- java.lang.Object
-
- no.digipost.DiggIO
-
public final class DiggIO extends Object
IO-related utilities.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T extends AutoCloseable>
Consumer<T>autoClosing(ThrowingConsumer<T,? extends Exception> consumer)
Wrap a consumer which processes anAutoCloseable
(typically an InputStream or similar) into a newConsumer
which will always close theAutoCloseable
when the given consumer returns, successfully or throwing an exception.static <T extends AutoCloseable,R>
Function<T,R>autoClosing(ThrowingFunction<T,R,? extends Exception> function)
Wrap a function which yields a result from processing anAutoCloseable
(typically an InputStream or similar) into a newFunction
which will always close theAutoCloseable
when the given function returns, successfully or throwing an exception.static InputStream
limit(InputStream inputStream, DataSize maxDataToRead)
Limit the number of bytes to be read from anInputStream
.static InputStream
limit(InputStream inputStream, DataSize maxDataToRead, Supplier<? extends Exception> throwIfTooManyBytes)
Limit the number of bytes to be read from anInputStream
.
-
-
-
Method Detail
-
autoClosing
public static <T extends AutoCloseable> Consumer<T> autoClosing(ThrowingConsumer<T,? extends Exception> consumer)
Wrap a consumer which processes anAutoCloseable
(typically an InputStream or similar) into a newConsumer
which will always close theAutoCloseable
when the given consumer returns, successfully or throwing an exception.- Parameters:
consumer
- theThrowingConsumer
taking anAutoCloseable
as argument.- Returns:
- the
Consumer
which will handle closing of the passedAutoCloseable
.
-
autoClosing
public static <T extends AutoCloseable,R> Function<T,R> autoClosing(ThrowingFunction<T,R,? extends Exception> function)
Wrap a function which yields a result from processing anAutoCloseable
(typically an InputStream or similar) into a newFunction
which will always close theAutoCloseable
when the given function returns, successfully or throwing an exception.- Parameters:
function
- theThrowingFunction
taking anAutoCloseable
as argument.- Returns:
- the
Function
which will handle closing of the passedAutoCloseable
.
-
limit
public static InputStream limit(InputStream inputStream, DataSize maxDataToRead)
Limit the number of bytes to be read from anInputStream
. The returned stream silently ends reading, i.e. returns-1
from.read()
, if the read bytes exceeds the given threshold. This behavior is appropriate when it is actually not desireable to consume the entire stream, typically for processing some data known to be located in the leading part of the stream, and it is imperative to protect against accidentally consuming an entire (potentially large) stream.- Parameters:
inputStream
- TheInputStream
to limit bytes to read from.maxDataToRead
- The limit of data to read.- See Also:
limit(InputStream, DataSize, Supplier)
-
limit
public static InputStream limit(InputStream inputStream, DataSize maxDataToRead, Supplier<? extends Exception> throwIfTooManyBytes)
Limit the number of bytes to be read from anInputStream
. If the number of bytes exceeds the given threshold, an exception will be thrown.- Parameters:
inputStream
- TheInputStream
to limit bytes to read from.maxDataToRead
- The limit of data to read.throwIfTooManyBytes
- Supplier of exception to throw if more bytes are read than the max allowed. If the supplier returns a non-RuntimeException
which is not anIOException
, it will be wrapped in aRuntimeException
.
-
-