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", "");
67 IOUtils
.deltree(root
, true);
70 root
= new File(root
.getParentFile(), ".temp");
73 throw new IOException("Cannot create root directory: " + root
);
78 root
= createTempFile(name
);
79 IOUtils
.deltree(root
, true);
83 throw new IOException("Cannot create root subdirectory: " + root
);
88 * Create an auto-delete temporary file.
91 * a base for the final filename (only a <b>part</b> of said
94 * @return the newly created file
97 * in case of I/O errors
99 public synchronized File
createTempFile(String name
) throws IOException
{
101 while (name
.length() < 3) {
106 File tmp
= File
.createTempFile(name
, "");
107 IOUtils
.deltree(tmp
, true);
109 File test
= new File(root
, tmp
.getName());
110 if (!test
.exists()) {
111 test
.createNewFile();
112 if (!test
.exists()) {
113 throw new IOException(
114 "Cannot create temporary file: " + test
);
124 * Create an auto-delete temporary directory.
126 * Note that creating 2 temporary directories with the same name will result
127 * in two <b>different</b> directories, even if the final name is the same
128 * (the absolute path will be different).
131 * the actual directory name (not path)
133 * @return the newly created file
135 * @throws IOException
136 * in case of I/O errors, or if the name was a path instead of a
139 public synchronized File
createTempDir(String name
) throws IOException
{
140 File localRoot
= createTempFile(name
);
141 IOUtils
.deltree(localRoot
, true);
144 if (!localRoot
.exists()) {
145 throw new IOException("Cannot create subdirectory: " + localRoot
);
148 File dir
= new File(localRoot
, name
);
149 if (!dir
.getName().equals(name
)) {
150 throw new IOException(
151 "Cannot create temporary directory with a path, only names are allowed: "
159 throw new IOException("Cannot create subdirectory: " + dir
);
166 public synchronized void close() throws IOException
{
167 File root
= this.root
;
171 IOUtils
.deltree(root
);
173 // Since we allocate temp directories from a base point,
174 // try and remove that base point
175 root
.getParentFile().delete(); // (only works if empty)
180 protected void finalize() throws Throwable
{