Class TempFileContext

java.lang.Object
com.aoapps.tempfiles.TempFileContext
All Implemented Interfaces:
Closeable, AutoCloseable

public class TempFileContext extends Object implements Closeable
Provides a central temporary file manager for use by any number of projects. The temporary files are optionally deleted on shutdown using shutdown hooks. The shutdown-registered temporary files are also immediately deleted when the last instance is closed.

Thread-safe with fine-grained locking.

  • Constructor Details

    • TempFileContext

      public TempFileContext(File tmpDir)

      Create a new instance of the temp file manager. close() must be called when done with the instance. This should be done in a try-with-resources, try-finally, or strong equivalent, such as Servlet.destroy().

      Shutdown hooks are shared between instances.

      Parameters:
      tmpDir - The temporary directory or null to use the system default
      See Also:
    • TempFileContext

      public TempFileContext(String tmpDir)
      Uses the provided temporary directory.
      Parameters:
      tmpDir - The temporary directory or null to use the system default
      See Also:
    • TempFileContext

      public TempFileContext()
      Uses the system default temporary directory from system property "java.io.tmpdir".
      See Also:
  • Method Details

    • getTmpDir

      public File getTmpDir()
      Gets the temporary directory this instance is using, which may be null when using the system directory.
    • generatePrefix

      public static String generatePrefix(String template)
      Generates a temp file prefix given a name template.
      1. If null or String.isEmpty(), "tmp_" is used.
      2. Any character that is not in [a-zA-Z0-9.-_] is converted to '_'.
      3. If less than MIN_PREFIX_LENGTH, padded with trailing '_' to a length of MIN_PREFIX_LENGTH.
      4. If greater than MAX_PREFIX_LENGTH characters, is truncated to a length of MAX_PREFIX_LENGTH.
      Parameters:
      template - the name of the temporary file that will be filtered for only safe characters
    • createTempDirectory

      public TempFile createTempDirectory(String prefix) throws IllegalStateException, IOException
      Creates a new temporary directory with the given prefix, recursively deleting on close or exit.
      Parameters:
      prefix - If null or String.isEmpty(), "tmp_" is used. If less than MIN_PREFIX_LENGTH, padded with trailing '_' to a length of MIN_PREFIX_LENGTH. If greater than MAX_PREFIX_LENGTH characters, is truncated to a length of MAX_PREFIX_LENGTH.
      Throws:
      IllegalStateException - if already closed
      IOException
    • createTempDirectory

      public TempFile createTempDirectory() throws IllegalStateException, IOException
      Creates a new temporary directory with default prefix, recursively deleting on close or exit.
      Throws:
      IllegalStateException - if already closed
      IOException
    • createTempFile

      public TempFile createTempFile(String prefix, String suffix) throws IllegalStateException, IOException
      Creates a new temporary file with the given prefix and suffix, deleting on close or exit.
      Parameters:
      prefix - If null or String.isEmpty(), "tmp_" is used. If less than MIN_PREFIX_LENGTH, padded with trailing '_' to a length of MIN_PREFIX_LENGTH. If greater than MAX_PREFIX_LENGTH characters, is truncated to a length of MAX_PREFIX_LENGTH.
      suffix - when null, ".tmp" is used.
      Throws:
      IllegalStateException - if already closed
      IOException
    • createTempFile

      public TempFile createTempFile(String name) throws IllegalStateException, IOException
      Creates a new temporary file based on the given name, deleting on close or exit.

      When the file contains a dot ('.') followed by only the characters in [a-zA-Z0-9_], and the dot is not the first character, it is used as the temp file suffix (to maintain file type by extension). Multiple extensions are supported, such as used by "filename.tar.gz".

      When the suffix is removed from the name, the prefix is followed by an underscore ('_') separator.

      When no automatic suffix is determine, defaults to ".tmp".

      Throws:
      IllegalStateException - if already closed
      IOException
    • createTempFile

      public TempFile createTempFile() throws IllegalStateException, IOException
      Creates a new temporary file with default prefix and suffix, deleting on close or exit.
      Throws:
      IllegalStateException - if already closed
      IOException
    • getSize

      public int getSize()
      Gets the number of files that are currently scheduled to be deleted on close/exit.
    • close

      public void close() throws IOException

      Closes this instance. Once closed, no additional temp files may be managed. Any overriding method must call super.close().

      If this is the last active instance, the underlying shutdown hook is also removed.

      If already closed, no action will be taken and no exception thrown.

      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException