2003/11/03 06:32:55
[org.ibex.core.git] / src / org / xwt / Res.java
index 399476b..954f290 100644 (file)
@@ -80,7 +80,7 @@ public abstract class Res extends JS {
         }
         if (url.startsWith("http://")) return new HTTP(url);
         if (url.startsWith("https://")) return new HTTP(url);
-        if (url.startsWith("cab:")) return new CAB(stringToRes(url.substring(4)));
+        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);
@@ -145,6 +145,18 @@ public abstract class Res extends JS {
         }
     }
 
+    /** "unwrap" a Cab archive */
+    public static class Cab extends Res {
+        private Res parent;
+        Cab(Res parent) { this.parent = parent; }
+        public String getDescriptiveName() { return "cab[" + parent.getDescriptiveName() + "]"; }
+        public InputStream getInputStream(String path) throws IOException {
+            // FIXME: knownlength
+            if (path.startsWith("/")) path = path.substring(1);
+            return new org.xwt.translators.MSPack(parent.getInputStream()).getInputStream(path);
+        }
+    }
+
     /** the Builtin resource */
     public static class Builtin extends Res {
        public Builtin() { };
@@ -204,8 +216,8 @@ public abstract class Res extends JS {
     /** shadow resource which replaces the graft */
     public static class ProgressWatcher extends Res {
         final Res watchee;
-        JS.CompiledFunction callback;
-        ProgressWatcher(Res watchee, JS.CompiledFunction callback) { this.watchee = watchee; this.callback = callback; }
+        Function callback;
+        ProgressWatcher(Res watchee, Function callback) { this.watchee = watchee; this.callback = callback; }
         public String getDescriptiveName() { return watchee.getDescriptiveName(); }
         public InputStream getInputStream(String s) throws IOException {
             final InputStream is = watchee.getInputStream(s);
@@ -232,24 +244,6 @@ public abstract class Res extends JS {
         }
     }
 
-    /** unpacks a Microsoft CAB file (possibly embedded in another file; we scan for 'MSCF' */
-    public static class CAB extends Res {
-        private Res parent;
-        CAB(Res parent) { this.parent = parent; }
-        private int swap_endian(int i) {
-            return ((i & 0xff) << 24) | ((i & 0xff00) << 8) | ((i & 0xff0000) >>> 8) | (i >>> 24);
-        }
-        public InputStream getInputStream(String path) throws IOException {
-            try {
-               return org.xwt.util.CAB.getFileInputStream(parent.getInputStream(), 2, path);
-            } catch (EOFException eof) {
-               throw new JS.Exn("MSCF header tag not found in file");
-            } catch (IOException ioe) {
-               throw new JS.Exn("IOException while reading file");
-            }
-        }
-    }
-
     public Object callMethod(Object method, Array args, boolean checkOnly) throws JS.Exn {
         if (method.equals("getUTF")) {
             if (checkOnly) return Boolean.TRUE;