2003/12/22 05:14:36
authormegacz <megacz@xwt.org>
Fri, 30 Jan 2004 07:43:08 +0000 (07:43 +0000)
committermegacz <megacz@xwt.org>
Fri, 30 Jan 2004 07:43:08 +0000 (07:43 +0000)
darcs-hash:20040130074308-2ba56-2d9d550b5a4c6dd6cec2d55dbbb710d72bc47ecb.gz

src/org/xwt/LocalStorage.java [new file with mode: 0644]
src/org/xwt/Res.java

diff --git a/src/org/xwt/LocalStorage.java b/src/org/xwt/LocalStorage.java
new file mode 100644 (file)
index 0000000..a33166f
--- /dev/null
@@ -0,0 +1,58 @@
+// Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
+package org.xwt;
+
+import java.io.*;
+import java.util.*;
+import java.util.zip.*;
+import org.xwt.js.*;
+import org.xwt.util.*;
+import org.bouncycastle.util.encoders.Base64;
+
+/** Manages access to ~/.xwt */
+public class LocalStorage {
+
+    static String xwtDirName = System.getProperty("user.home") + java.io.File.separatorChar + ".xwt";
+
+    static File xwtDir = null;
+    static File cacheDir = null;
+
+    static {
+        try {
+            xwtDir = new File(xwtDirName);
+            if (!xwtDir.mkdirs()) xwtDir = null;
+        } catch (Exception e) {
+            Log.log(LocalStorage.class, "unable to create xwt directory " + xwtDirName);
+        }
+        try {
+            cacheDir = new File(xwtDirName + java.io.File.separatorChar + "cache");
+            if (!cacheDir.mkdirs()) cacheDir = null;
+        } catch (Exception e) {
+            Log.log(LocalStorage.class, "unable to create cache directory " + xwtDirName + java.io.File.separatorChar + "cache");
+        }
+    }
+
+
+    // FEATURE: we ought to be able to do stuff like sha1-checking and date checking on cached resources    
+    public static class Cache {
+
+        private static void delTree(File f) throws IOException {
+            if (f.isDirectory()) {
+                String[] s = f.list();
+                for(int i=0; i<s.length; i++)
+                    delTree(new File(f.getPath() + File.separatorChar + s[i]));
+            }
+            f.delete();
+        }
+
+        public static void flush() throws IOException {
+            delTree(cacheDir);
+            cacheDir.mkdirs();
+        }
+
+        public static File getCacheFileForKey(String key) {
+            // FEATURE: be smarter here
+            return new File(cacheDir.getPath() + File.separatorChar + new String(Base64.encode(key.getBytes())));
+        }
+
+    }
+}
index 5639080..32f812c 100644 (file)
@@ -1,4 +1,4 @@
-// FIXME
+// FIXEME
 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
 package org.xwt;
 
@@ -78,18 +78,13 @@ public abstract class Res extends JS {
         public InputStream getInputStream(String path) throws IOException {
             CachedInputStream cis = (CachedInputStream)cachedInputStreams.get(path);
             if (cis == null) {
-                java.io.File f = null;
                 if (disk) {
-                    f = new java.io.File(System.getProperty("user.home") +
-                                         java.io.File.separatorChar + ".xwt" +
-                                         java.io.File.separatorChar + "caches" +
-                                         java.io.File.separatorChar +
-                                         new String(Base64.encode(key.getBytes())));
-                    Log.log(this, "caching resource in " + f);
-                    new java.io.File(f.getParent()).mkdirs();
+                    java.io.File f = LocalStorage.Cache.getCacheFileForKey(key);
                     if (f.exists()) return new FileInputStream(f);
+                    cis = new CachedInputStream(parent.getInputStream(path), f);
+                } else {
+                    cis = new CachedInputStream(parent.getInputStream(path));
                 }
-                cis = new CachedInputStream(parent.getInputStream(path), f);
                 cachedInputStreams.put(path, cis);
             }
             return cis.getInputStream();