* @author niki
*/
public class TempFiles implements Closeable {
+ /**
+ * Root directory of this instance, owned by it, where all temporary files
+ * must reside.
+ */
protected File root;
/**
* dedicated sub-directory in a shared temporary root.
* <p>
* The whole repository will be deleted on close (if you fail to call it,
- * the program will <b>try</b> to all it on JVM termination).
+ * the program will <b>try</b> to call it on JVM termination).
*
* @param name
* the instance name (will be <b>part</b> of the final directory
* in case of I/O error
*/
public TempFiles(String name) throws IOException {
- root = File.createTempFile(".temp", "");
- IOUtils.deltree(root, true);
+ this(null, name);
+ }
+
+ /**
+ * Create a new {@link TempFiles} -- each instance is separate and have a
+ * dedicated sub-directory in a given temporary root.
+ * <p>
+ * The whole repository will be deleted on close (if you fail to call it,
+ * the program will <b>try</b> to call it on JVM termination).
+ * <p>
+ * Be careful, this instance will <b>own</b> the given root directory, and
+ * will most probably delete all its files.
+ *
+ * @param base
+ * the root base directory to use for all the temporary files of
+ * this instance (if NULL, will be the default temporary
+ * directory of the OS)
+ * @param name
+ * the instance name (will be <b>part</b> of the final directory
+ * name)
+ *
+ * @throws IOException
+ * in case of I/O error
+ */
+ public TempFiles(File base, String name) throws IOException {
+ if (base == null) {
+ base = File.createTempFile(".temp", "");
+ }
+
+ root = base;
+
+ if (root.exists()) {
+ IOUtils.deltree(root, true);
+ }
root = new File(root.getParentFile(), ".temp");
root.mkdir();
if (!test.exists()) {
test.createNewFile();
if (!test.exists()) {
- throw new IOException("Cannot create temporary file: "
- + test);
+ throw new IOException(
+ "Cannot create temporary file: " + test);
}
test.deleteOnExit();
@Override
public synchronized void close() throws IOException {
- IOUtils.deltree(root); // NO exception here
- root.getParentFile().delete(); // only if empty
- root = null;
+ File root = this.root;
+ this.root = null;
+
+ if (root != null) {
+ IOUtils.deltree(root);
+
+ // Since we allocate temp directories from a base point,
+ // try and remove that base point
+ root.getParentFile().delete(); // (only works if empty)
+ }
}
@Override