Class IoUtils


  • public final class IoUtils
    extends Object
    Utilities for IO operations.
    • Method Detail

      • toByteArray

        public static byte[] toByteArray​(InputStream is)
                                  throws IOException
        Reads and returns the rest of the given input stream as a byte array. Caller is responsible for closing the given input stream.
        Throws:
        IOException
      • toUtf8String

        public static String toUtf8String​(InputStream is)
                                   throws IOException
        Reads and returns the rest of the given input stream as a string. Caller is responsible for closing the given input stream.
        Throws:
        IOException
      • closeQuietly

        public static void closeQuietly​(AutoCloseable is,
                                        org.slf4j.Logger log)
        Closes the given Closeable quietly.
        Parameters:
        is - the given closeable
        log - logger used to log any failure should the close fail
      • closeIfCloseable

        public static void closeIfCloseable​(Object maybeCloseable,
                                            org.slf4j.Logger log)
        Closes the given Closeable quietly.
        Parameters:
        maybeCloseable - the given closeable
        log - logger used to log any failure should the close fail
      • copy

        public static long copy​(InputStream in,
                                OutputStream out)
                         throws IOException
        Copies all bytes from the given input stream to the given output stream. Caller is responsible for closing the streams.
        Throws:
        IOException - if there is any IO exception during read or write.
      • copy

        public static long copy​(InputStream in,
                                OutputStream out,
                                long readLimit)
                         throws IOException
        Copies all bytes from the given input stream to the given output stream. Caller is responsible for closing the streams.
        Throws:
        IOException - if there is any IO exception during read or write or the read limit is exceeded.
      • drainInputStream

        public static void drainInputStream​(InputStream in)
        Read all remaining data in the stream.
        Parameters:
        in - InputStream to read.
      • markStreamWithMaxReadLimit

        public static void markStreamWithMaxReadLimit​(InputStream s)
        If the stream supports marking, marks the stream at the current position with a readLimit value of 128 KiB.
        Parameters:
        s - The stream.
      • markStreamWithMaxReadLimit

        public static void markStreamWithMaxReadLimit​(InputStream s,
                                                      Integer maxReadLimit)
        If the stream supports marking, marks the stream at the current position with a read limit specified in maxReadLimit.
        Parameters:
        maxReadLimit - the maxReadLimit, if it's null, 128 KiB will be used.
        s - The stream.