new js api
[org.ibex.core.git] / src / org / ibex / js / Stream.java
index 72ee0a0..2344e76 100644 (file)
@@ -17,15 +17,13 @@ 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);
+    protected JS _get(Object key) { return null; }
+    public final JS get(JS key) {
+        JS ret = (JS) getCache.get(key);
         if (ret == null) getCache.put(key, ret = _get(key));
         return ret;
     }
@@ -36,11 +34,12 @@ public abstract class Stream extends JS implements JS.Cloneable {
     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; }
+        public String coerceToString() { return "Stream.HTTP:" + url; }
         public 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 JS _get(JS key) throws JSExn { return new HTTP(url + "/" + JS.toString(key)); }
         public String getCacheKey(Vec path) throws NotCacheableException { return url; }
         public InputStream getInputStream() throws IOException { return new org.ibex.net.HTTP(url).GET(); }
     }
@@ -59,10 +58,10 @@ public abstract class Stream extends JS implements JS.Cloneable {
     public static class File extends Stream {
         private String path;
         public File(String path) { this.path = path; }
-        public String toString() { return "file:" + path; }
+        public String coerceToString() { 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) { return new File(path + java.io.File.separatorChar + (String)key); }
+        public JS _get(JS key) throws JSExn { return new File(path + java.io.File.separatorChar + JS.toString(key)); }
     }
 
     /** "unwrap" a Zip archive */
@@ -76,7 +75,7 @@ public abstract class Stream extends JS implements JS.Cloneable {
             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 JS _get(JS key) throws JSExn { return new Zip(parent, path==null?JS.toString(key):path+'/'+JS.toString(key)); }
         public InputStream getInputStream() throws IOException {
             InputStream pis = parent.getInputStream();
             ZipInputStream zis = new ZipInputStream(pis);
@@ -94,7 +93,7 @@ public abstract class Stream extends JS implements JS.Cloneable {
         public Cab(Stream parent) { this(parent, null); }
         public 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 JS _get(JS key) throws JSExn { return new Cab(parent, path==null?JS.toString(key):path+'/'+JS.toString(key)); }
         public InputStream getInputStream() throws IOException { return new MSPack(parent.getInputStream()).getInputStream(path); }
     }