X-Git-Url: http://git.megacz.com/?p=org.ibex.core.git;a=blobdiff_plain;f=src%2Forg%2Fibex%2Fcore%2FStream.java;h=247540a95a25badaee392ca47e777fefcdb5e5fc;hp=34b71d5e9b6fecf09c2b083f31bc97bdc9fb6025;hb=0db31fc0dc88749cd8022790fd475df8b7b06c27;hpb=586485c446bc6870e08cfca8675a41442cca2c54 diff --git a/src/org/ibex/core/Stream.java b/src/org/ibex/core/Stream.java index 34b71d5..247540a 100644 --- a/src/org/ibex/core/Stream.java +++ b/src/org/ibex/core/Stream.java @@ -26,7 +26,7 @@ public abstract class Stream extends JS.Obj implements JS.Cloneable { // streams are "sealed" by default to prevent accidental object leakage public void put(Object key, Object val) { } - private Cache getCache = new Cache(100); + private Cache getCache = new Cache(100, true); protected JS _get(JS key) throws JSExn { return null; } public final JS get(JS key) throws JSExn { JS ret = (JS)getCache.get(key); @@ -44,7 +44,7 @@ public abstract class Stream extends JS.Obj implements JS.Cloneable { private String url; //public String toString() { 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(JS key) throws JSExn { return new HTTP(url + "/" + JSU.toString(key)); } public String getCacheKey(Vec path) throws NotCacheableException { return url; } public InputStream getInputStream() throws IOException { return new org.ibex.net.HTTP(url).GET(null, null); } } @@ -67,8 +67,8 @@ public abstract class Stream extends JS.Obj implements JS.Cloneable { 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 { - System.out.println("get: " + JS.debugToString(key)); - return new File(path + java.io.File.separatorChar + JS.toString(key)); } + System.out.println("get: " + JSU.str(key)); + return new File(path + java.io.File.separatorChar + JSU.toString(key)); } } /** "unwrap" a Zip archive */ @@ -82,7 +82,7 @@ public abstract class Stream extends JS.Obj implements JS.Cloneable { 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 JS _get(JS key) throws JSExn { return new Zip(parent, path==null?JSU.toString(key):path+'/'+JSU.toString(key)); } public InputStream getInputStream() throws IOException { InputStream pis = parent.getInputStream(); ZipInputStream zis = new ZipInputStream(pis); @@ -122,6 +122,7 @@ public abstract class Stream extends JS.Obj implements JS.Cloneable { /** shadow resource which replaces the graft */ public static class ProgressWatcher extends Stream { + private final JS[] callargs = new JS[2]; final Stream watchee; JS callback; public ProgressWatcher(Stream watchee, JS callback) { this.watchee = watchee; this.callback = callback; } @@ -138,9 +139,16 @@ public abstract class Stream extends JS.Obj implements 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 Task() { public void perform() throws IOException, JSExn { - callback.call(N(bytesDownloaded), - N(is instanceof KnownLength ? ((KnownLength)is).getLength() : 0), null, null, 2); + Scheduler.add(new Callable() { + public Object run(Object o) throws IOException, JSExn { + try { + int len = is instanceof KnownLength.KnownLengthInputStream ? + ((KnownLength.KnownLengthInputStream)is).getLength() : 0; + callargs[0] = JSU.N(bytesDownloaded); + callargs[1] = JSU.N(len); + callback.call(callargs); + } finally { callargs[0] = callargs[1] = null; } + return null; } }); return ret; }