Class IOUtils


  • public final class IOUtils
    extends java.lang.Object
    This class contains various I/O-related methods.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.io.IOException closeAndLogException​(java.io.Closeable closeable, org.apache.commons.logging.Log logger, java.lang.String resourceName, java.io.IOException initialException)
      Try to close an IO resource and log and return if there was an exception.
      static void closeQuietly​(java.io.Closeable closeable)
      Null safe close of the given Closeable suppressing any exception.
      static long copy​(java.io.InputStream input, java.io.OutputStream output)
      Copies all the contents from the given input stream to the given output stream.
      static RandomAccessStreamCache.StreamCacheCreateFunction createMemoryOnlyStreamCache()
      Provides a function to create an instance of a memory only StreamCache using unrestricted main memory.
      static java.nio.file.Path createProtectedTempDir()
      Creates a temporary directory in the default temporary-file directory with owner-only permissions and registers a shutdown hook to delete it on JVM exit.
      static java.nio.file.Path createProtectedTempFile​(java.nio.file.Path dir, java.lang.String prefix, java.lang.String suffix)
      Creates a temporary file in the specified directory (or default temporary-file directory if null) with owner-only permissions.
      static RandomAccessStreamCache.StreamCacheCreateFunction createTempFileOnlyStreamCache()
      Provides a function to create an instance of a temp file only StreamCache using unrestricted size.
      static long populateBuffer​(java.io.InputStream in, byte[] buffer)
      Populates the given buffer with data read from the input stream.
      static byte[] toByteArray​(java.io.InputStream in)
      Reads the input stream and returns its contents as a byte array.
      static void unmap​(java.nio.ByteBuffer buf)
      Unmap memory mapped byte buffers.
      • Methods inherited from class java.lang.Object

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

      • toByteArray

        public static byte[] toByteArray​(java.io.InputStream in)
                                  throws java.io.IOException
        Reads the input stream and returns its contents as a byte array.
        Parameters:
        in - the input stream to read from.
        Returns:
        the byte array
        Throws:
        java.io.IOException - if an I/O error occurs
      • copy

        public static long copy​(java.io.InputStream input,
                                java.io.OutputStream output)
                         throws java.io.IOException
        Copies all the contents from the given input stream to the given output stream.
        Parameters:
        input - the input stream
        output - the output stream
        Returns:
        the number of bytes that have been copied
        Throws:
        java.io.IOException - if an I/O error occurs
      • populateBuffer

        public static long populateBuffer​(java.io.InputStream in,
                                          byte[] buffer)
                                   throws java.io.IOException
        Populates the given buffer with data read from the input stream. If the data doesn't fit the buffer, only the data that fits in the buffer is read. If the data is less than fits in the buffer, the buffer is not completely filled.
        Parameters:
        in - the input stream to read from
        buffer - the buffer to fill
        Returns:
        the number of bytes written to the buffer
        Throws:
        java.io.IOException - if an I/O error occurs
      • closeQuietly

        public static void closeQuietly​(java.io.Closeable closeable)
        Null safe close of the given Closeable suppressing any exception.
        Parameters:
        closeable - to be closed
      • closeAndLogException

        public static java.io.IOException closeAndLogException​(java.io.Closeable closeable,
                                                               org.apache.commons.logging.Log logger,
                                                               java.lang.String resourceName,
                                                               java.io.IOException initialException)
        Try to close an IO resource and log and return if there was an exception.

        An exception is only returned if the IOException passed in is null.

        Parameters:
        closeable - to be closed
        logger - the logger to be used so that logging appears under that log instance
        resourceName - the name to appear in the log output
        initialException - if set, this exception will be returned even where there is another exception while closing the IO resource
        Returns:
        the IOException is there was any but only if initialException is null
      • unmap

        public static void unmap​(java.nio.ByteBuffer buf)
        Unmap memory mapped byte buffers. This is a hack waiting for a proper JVM provided solution mentioned in JDK-4724038: Add unmap method to MappedByteBuffer. The issue here is that even when closed, memory mapped byte buffers hold a lock on the underlying file until GC is executing and this in turns result in an error if the user tries to move or delete the file.
        Parameters:
        buf - the buffer to be unmapped
      • createMemoryOnlyStreamCache

        public static RandomAccessStreamCache.StreamCacheCreateFunction createMemoryOnlyStreamCache()
        Provides a function to create an instance of a memory only StreamCache using unrestricted main memory. RandomAccessReadWriteBuffer is used as current default implementation.
        Returns:
        a function to create an instance of a memory only StreamCache using unrestricted main memory
      • createTempFileOnlyStreamCache

        public static RandomAccessStreamCache.StreamCacheCreateFunction createTempFileOnlyStreamCache()
        Provides a function to create an instance of a temp file only StreamCache using unrestricted size. ScratchFile is used as current default implementation.
        Returns:
        a function to create an instance of a temp file only StreamCache using unrestricted size
      • createProtectedTempDir

        public static java.nio.file.Path createProtectedTempDir()
                                                         throws java.io.IOException
        Creates a temporary directory in the default temporary-file directory with owner-only permissions and registers a shutdown hook to delete it on JVM exit.

        Note: This method is designed to be used for storing temporary files that may contain sensitive data in a temporary directories with restricted permissions, to mitigate the risk of unauthorized access by other users or processes on the same system. Used e.g. by PDFDebugger.

        Returns:
        the path to the created temporary directory
        Throws:
        java.io.IOException
      • createProtectedTempFile

        public static java.nio.file.Path createProtectedTempFile​(java.nio.file.Path dir,
                                                                 java.lang.String prefix,
                                                                 java.lang.String suffix)
                                                          throws java.io.IOException
        Creates a temporary file in the specified directory (or default temporary-file directory if null) with owner-only permissions.

        This method attempts to set owner-only permissions at file creation time when supported, to minimize the time window during which the file may have default (world-readable) permissions. On POSIX systems (Linux, macOS), permissions are set during creation. On Windows, permissions are set after file creation via ACL.

        Note: This method is designed for storing temporary files that may contain sensitive data in a temporary directory with restricted permissions, to mitigate the risk of unauthorized access by other users or processes on the same system. However, unlike createProtectedTempDir(), this method does NOT automatically delete the file on JVM shutdown. The caller is responsible for deleting the temporary file when no longer needed. Used e.g. by PDFDebugger.

        Parameters:
        dir - the directory in which to create the temporary file, or null to use the default temporary-file directory
        prefix - the prefix string to be used in generating the file's name; may be null
        suffix - the suffix string to be used in generating the file's name; may be null
        Returns:
        the path to the created temporary file with owner-only permissions
        Throws:
        java.io.IOException - if an I/O error occurs during file creation or permission setting
        java.lang.SecurityException - if a security manager is installed and denies access
        See Also:
        createProtectedTempDir(), Files.createTempFile(Path, String, String, FileAttribute[])