stream cleanup
[org.ibex.core.git] / src / org / ibex / js / Stream.java
index 878afa3..8ed5759 100644 (file)
@@ -1,11 +1,11 @@
 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
-package org.ibex;
+package org.ibex.js;
 
 import java.io.*;
 import java.util.zip.*;
-import org.ibex.js.*;
 import org.ibex.util.*;
-import org.ibex.translators.MSPack;
+import org.ibex.plat.*;
+import org.ibex.net.*;
 
 /**
  *   Essentiall an InputStream "factory".  You can repeatedly ask a
@@ -13,70 +13,73 @@ import org.ibex.translators.MSPack;
  *   be totally independent of the others (ie separate stream position
  *   and state) although they draw from the same data source.
  */
-public abstract class Stream extends JS.Cloneable {
+public abstract class Stream extends JS implements JS.Cloneable {
 
     // Public Interface //////////////////////////////////////////////////////////////////////////////
 
-    public static InputStream getInputStream(Object js) throws IOException { return ((Stream)((JS)js).unclone()).getInputStream();}
+    /*public static InputStream getInputStream(JS js) throws IOException { return ((Stream)js.unclone()).getInputStream();}*/
     public static class NotCacheableException extends Exception { }
 
-    // streams are "sealed" by default to prevent accidental object leakage
-    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));
+    public abstract JS _get(String key);
+    public final JS get(JS key) throws JSExn {
+        JS ret = (JS) getCache.get(key);
+        if (ret == null) getCache.put(key, ret = _get(JS.toString(key)));
         return ret;
     }
 
     // Private Interface //////////////////////////////////////////////////////////////////////////////
 
-    protected abstract InputStream getInputStream() throws IOException;
+    static String getCacheKey(JS s) throws NotCacheableException {
+        if(s instanceof Stream) return ((Stream)s).getCacheKey();
+        throw new NotCacheableException();
+    }
+    
+    public abstract InputStream getInputStream() throws IOException;
     protected String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); }
 
     /** HTTP or HTTPS resource */
+    // FEATURE: Only instansiate only ibex.net.HTTP, share with all substreams
     public static class HTTP extends Stream {
         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) { return new HTTP(url + "/" + (String)key); }
+        public HTTP(String url) { while (url.endsWith("/")) url = url.substring(0, url.length() - 1); this.url = url; }
+        public JS _get(String key) { return new HTTP(url + "/" + key); }
         public String getCacheKey(Vec path) throws NotCacheableException { return url; }
