2003/09/28 22:05:58
authormegacz <megacz@xwt.org>
Fri, 30 Jan 2004 07:38:40 +0000 (07:38 +0000)
committermegacz <megacz@xwt.org>
Fri, 30 Jan 2004 07:38:40 +0000 (07:38 +0000)
darcs-hash:20040130073840-2ba56-7b510046272e4bd532df2a9a01346864c01a66de.gz

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;