2003/09/28 22:05:58
[org.ibex.core.git] / src / org / xwt / Res.java
index 107c766..862b12d 100644 (file)
@@ -140,6 +140,31 @@ public abstract class Res extends JS {
         public Res getParent() { return graftee.getParent(); }
     }
 
+    /** shadow resource which replaces the graft */
+    public static class ProgressWatcher extends Res {
+        Res watchee;
+        JS.Callable callback;
+        Graft(Res watchee, JS.Callable callback) { this.watchee = watchee; this.callback = callback; }
+        public InputStream getInputStream(String s) throws IOException {
+            return new FilterInputStream(graftee.getInputStream(s)) {
+                    int bytesDownloaded = 0;
+                    public int read() throws IOException {
+                        int ret = super.read();
+                        if (ret != -1) bytesDownloaded++;
+                        return ret;
+                    }
+                    public int read(byte[] b, int off, int len) throws IOException {
+                        int ret = super.read(b, off, len);
+                        ThreadMessage.newthread(new JS.Callable() { public Object call(JS.Args a) {
+                            JS.Array args = new JS.Array();
+                            args.addElement(new Integer(bytesDownloaded));
+                            callback.call(args);
+                        } });
+                    }
+                };
+        }
+    });
+
     /** unpacks a Microsoft CAB file (possibly embedded in another file; we scan for 'MSCF' */
     public static class CAB extends Res {
         private Res parent;