Class TempFileManager


  • public final class TempFileManager
    extends java.lang.Object
    Management of temporary files in surefire with support for sub directories of the system's directory of temporary files.
    The File.createTempFile(String, String) API creates rather meaningless file names and only in the system's temp directory.
    This class creates temp files from a prefix, a unique date/timestamp, a short file id and suffix. It also helps you organize temporary files into sub-directories, and thus avoid bloating the temp directory root.
    Apart from that, this class works in much the same way as File.createTempFile(String, String) and File.deleteOnExit(), and can be used as a drop-in replacement.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.String baseName
      The fixed base name used between prefix and suffix of temporary files created by this class.
      private boolean deleteOnExit
      Temporary files are delete on JVM exit if true.
      private static java.util.concurrent.atomic.AtomicInteger FILE_ID
      An id unique across all file managers used as part of the temporary file's base name.
      private static java.util.Map<java.io.File,​TempFileManager> INSTANCES  
      private static java.lang.Thread shutdownHook  
      private static java.lang.String SUFFIX_TMP  
      private java.io.File tempDir
      The temporary directory or sub-directory under which temporary files are created.
      private java.util.List<java.io.File> tempFiles
      List of successfully created temporary files.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private TempFileManager​(java.io.File tempDir)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private static java.io.File calcTempDir​(java.lang.String subDirName)  
      java.io.File createTempFile​(java.lang.String prefix)  
      java.io.File createTempFile​(java.lang.String prefix, java.lang.String suffix)
      Creates a new, uniquely-named temporary file in this object's tempDir with user-defined prefix and suffix (both may be null or empty and won't be trimmed).
      void deleteAll()  
      static java.lang.String getJavaIoTmpDir()
      Returns the value of system property java.io.tmpdir, the system's temp directory.
      java.io.File getTempDir()
      Returns the temporary directory or sub-directory under which temporary files are created.
      static TempFileManager instance()  
      static TempFileManager instance​(java.io.File tempDir)  
      static TempFileManager instance​(java.lang.String subDirName)
      Creates an instance using a subdirectory of the system's temporary directory.
      boolean isDeleteOnExit()  
      void setDeleteOnExit​(boolean deleteOnExit)
      Instructs this file manager to delete its temporary files during JVM shutdown.
      This status can be turned on and off unlike File.deleteOnExit().
      (package private) static void shutdownAll()  
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

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

      • INSTANCES

        private static final java.util.Map<java.io.File,​TempFileManager> INSTANCES
      • FILE_ID

        private static final java.util.concurrent.atomic.AtomicInteger FILE_ID
        An id unique across all file managers used as part of the temporary file's base name.
      • shutdownHook

        private static java.lang.Thread shutdownHook
      • tempDir

        private final java.io.File tempDir
        The temporary directory or sub-directory under which temporary files are created.
      • baseName

        private final java.lang.String baseName
        The fixed base name used between prefix and suffix of temporary files created by this class.
      • tempFiles

        private final java.util.List<java.io.File> tempFiles
        List of successfully created temporary files.
      • deleteOnExit

        private boolean deleteOnExit
        Temporary files are delete on JVM exit if true.
    • Constructor Detail

      • TempFileManager

        private TempFileManager​(java.io.File tempDir)
    • Method Detail

      • calcTempDir

        private static java.io.File calcTempDir​(java.lang.String subDirName)
      • instance

        public static TempFileManager instance​(java.lang.String subDirName)
        Creates an instance using a subdirectory of the system's temporary directory.
        Parameters:
        subDirName - name of subdirectory
        Returns:
        instance
      • instance

        public static TempFileManager instance​(java.io.File tempDir)
      • deleteAll

        public void deleteAll()
      • shutdownAll

        static void shutdownAll()
      • getTempDir

        public java.io.File getTempDir()
        Returns the temporary directory or sub-directory under which temporary files are created.
        Returns:
        temporary directory
      • createTempFile

        public java.io.File createTempFile​(java.lang.String prefix,
                                           java.lang.String suffix)
        Creates a new, uniquely-named temporary file in this object's tempDir with user-defined prefix and suffix (both may be null or empty and won't be trimmed).

        This method behaves similarly to File.createTempFile(String, String) and may be used as a drop-in replacement.
        This method is synchronized to help ensure uniqueness of temporary files.

        Parameters:
        prefix - optional file name prefix
        suffix - optional file name suffix
        Returns:
        file object
      • createTempFile

        public java.io.File createTempFile​(java.lang.String prefix)
      • isDeleteOnExit

        public boolean isDeleteOnExit()
      • setDeleteOnExit

        public void setDeleteOnExit​(boolean deleteOnExit)
        Instructs this file manager to delete its temporary files during JVM shutdown.
        This status can be turned on and off unlike File.deleteOnExit().
        Parameters:
        deleteOnExit - delete the file in a shutdown hook on JVM exit
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • getJavaIoTmpDir

        public static java.lang.String getJavaIoTmpDir()
        Returns the value of system property java.io.tmpdir, the system's temp directory.
        Returns:
        path to system temp directory