X-Git-Url: http://git.megacz.com/?p=org.ibex.core.git;a=blobdiff_plain;f=src%2Forg%2Fibex%2Fjs%2FStream.java;h=8ed5759a08ecbe1b4c8706aa35c7f54192e69e70;hp=e69c28c050738f1c24682f6544cab59b4939cc08;hb=e1a626a6cccf4846b90c98821f597429bf095d71;hpb=9bff7ae2a6824ac83fb1e0ccd455ac3b1277ef3c diff --git a/src/org/ibex/js/Stream.java b/src/org/ibex/js/Stream.java index e69c28c..8ed5759 100644 --- a/src/org/ibex/js/Stream.java +++ b/src/org/ibex/js/Stream.java @@ -17,21 +17,24 @@ public abstract class Stream extends JS implements JS.Cloneable { // Public Interface ////////////////////////////////////////////////////////////////////////////// - // FIXME: This should be in JS, "everything has a stream" - public static InputStream getInputStream(JS js) throws IOException { return ((Stream)js.unclone()).getInputStream();} + /*public static InputStream getInputStream(JS js) throws IOException { return ((Stream)js.unclone()).getInputStream();}*/ public static class NotCacheableException extends Exception { } private Cache getCache = new Cache(100); - // FEATURE: Mandate that Streams use only String keys? - protected JS _get(JS key) throws JSExn { return null; } + 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(key)); + if (ret == null) getCache.put(key, ret = _get(JS.toString(key))); return ret; } // Private Interface ////////////////////////////////////////////////////////////////////////////// + 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(); } @@ -39,9 +42,8 @@ public abstract class Stream extends JS implements JS.Cloneable { // FEATURE: Only instansiate only ibex.net.HTTP, share with all substreams public static class HTTP extends Stream { private String 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 JS _get(JS key) throws JSExn { return new HTTP(url + "/" + JS.toString(key)); } + 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.net.HTTP(url).GET(); } } @@ -54,30 +56,30 @@ public abstract class Stream extends JS implements JS.Cloneable { 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; public File(String path) { this.path = 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 JS _get(JS key) throws JSExn { return new File(path + java.io.File.separatorChar + JS.toString(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; - public Zip(Stream parent) { this(parent, null); } - public 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 JS _get(JS key) throws JSExn { return new Zip(parent, path==null?JS.toString(key):path+'/'+JS.toString(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); @@ -90,12 +92,12 @@ public abstract class Stream extends JS implements JS.Cloneable { /** "unwrap" a Cab archive */ public static class Cab extends Stream { - private Stream parent; + private JS parent; private String path; - 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 JS _get(JS key) throws JSExn { return new Cab(parent, path==null?JS.toString(key):path+'/'+JS.toString(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); } } @@ -103,14 +105,15 @@ public abstract class Stream extends JS implements 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; - public 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) { @@ -131,28 +134,32 @@ public abstract class Stream extends JS implements 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 { + // 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; } } }