Package no.digipost

Class DiggIO


  • public final class DiggIO
    extends Object
    IO-related utilities.
    • Method Detail

      • autoClosing

        public static <T extends AutoCloseableConsumer<T> autoClosing​(ThrowingConsumer<T,​? extends Exception> consumer)
        Wrap a consumer which processes an AutoCloseable (typically an InputStream or similar) into a new Consumer which will always close the AutoCloseable when the given consumer returns, successfully or throwing an exception.
        Parameters:
        consumer - the ThrowingConsumer taking an AutoCloseable as argument.
        Returns:
        the Consumer which will handle closing of the passed AutoCloseable.
      • 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 an AutoCloseable (typically an InputStream or similar) into a new Function which will always close the AutoCloseable when the given function returns, successfully or throwing an exception.
        Parameters:
        function - the ThrowingFunction taking an AutoCloseable as argument.
        Returns:
        the Function which will handle closing of the passed AutoCloseable.
      • limit

        public static InputStream limit​(InputStream inputStream,
                                        DataSize maxDataToRead)
        Limit the number of bytes to be read from an InputStream. 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 - The InputStream 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 an InputStream. If the number of bytes exceeds the given threshold, an exception will be thrown.
        Parameters:
        inputStream - The InputStream 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 an IOException, it will be wrapped in a RuntimeException.