-        public InputStream getInputStream() throws IOException { return new org.ibex.HTTP(url).GET(); }
+        public InputStream getInputStream() throws IOException { return new org.ibex.net.HTTP(url).GET(); }
     }
 
     /** byte arrays */
     public static class ByteArray extends Stream {
         private byte[] bytes;
         private String cacheKey;
-        ByteArray(byte[] bytes, String cacheKey) { this.bytes = bytes; this.cacheKey = cacheKey; }
+        public ByteArray(byte[] bytes, String cacheKey) { this.bytes = bytes; this.cacheKey = cacheKey; }
         public String getCacheKey() throws NotCacheableException {
             if (cacheKey == null) throw new NotCacheableException(); return cacheKey; }
         public InputStream getInputStream() throws IOException { return new ByteArrayInputStream(bytes); }
+        public JS _get(String key) { return null; }
     }
 
     /** a file */
     public static class File extends Stream {
         private String path;
-        File(String path) { this.path = path; }
-        public String toString() { return "file:" + path; }
+        public File(String path) { this.path = 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) { return new File(path + java.io.File.separatorChar + (String)key); }
+        public JS _get(String key) { return new File(path + java.io.File.separatorChar + key); }
     }
 
     /** "unwrap" a Zip archive */
     public static class Zip extends Stream {
-        private Stream parent;
+        private JS parent;
         private String path;
-        Zip(Stream parent) { this(parent, null); }
-        Zip(Stream parent, String path) {
+        public Zip(JS parent) { this(parent, null); }
+        public Zip(JS parent, String path) {
             while(path != null && path.startsWith("/")) path = path.substring(1);
             this.parent = parent;
             this.path = path;
         }
-        public String getCacheKey() throws NotCacheableException { return parent.getCacheKey() + "!zip:"; }
-        public Object _get(Object key) { return new Zip(parent, path==null?(String)key:path+'/'+(String)key); }
+        public String getCacheKey() throws NotCacheableException { return getCacheKey(parent) + "!zip:"; }
+        public JS _get(String key) { return new Zip(parent, path==null?key:path+'/'+key); }
         public InputStream getInputStream() throws IOException {
             InputStream pis = parent.getInputStream();
             ZipInputStream zis = new ZipInputStream(pis);
@@ -89,12 +92,12 @@ public abstract class Stream extends JS.Cloneable {
 
     /** "unwrap" a Cab archive */
     public static class Cab extends Stream {
-        private Stream parent;
+        private JS parent;
         private String path;
-        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) { return new Cab(parent, path==null?(String)key:path+'/'+(String)key); }
+        public Cab(JS parent) { this(parent, null); }
+        public Cab(JS parent, String path) { this.parent = parent; this.path = path; }
+        public String getCacheKey() throws NotCacheableException { return getCacheKey(parent) + "!cab:"; }
+        public JS _get(String key) { return new Cab(parent, path==null?key:path+'/'+key); }
         public InputStream getInputStream() throws IOException { return new MSPack(parent.getInputStream()).getInputStream(path); }
     }
 
@@ -102,14 +105,15 @@ public abstract class Stream extends JS.Cloneable {
     public static class Builtin extends Stream {
         public String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); }
         public InputStream getInputStream() throws IOException { return Platform.getBuiltinInputStream(); }
+        public JS _get(String key) { return null; }
     }
 
     /** shadow resource which replaces the graft */
     public static class ProgressWatcher extends Stream {
-        final Stream watchee;
+        final JS watchee;
         JS callback;
-        ProgressWatcher(Stream watchee, JS callback) { this.watchee = watchee; this.callback = callback; }
-        public String getCacheKey() throws NotCacheableException { return watchee.getCacheKey(); }
+        public ProgressWatcher(JS watchee, JS callback) { this.watchee = watchee; this.callback = callback; }
+        public String getCacheKey() throws NotCacheableException { return getCacheKey(watchee); }
         public InputStream getInputStream() throws IOException {
             final InputStream is = watchee.getInputStream();
             return new FilterInputStream(is) {
@@ -122,7 +126,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 IOException, JSExn {
+                        Scheduler.add(new Task() { public void perform() throws IOException, JSExn {
                             callback.call(N(bytesDownloaded),
                                           N(is instanceof KnownLength ? ((KnownLength)is).getLength() : 0), null, null, 2);
                         } });
@@ -130,28 +134,32 @@ public abstract class Stream extends JS.Cloneable {
                     }
                 };
         }
+        public JS _get(String s) { return null; }
     }
 
     /** subclass from this if you want a CachedInputStream for each path */
     public static class CachedStream extends Stream {
-        private Stream parent;
+        private JS parent;
         private boolean disk = false;
         private String key;
+        private String s;
         public String getCacheKey() throws NotCacheableException { return key; }
         CachedInputStream cis = null;
-        public CachedStream(Stream p, String s, boolean d) throws NotCacheableException {
-            this.parent = p; this.disk = d; this.key = p.getCacheKey();
+        public CachedStream(JS p, String s, boolean d) throws NotCacheableException {
+            this.parent = p; this.s = s; this.disk = d; this.key = getCacheKey(p);
         }
         public InputStream getInputStream() throws IOException {
             if (cis != null) return cis.getInputStream();
             if (!disk) {
                 cis = new CachedInputStream(parent.getInputStream());
             } else {
-                java.io.File f = LocalStorage.Cache.getCacheFileForKey(key);
+                // FEATURE: Move LocalStorage into org.ibex.js or move this out
+                java.io.File f = org.ibex.core.LocalStorage.Cache.getCacheFileForKey(key);
                 if (f.exists()) return new FileInputStream(f);
                 cis = new CachedInputStream(parent.getInputStream(), f);
             }
             return cis.getInputStream();
         }
+        public JS _get(String s) { return null; }
     }
 }