2003/09/29 01:41:51
[org.ibex.core.git] / src / org / xwt / Res.java
index 37cdc29..01856f2 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;
@@ -145,7 +173,7 @@ public abstract class Res extends JS {
             return ((i & 0xff) << 24) | ((i & 0xff00) << 8) | ((i & 0xff0000) >>> 8) | (i >>> 24);
         }
         public InputStream getInputStream(String path) throws IOException {
-            InputStream is = parent.getInputStream();
+            /* InputStream is = parent.getInputStream();
             byte[] scan = new byte[4];
             int ofs = 0;
             for(int i=0; i<2; i++)  {
@@ -161,6 +189,14 @@ public abstract class Res extends JS {
             }
             Log.log(this, "found MSCF header at offset " + ofs);
             return org.xwt.util.CAB.getFileInputStream(is, path, true);
+            */
+            try {
+               return org.xwt.util.CAB.getFileInputStream(is, 2, path);
+            } catch (EOFException eof) {
+               throw new JS.Exn("MSCF header tag not found in file");
+            } catch (EOFException eof) {
+               throw new JS.Exn("IOException while reading file");
+            }
         }
     }