2003/09/30 21:30:49
[org.ibex.core.git] / src / org / xwt / Res.java
index 10a920b..cd0fc86 100644 (file)
@@ -48,7 +48,8 @@ public abstract class Res extends JS {
     public static Res stringToRes(String url) { return stringToRes(url, false); }
     public static Res stringToRes(String url, boolean permitLocalFilesystem) {
         if (url.indexOf('!') != -1)
-            return (Res)(new Zip(stringToRes(url.substring(0, url.lastIndexOf('!')))).get(url.substring(url.lastIndexOf('!') + 1)));
+            return (Res)(new Zip(stringToRes(url.substring(0, url.lastIndexOf('!')))).
+                         get(url.substring(url.lastIndexOf('!') + 1)));
         if (url.startsWith("http://")) return new HTTP(url);
         if (url.startsWith("https://")) return new HTTP(url);
         if (url.startsWith("file:") && permitLocalFilesystem) return new File(url.substring(5));
@@ -58,11 +59,26 @@ public abstract class Res extends JS {
         throw new JS.Exn("invalid resource specifier " + url);
     }
 
+    /** subclass from this if you want a CachedInputStream for each path */
+    public static abstract class CachedRes extends Res {
+        private Hash cachedInputStreams = new Hash();
+        abstract InputStream _getInputStream(String path) throws IOException;
+        public final InputStream getInputStream(String path) throws IOException {
+            CachedInputStream cis = (CachedInputStream)cachedInputStreams.get(path);
+            if (cis == null) {
+                cis = new CachedInputStream(_getInputStream(path));
+                cachedInputStreams.put(path, cis);
+            }
+            return cis.getInputStream();
+        }
+    }
+
     /** HTTP or HTTPS resource */
-    public static class HTTP extends Res {
+    public static class HTTP extends CachedRes {
         private String url;
         HTTP(String url) { this.url = url; }
-        public InputStream getInputStream(String path) throws IOException { return new org.xwt.HTTP(url + path).GET(); }
+        public InputStream _getInputStream(String path) throws IOException {
+            return new org.xwt.HTTP(url + path).GET(); }
     }
 
     /** byte arrays */