- All Implemented Interfaces:
Closeable
,AutoCloseable
Thread-safe with fine-grained locking.
-
Constructor Summary
ConstructorsConstructorDescriptionUses 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
Modifier and TypeMethodDescriptionvoid
close()
Closes this instance.Creates a new temporary directory with default prefix, recursively deleting on close or exit.createTempDirectory
(String prefix) Creates a new temporary directory with the given prefix, recursively deleting on close or exit.Creates a new temporary file with default prefix and suffix, deleting on close or exit.createTempFile
(String name) Creates a new temporary file based on the given name, deleting on close or exit.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.Gets the temporary directory this instance is using, which may benull
when using the system directory.
-
Constructor Details
-
TempFileContext
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:
-
TempFileContext
Uses the provided temporary directory.- Parameters:
tmpDir
- The temporary directory ornull
to use the system default- See Also:
-
TempFileContext
public TempFileContext()Uses the system default temporary directory fromsystem property
"java.io.tmpdir"
.- See Also:
-
getSystemTmpDir()
TempFileContext(java.io.File)
-
-
Method Details
-
getTmpDir
Gets the temporary directory this instance is using, which may benull
when using the system directory. -
generatePrefix
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
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
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
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
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
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
-