2003/09/28 22:05:58
[org.ibex.core.git] / src / org / xwt / Res.java
index 37cdc29..862b12d 100644 (file)
@@ -6,6 +6,7 @@ import java.util.*;
 import java.util.zip.*;
 import org.xwt.js.*;
 import org.xwt.util.*;
+import org.bouncycastle.util.encoders.Base64;
 
 /** base class for XWT resources */
 public abstract class Res extends JS {
@@ -52,6 +53,8 @@ public abstract class Res extends JS {
         if (url.startsWith("https://")) return new HTTP(url);
         if (url.startsWith("file:") && permitLocalFilesystem) return new File(url.substring(5));
         if (url.startsWith("cab:")) return new CAB(stringToRes(url.substring(4)));
+        if (url.startsWith("data:")) return new ByteArray(Base64.decode(url.substring(5)));
+        if (url.startsWith("utf8:")) return new ByteArray(url.substring(5).getBytes());
         throw new JS.Exn("invalid resource specifier " + url);
     }
 
@@ -137,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;