resolve darcs stupidity
[org.ibex.core.git] / src / org / ibex / Stream.java
index d200ce0..878afa3 100644 (file)
@@ -5,7 +5,7 @@ import java.io.*;
 import java.util.zip.*;
 import org.ibex.js.*;
 import org.ibex.util.*;
-import org.xwt.translators.MSPack;
+import org.ibex.translators.MSPack;
 
 /**
  *   Essentiall an InputStream "factory".  You can repeatedly ask a
@@ -21,8 +21,14 @@ public abstract class Stream extends JS.Cloneable {
     public static class NotCacheableException extends Exception { }
 
     // streams are "sealed" by default to prevent accidental object leakage
-    public void put(Object key, Object val) throws JSExn { }
-    public Object get(Object key) throws JSExn { return null; }
+    public void put(Object key, Object val) { }
+    private Cache getCache = new Cache(100);
+    protected Object _get(Object key) { return null; }
+    public final Object get(Object key) {
+        Object ret = getCache.get(key);
+        if (ret == null) getCache.put(key, ret = _get(key));
+        return ret;
+    }
 
     // Private Interface //////////////////////////////////////////////////////////////////////////////
 
@@ -34,7 +40,7 @@ public abstract class Stream extends JS.Cloneable {
         private String url;
         public String toString() { return "Stream.HTTP:" + url; }
         HTTP(String url) { while (url.endsWith("/")) url = url.substring(0, url.length() - 1); this.url = url; }
-        public Object get(Object key) throws JSExn { return new HTTP(url + "/" + (String)key); }
+        public Object _get(Object key) { return new HTTP(url + "/" + (String)key); }
         public String getCacheKey(Vec path) throws NotCacheableException { return url; }
         public InputStream getInputStream() throws IOException { return new org.ibex.HTTP(url).GET(); }
     }
@@ -56,7 +62,7 @@ public abstract class Stream extends JS.Cloneable {
         public String toString() { return "file:" + path; }
         public String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); /* already on disk */ }
         public InputStream getInputStream() throws IOException { return new FileInputStream(path); }
-        public Object get(Object key) throws JSExn { return new File(path + java.io.File.separatorChar + (String)key); }
+        public Object _get(Object key) { return new File(path + java.io.File.separatorChar + (String)key); }
     }
 
     /** "unwrap" a Zip archive */
@@ -70,7 +76,7 @@ public abstract class Stream extends JS.Cloneable {
             this.path = path;
         }
         public String getCacheKey() throws NotCacheableException { return parent.getCacheKey() + "!zip:"; }
-        public Object get(Object key) throws JSExn { return new Zip(parent, path==null?(String)key:path+'/'+(String)key); }
+        public Object _get(Object key) { return new Zip(parent, path==null?(String)key:path+'/'+(String)key); }
         public InputStream getInputStream() throws IOException {
             InputStream pis = parent.getInputStream();
             ZipInputStream zis = new ZipInputStream(pis);
@@ -88,13 +94,12 @@ public abstract class Stream extends JS.Cloneable {
         Cab(Stream parent) { this(parent, null); }
         Cab(Stream parent, String path) { this.parent = parent; this.path = path; }
         public String getCacheKey() throws NotCacheableException { return parent.getCacheKey() + "!cab:"; }
-        public Object get(Object key) throws JSExn { return new Cab(parent, path==null?(String)key:path+'/'+(String)key); }
+        public Object _get(Object key) { return new Cab(parent, path==null?(String)key:path+'/'+(String)key); }
         public InputStream getInputStream() throws IOException { return new MSPack(parent.getInputStream()).getInputStream(path); }
     }
 
     /** the Builtin resource */
     public static class Builtin extends Stream {
-        public Builtin() { };
         public String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); }
         public InputStream getInputStream() throws IOException { return Platform.getBuiltinInputStream(); }
     }
@@ -117,7 +122,7 @@ public abstract class Stream extends JS.Cloneable {
                     public int read(byte[] b, int off, int len) throws IOException {
                         int ret = super.read(b, off, len);
                         if (ret != 1) bytesDownloaded += ret;
-                        Scheduler.add(new Scheduler.Task() { public void perform() throws Exception {
+                        Scheduler.add(new Scheduler.Task() { public void perform() throws IOException, JSExn {
                             callback.call(N(bytesDownloaded),
                                           N(is instanceof KnownLength ? ((KnownLength)is).getLength() : 0), null, null, 2);
                         } });