X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2FTempFiles.java;fp=src%2Fbe%2Fnikiroo%2Futils%2FTempFiles.java;h=b4ac6d2aead595b9e2a9421b6be6ebd60a117b4e;hb=82fcfcde9722e9e3b3eb713cf1f2bcb35fefaa7e;hp=186bfb8938ad42ba9300abb80fb3c6180ab30b9c;hpb=ce3972ea1a7083d1f8ef17463fb48c5bfa7ebf73;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/TempFiles.java b/src/be/nikiroo/utils/TempFiles.java index 186bfb8..b4ac6d2 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,7 +32,37 @@ public class TempFiles implements Closeable { * in case of I/O error */ public TempFiles(String name) throws IOException { - root = File.createTempFile(".temp", ""); + 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; + IOUtils.deltree(root, true); root = new File(root.getParentFile(), ".temp");