b4ac6d2aead595b9e2a9421b6be6ebd60a117b4e
1 package be
.nikiroo
.utils
;
3 import java
.io
.Closeable
;
5 import java
.io
.IOException
;
8 * A small utility class to generate auto-delete temporary files in a
9 * centralised location.
13 public class TempFiles
implements Closeable
{
15 * Root directory of this instance, owned by it, where all temporary files
21 * Create a new {@link TempFiles} -- each instance is separate and have a
22 * dedicated sub-directory in a shared temporary root.
24 * The whole repository will be deleted on close (if you fail to call it,
25 * the program will <b>try</b> to call it on JVM termination).
28 * the instance name (will be <b>part</b> of the final directory
32 * in case of I/O error
34 public TempFiles(String name
) throws IOException
{
39 * Create a new {@link TempFiles} -- each instance is separate and have a
40 * dedicated sub-directory in a given temporary root.
42 * The whole repository will be deleted on close (if you fail to call it,
43 * the program will <b>try</b> to call it on JVM termination).
45 * Be careful, this instance will <b>own</b> the given root directory, and
46 * will most probably delete all its files.
49 * the root base directory to use for all the temporary files of
50 * this instance (if NULL, will be the default temporary
51 * directory of the OS)
53 * the instance name (will be <b>part</b> of the final directory
57 * in case of I/O error
59 public TempFiles(File base
, String name
) throws IOException
{
61 base
= File
.createTempFile(".temp", "");
66 IOUtils
.deltree(root
, true);
68 root
= new File(root
.getParentFile(), ".temp");
71 throw new IOException("Cannot create root directory: " + root
);
76 root
= createTempFile(name
);
77 IOUtils
.deltree(root
, true);
81 throw new IOException("Cannot create root subdirectory: " + root
);
86 * Create an auto-delete temporary file.
89 * a base for the final filename (only a <b>part</b> of said
92 * @return the newly created file
95 * in case of I/O errors
97 public synchronized File
createTempFile(String name
) throws IOException
{
99 while (name
.length() < 3) {
104 File tmp
= File
.createTempFile(name
, "");
105 IOUtils
.deltree(tmp
, true);
107 File test
= new File(root
, tmp
.getName());
108 if (!test
.exists()) {
109 test
.createNewFile();
110 if (!test
.exists()) {
111 throw new IOException("Cannot create temporary file: "
122 * Create an auto-delete temporary directory.
124 * Note that creating 2 temporary directories with the same name will result
125 * in two <b>different</b> directories, even if the final name is the same
126 * (the absolute path will be different).
129 * the actual directory name (not path)
131 * @return the newly created file
133 * @throws IOException
134 * in case of I/O errors, or if the name was a path instead of a
137 public synchronized File
createTempDir(String name
) throws IOException
{
138 File localRoot
= createTempFile(name
);
139 IOUtils
.deltree(localRoot
, true);
142 if (!localRoot
.exists()) {
143 throw new IOException("Cannot create subdirectory: " + localRoot
);
146 File dir
= new File(localRoot
, name
);
147 if (!dir
.getName().equals(name
)) {
148 throw new IOException(
149 "Cannot create temporary directory with a path, only names are allowed: "
157 throw new IOException("Cannot create subdirectory: " + dir
);
164 public synchronized void close() throws IOException
{
165 IOUtils
.deltree(root
); // NO exception here
166 root
.getParentFile().delete(); // only if empty
171 protected void finalize() throws Throwable
{