Class IOUtils


  • public final class IOUtils
    extends java.lang.Object
    Utility functions.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int DEFAULT_BUFFER_SIZE
      The default buffer size (8192) to use in copy methods.
      static java.nio.file.LinkOption[] EMPTY_LINK_OPTIONS
      Empty array of type LinkOption.
      static java.nio.file.OpenOption[] EMPTY_OPEN_OPTIONS
      Empty array of type OpenOption.
      private static byte[] SKIP_BUFFER  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private IOUtils()
      Private constructor to prevent instantiation of this utility class.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void close​(java.io.Closeable closeable)
      Closes the given Closeable as a null-safe operation.
      static void closeQuietly​(java.io.Closeable closeable)
      Closes the given Closeable and swallows any IOException that may occur.
      static long copy​(java.io.InputStream input, java.io.OutputStream output)
      Copies the content of a InputStream into an OutputStream.
      static long copy​(java.io.InputStream input, java.io.OutputStream output, int bufferSize)
      Copies the content of a InputStream into an OutputStream
      static long copyRange​(java.io.InputStream input, long len, java.io.OutputStream output)
      Copies part of the content of a InputStream into an OutputStream.
      static long copyRange​(java.io.InputStream input, long len, java.io.OutputStream output, int bufferSize)
      Copies part of the content of a InputStream into an OutputStream
      static int readFully​(java.io.InputStream input, byte[] array)
      Reads as much from input as possible to fill the given array.
      static int readFully​(java.io.InputStream input, byte[] array, int offset, int length)
      Reads as much from input as possible to fill the given array with the given amount of bytes.
      static void readFully​(java.nio.channels.ReadableByteChannel channel, java.nio.ByteBuffer byteBuffer)
      Reads b.remaining() bytes from the given channel starting at the current channel's position.
      static byte[] readRange​(java.io.InputStream input, int length)
      Gets part of the contents of an InputStream as a byte[].
      static byte[] readRange​(java.nio.channels.ReadableByteChannel input, int length)
      Gets part of the contents of an ReadableByteChannel as a byte[].
      static long skip​(java.io.InputStream input, long toSkip)
      Skips bytes from an input byte stream.
      static byte[] toByteArray​(java.io.InputStream input)
      Gets the contents of an InputStream as a byte[].
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • DEFAULT_BUFFER_SIZE

        public static final int DEFAULT_BUFFER_SIZE
        The default buffer size (8192) to use in copy methods.
        Since:
        1.27.1-0
        See Also:
        Constant Field Values
      • EMPTY_OPEN_OPTIONS

        public static final java.nio.file.OpenOption[] EMPTY_OPEN_OPTIONS
        Empty array of type OpenOption.
        Since:
        1.27.1-0
      • EMPTY_LINK_OPTIONS

        public static final java.nio.file.LinkOption[] EMPTY_LINK_OPTIONS
        Empty array of type LinkOption.
        Since:
        1.21
      • SKIP_BUFFER

        private static final byte[] SKIP_BUFFER
    • Constructor Detail

      • IOUtils

        private IOUtils()
        Private constructor to prevent instantiation of this utility class.
    • Method Detail

      • close

        public static void close​(java.io.Closeable closeable)
                          throws java.io.IOException
        Closes the given Closeable as a null-safe operation.
        Parameters:
        closeable - The resource to close, may be null.
        Throws:
        java.io.IOException - if an I/O error occurs.
        Since:
        1.27.1-0
      • closeQuietly

        public static void closeQuietly​(java.io.Closeable closeable)
        Closes the given Closeable and swallows any IOException that may occur.
        Parameters:
        closeable - Closeable to close, can be null
        Since:
        1.7
      • copy

        public static long copy​(java.io.InputStream input,
                                java.io.OutputStream output)
                         throws java.io.IOException
        Copies the content of a InputStream into an OutputStream. Uses a default buffer size of 8192 bytes.
        Parameters:
        input - the InputStream to read.
        output - the OutputStream to write.
        Returns:
        the number of bytes copied.
        Throws:
        java.io.IOException - if an error occurs.
        java.lang.NullPointerException - if the input or output is null.
      • copy

        public static long copy​(java.io.InputStream input,
                                java.io.OutputStream output,
                                int bufferSize)
                         throws java.io.IOException
        Copies the content of a InputStream into an OutputStream
        Parameters:
        input - the InputStream to read.
        output - the OutputStream to write.
        bufferSize - the buffer size to use, must be bigger than 0.
        Returns:
        the number of bytes copied.
        Throws:
        java.io.IOException - if an error occurs.
        java.lang.NullPointerException - if the input or output is null.
      • copyRange

        public static long copyRange​(java.io.InputStream input,
                                     long len,
                                     java.io.OutputStream output)
                              throws java.io.IOException
        Copies part of the content of a InputStream into an OutputStream. Uses a default buffer size of 8024 bytes.
        Parameters:
        input - the InputStream to copy
        output - the target Stream
        len - maximum amount of bytes to copy
        Returns:
        the number of bytes copied
        Throws:
        java.io.IOException - if an error occurs
        Since:
        1.21
      • copyRange

        public static long copyRange​(java.io.InputStream input,
                                     long len,
                                     java.io.OutputStream output,
                                     int bufferSize)
                              throws java.io.IOException
        Copies part of the content of a InputStream into an OutputStream
        Parameters:
        input - the InputStream to copy
        len - maximum amount of bytes to copy
        output - the target, may be null to simulate output to dev/null on Linux and NUL on Windows
        bufferSize - the buffer size to use, must be bigger than 0
        Returns:
        the number of bytes copied
        Throws:
        java.io.IOException - if an error occurs
        java.lang.IllegalArgumentException - if bufferSize is smaller than or equal to 0
        Since:
        1.21
      • readFully

        public static int readFully​(java.io.InputStream input,
                                    byte[] array)
                             throws java.io.IOException
        Reads as much from input as possible to fill the given array.

        This method may invoke read repeatedly to fill the array and only read less bytes than the length of the array if the end of the stream has been reached.

        Parameters:
        input - stream to read from
        array - buffer to fill
        Returns:
        the number of bytes actually read
        Throws:
        java.io.IOException - on error
      • readFully

        public static int readFully​(java.io.InputStream input,
                                    byte[] array,
                                    int offset,
                                    int length)
                             throws java.io.IOException
        Reads as much from input as possible to fill the given array with the given amount of bytes.

        This method may invoke read repeatedly to read the bytes and only read less bytes than the requested length if the end of the stream has been reached.

        Parameters:
        input - stream to read from
        array - buffer to fill
        offset - offset into the buffer to start filling at
        length - of bytes to read
        Returns:
        the number of bytes actually read
        Throws:
        java.io.IOException - if an I/O error has occurred
      • readFully

        public static void readFully​(java.nio.channels.ReadableByteChannel channel,
                                     java.nio.ByteBuffer byteBuffer)
                              throws java.io.IOException
        Reads b.remaining() bytes from the given channel starting at the current channel's position.

        This method reads repeatedly from the channel until the requested number of bytes are read. This method blocks until the requested number of bytes are read, the end of the channel is detected, or an exception is thrown.

        Parameters:
        channel - the channel to read from
        byteBuffer - the buffer into which the data is read.
        Throws:
        java.io.IOException - if an I/O error occurs.
        java.io.EOFException - if the channel reaches the end before reading all the bytes.
      • readRange

        public static byte[] readRange​(java.io.InputStream input,
                                       int length)
                                throws java.io.IOException
        Gets part of the contents of an InputStream as a byte[].
        Parameters:
        input - the InputStream to read from
        length - maximum amount of bytes to copy
        Returns:
        the requested byte array
        Throws:
        java.lang.NullPointerException - if the input is null
        java.io.IOException - if an I/O error occurs
        Since:
        1.21
      • readRange

        public static byte[] readRange​(java.nio.channels.ReadableByteChannel input,
                                       int length)
                                throws java.io.IOException
        Gets part of the contents of an ReadableByteChannel as a byte[].
        Parameters:
        input - the ReadableByteChannel to read from
        length - maximum amount of bytes to copy
        Returns:
        the requested byte array
        Throws:
        java.lang.NullPointerException - if the input is null
        java.io.IOException - if an I/O error occurs
        Since:
        1.21
      • skip

        public static long skip​(java.io.InputStream input,
                                long toSkip)
                         throws java.io.IOException
        Skips bytes from an input byte stream.

        This method will only skip less than the requested number of bytes if the end of the input stream has been reached.

        Parameters:
        input - stream to skip bytes in
        toSkip - the number of bytes to skip
        Returns:
        the number of bytes actually skipped
        Throws:
        java.io.IOException - on error
      • toByteArray

        public static byte[] toByteArray​(java.io.InputStream input)
                                  throws java.io.IOException
        Gets the contents of an InputStream as a byte[].

        This method buffers the input internally, so there is no need to use a BufferedInputStream.

        Parameters:
        input - the InputStream to read from
        Returns:
        the requested byte array
        Throws:
        java.lang.NullPointerException - if the input is null
        java.io.IOException - if an I/O error occurs
        Since:
        1.5