# nikiroo-utils
+## Version WIP
+
+- new: can now select the TempFiles root
+- new: can now select the Image temporary root
+- fix: Cache now offer some tracing when cleaning the cache
+
## Version 4.7.0
- tracer: traces on stderr by default
* @return the number of cleaned items
*/
private int clean(boolean onlyOld, File cacheDir) {
+ long ms = System.currentTimeMillis();
+
+ tracer.trace("Cleaning cache from old files...");
+
+ int num = doClean(onlyOld, cacheDir);
+
+ tracer.trace("Cache cleaned in " + (System.currentTimeMillis() - ms)
+ + " ms");
+
+ return num;
+ }
+
+ /**
+ * Actual work done for {@link Cache#clean(boolean, File)}.
+ *
+ * @param onlyOld
+ * only clean the files that are considered too old for stable
+ * resources
+ * @param cacheDir
+ * the cache directory to clean
+ *
+ * @return the number of cleaned items
+ */
+ private int doClean(boolean onlyOld, File cacheDir) {
int num = 0;
File[] files = cacheDir.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
- num += clean(onlyOld, file);
+ num += doClean(onlyOld, file);
} else {
if (!onlyOld || isOld(file, true)) {
if (file.delete()) {
* @author niki
*/
public class Image implements Closeable {
+ static private File tempRoot;
static private TempFiles tmpRepository;
static private long count = 0;
static private Object lock = new Object();
private File getTemporaryFile() throws IOException {
synchronized (lock) {
if (tmpRepository == null) {
- tmpRepository = new TempFiles("images");
+ tmpRepository = new TempFiles(tempRoot, "images");
count = 0;
}
return tmpRepository.createTempFile("image");
}
}
+
+ /**
+ * Change the temporary root directory used by the program.
+ * <p>
+ * Caution: the directory will be <b>owned</b> by the system, all its files
+ * now belong to us (and will most probably be deleted).
+ * <p>
+ * Note: it may take some time until the new temporary root is used, we
+ * first need to make sure the previous one is not used anymore (i.e., we
+ * must reach a point where no unclosed {@link Image} remains in memory) to
+ * switch the temporary root.
+ *
+ * @param root
+ * the new temporary root, which will be <b>owned</b> by the
+ * system
+ */
+ public static void setTemporaryFilesRoot(File root) {
+ tempRoot = root;
+ }
}
* @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", "");
+ 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;
+
IOUtils.deltree(root, true);
root = new File(root.getParentFile(), ".temp");