- 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 Summary
Constructors Constructor Description TempFileContext()
Uses the system default temporary directory fromsystem property
"java.io.tmpdir"
.TempFileContext(File tmpDir)
Create a new instance of the temp file manager.TempFileContext(String tmpDir)
Uses the provided temporary directory.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Closes this instance.TempFile
createTempDirectory()
Creates a new temporary directory with default prefix, recursively deleting on close or exit.TempFile
createTempDirectory(String prefix)
Creates a new temporary directory with the given prefix, recursively deleting on close or exit.TempFile
createTempFile()
Creates a new temporary file with default prefix and suffix, deleting on close or exit.TempFile
createTempFile(String name)
Creates a new temporary file based on the given name, deleting on close or exit.TempFile
createTempFile(String prefix, String suffix)
Creates a new temporary file with the given prefix and suffix, deleting on close or exit.static String
generatePrefix(String template)
Generates a temp file prefix given a name template.int
getSize()
Gets the number of files that are currently scheduled to be deleted on close/exit.File
getTmpDir()
Gets the temporary directory this instance is using.
-
-
-
Constructor Detail
-
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 asServlet.destroy()
.Shutdown hooks are shared between instances.
- Parameters:
tmpDir
- The temporary directory ornull
to use the system default- See Also:
close()
-
TempFileContext
public TempFileContext(String tmpDir)
Uses the provided temporary directory.- Parameters:
tmpDir
- The temporary directory ornull
to use the system default- See Also:
TempFileContext(java.io.File)
-
TempFileContext
public TempFileContext()
Uses the system default temporary directory fromsystem property
"java.io.tmpdir"
.- See Also:
getSystemTmpDir()
,TempFileContext(java.io.File)
-
-
Method Detail
-
getTmpDir
public File getTmpDir()
Gets the temporary directory this instance is using.
-
generatePrefix
public static String generatePrefix(String template)
Generates a temp file prefix given a name template.- If
null
orString.isEmpty()
,"tmp_"
is used. - Any character that is not in
[a-zA-Z0-9.-_]
is converted to'_'
. - If less than
MIN_PREFIX_LENGTH
, padded with trailing'_'
to a length ofMIN_PREFIX_LENGTH
. - If greater than
MAX_PREFIX_LENGTH
characters, is truncated to a length ofMAX_PREFIX_LENGTH
.
- Parameters:
template
- the name of the temporary file that will be filtered for only safe characters
- If
-
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
- Ifnull
orString.isEmpty()
,"tmp_"
is used. If less thanMIN_PREFIX_LENGTH
, padded with trailing'_'
to a length ofMIN_PREFIX_LENGTH
. If greater thanMAX_PREFIX_LENGTH
characters, is truncated to a length ofMAX_PREFIX_LENGTH
.- Throws:
IllegalStateException
- if alreadyclosed
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 alreadyclosed
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
- Ifnull
orString.isEmpty()
,"tmp_"
is used. If less thanMIN_PREFIX_LENGTH
, padded with trailing'_'
to a length ofMIN_PREFIX_LENGTH
. If greater thanMAX_PREFIX_LENGTH
characters, is truncated to a length ofMAX_PREFIX_LENGTH
.suffix
- whennull
,".tmp"
is used.- Throws:
IllegalStateException
- if alreadyclosed
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 alreadyclosed
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 alreadyclosed
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 interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-
-