Class TemporaryResources

  • All Implemented Interfaces:
    Closeable, AutoCloseable

    public class TemporaryResources
    extends Object
    implements Closeable
    Utility class for tracking and ultimately closing or otherwise disposing a collection of temporary resources.

    Note that this class is not thread-safe.

    Since:
    Apache Tika 0.10
    • Constructor Detail

      • TemporaryResources

        public TemporaryResources()
    • Method Detail

      • setTemporaryFileDirectory

        public void setTemporaryFileDirectory​(Path tempFileDir)
        Sets the directory to be used for the temporary files created by the createTempFile(String) method.
        Parameters:
        tempFileDir - temporary file directory, or null for the system default
      • setTemporaryFileDirectory

        public void setTemporaryFileDirectory​(File tempFileDir)
        Sets the directory to be used for the temporary files created by the createTempFile(String) method.
        Parameters:
        tempFileDir - temporary file directory, or null for the system default
        See Also:
        setTemporaryFileDirectory(Path)
      • createTempFile

        public Path createTempFile​(String suffix)
                            throws IOException
        Creates a temporary file that will automatically be deleted when the close() method is called, returning its path.
        Parameters:
        suffix - -- the suffix of the file if known, starting with "." as in ".pdf"
        Returns:
        Path to created temporary file that will be deleted after closing
        Throws:
        IOException
      • createTempFile

        public Path createTempFile​(Metadata metadata)
                            throws IOException
        Creates a temporary file that will automatically be deleted when the close() method is called, returning its path.
        Returns:
        Path to created temporary file that will be deleted after closing
        Throws:
        IOException
      • createTemporaryFile

        public File createTemporaryFile()
                                 throws IOException
        Creates and returns a temporary file that will automatically be deleted when the close() method is called.
        Returns:
        Created temporary file that'll be deleted after closing
        Throws:
        IOException
        See Also:
        createTempFile(String)
      • addResource

        public void addResource​(Closeable resource)
        Adds a new resource to the set of tracked resources that will all be closed when the close() method is called.
        Parameters:
        resource - resource to be tracked
      • getResource

        public <T extends Closeable> T getResource​(Class<T> klass)
        Returns the latest of the tracked resources that implements or extends the given interface or class.
        Parameters:
        klass - interface or class
        Returns:
        matching resource, or null if not found
      • close

        public void close()
                   throws IOException
        Closes all tracked resources. The resources are closed in reverse order from how they were added.

        Any suppressed exceptions from managed resources are collected and then added to the first thrown exception, which is re-thrown once all the resources have been closed.

        Specified by:
        close in interface AutoCloseable
        Specified by:
        close in interface Closeable
        Throws:
        IOException - if one or more of the tracked resources could not be closed