Improve Downloader and Cache:
[nikiroo-utils.git] / src / be / nikiroo / utils / Cache.java
index bba88bb4b48f7e970039002393a72a47d50081cf..4750ef844f1e6593ab1f90ebd76273ddaee77497 100644 (file)
@@ -47,8 +47,8 @@ public class Cache {
        public Cache(File dir, int hoursChanging, int hoursStable)
                        throws IOException {
                this.dir = dir;
-               this.tooOldChanging = 1000 * 60 * 60 * hoursChanging;
-               this.tooOldStable = 1000 * 60 * 60 * hoursStable;
+               this.tooOldChanging = 1000L * 60 * 60 * hoursChanging;
+               this.tooOldStable = 1000L * 60 * 60 * hoursStable;
 
                if (dir != null && !dir.exists()) {
                        dir.mkdirs();
@@ -174,16 +174,19 @@ public class Cache {
         */
        private int clean(boolean onlyOld, File cacheDir) {
                int num = 0;
-               for (File file : cacheDir.listFiles()) {
-                       if (file.isDirectory()) {
-                               num += clean(onlyOld, file);
-                       } else {
-                               if (!onlyOld || isOld(file, true)) {
-                                       if (file.delete()) {
-                                               num++;
-                                       } else {
-                                               tracer.error("Cannot delete temporary file: "
-                                                               + file.getAbsolutePath());
+               File[] files = cacheDir.listFiles();
+               if (files != null) {
+                       for (File file : files) {
+                               if (file.isDirectory()) {
+                                       num += clean(onlyOld, file);
+                               } else {
+                                       if (!onlyOld || isOld(file, true)) {
+                                               if (file.delete()) {
+                                                       num++;
+                                               } else {
+                                                       tracer.error("Cannot delete temporary file: "
+                                                                       + file.getAbsolutePath());
+                                               }
                                        }
                                }
                        }
@@ -217,8 +220,9 @@ public class Cache {
         * @param allowTooOld
         *            allow files even if they are considered too old
         * @param stable
-        *            a stable file (that dones't change too often) -- parameter
-        *            used to check if the file is too old to keep or not
+        *            a stable file (that doesn't change too often) -- parameter
+        *            used to check if the file is too old to keep or not in the
+        *            cache
         * 
         * @return the opened resource if found, NULL if not
         */
@@ -287,6 +291,8 @@ public class Cache {
 
        /**
         * Save the given resource to the cache.
+        * <p>
+        * Will also clean the {@link Cache} from old files.
         * 
         * @param in
         *            the input data
@@ -297,6 +303,7 @@ public class Cache {
         *             in case of I/O error
         */
        private void save(InputStream in, File cached) throws IOException {
+               clean(true);
                IOUtils.write(in, cached);
        }