Package no.digipost
Class DiggIO
java.lang.Object
no.digipost.DiggIO
IO-related utilities.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends AutoCloseable>
Consumer<T>autoClosing(ThrowingConsumer<T, ? extends Exception> consumer) Wrap a consumer which processes anAutoCloseable(typically an InputStream or similar) into a newConsumerwhich will always close theAutoCloseablewhen 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 newFunctionwhich will always close theAutoCloseablewhen the given function returns, successfully or throwing an exception.static InputStreamlimit(InputStream inputStream, DataSize maxDataToRead) Limit the number of bytes to be read from anInputStream.static InputStreamlimit(InputStream inputStream, DataSize maxDataToRead, Supplier<? extends Exception> throwIfTooManyBytes) Limit the number of bytes to be read from anInputStream.
-
Method Details
-
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 newConsumerwhich will always close theAutoCloseablewhen the given consumer returns, successfully or throwing an exception.- Parameters:
consumer- theThrowingConsumertaking anAutoCloseableas argument.- Returns:
- the
Consumerwhich 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 newFunctionwhich will always close theAutoCloseablewhen the given function returns, successfully or throwing an exception.- Parameters:
function- theThrowingFunctiontaking anAutoCloseableas argument.- Returns:
- the
Functionwhich will handle closing of the passedAutoCloseable.
-
limit
Limit the number of bytes to be read from anInputStream. The returned stream silently ends reading, i.e. returns-1from.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- TheInputStreamto limit bytes to read from.maxDataToRead- The limit of data to read.- See Also:
-
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- TheInputStreamto 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-RuntimeExceptionwhich is not anIOException, it will be wrapped in aRuntimeException.
-