X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2FTempFiles.java;h=865fbce765c474b30e86362ae36a2343e745eb7e;hb=223aa0d4e40e5f3ba9f7b9a7a2165aaff4bf7494;hp=a16e9e40c50e152bb6ef7e31d0ba59d0207b1041;hpb=59864f77f0b8d5a57f479152a3131093544af6b2;p=fanfix.git diff --git a/src/be/nikiroo/utils/TempFiles.java b/src/be/nikiroo/utils/TempFiles.java index a16e9e4..865fbce 100644 --- a/src/be/nikiroo/utils/TempFiles.java +++ b/src/be/nikiroo/utils/TempFiles.java @@ -11,6 +11,10 @@ import java.io.IOException; * @author niki */ public class TempFiles implements Closeable { + /** + * Root directory of this instance, owned by it, where all temporary files + * must reside. + */ protected File root; /** @@ -18,7 +22,7 @@ public class TempFiles implements Closeable { * dedicated sub-directory in a shared temporary root. *

* The whole repository will be deleted on close (if you fail to call it, - * the program will try to all it on JVM termination). + * the program will try to call it on JVM termination). * * @param name * the instance name (will be part of the final directory @@ -28,8 +32,40 @@ public class TempFiles implements Closeable { * 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. + *

+ * The whole repository will be deleted on close (if you fail to call it, + * the program will try to call it on JVM termination). + *

+ * Be careful, this instance will own 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 part 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(); @@ -130,6 +166,7 @@ public class TempFiles implements Closeable { public synchronized void close() throws IOException { IOUtils.deltree(root); // NO exception here root.getParentFile().delete(); // only if empty + root = null; } @Override