- /** 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 {
- InputStream is = parent.getInputStream();
- byte[] scan = new byte[4];
- int ofs = 0;
- for(int i=0; i<2; i++) {
- // wierdly, .exe files have three MSCF's
- while(scan[0] != 'M' || scan[1] != 'S' || scan[2] != 'C' || scan[3] != 'F') {
- System.arraycopy(scan, 1, scan, 0, 3);
- int read = is.read();
- if (read == -1) throw new JS.Exn("MSCF header tag not found in file");
- scan[3] = (byte)read;
- ofs++;
- }
- scan[0] = 0;
- }
- Log.log(this, "found MSCF header at offset " + ofs);
- return org.xwt.util.CAB.getFileInputStream(is, path, true);
+ /** shadow resource which replaces the graft */
+ public static class ProgressWatcher extends Res {
+ final Res watchee;
+ 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);
+ return new FilterInputStream(is) {
+ 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);
+ if (ret != 1) bytesDownloaded += ret;
+ Scheduler.add(new Scheduler.Task() { public void perform() {
+ JS.Array args = new JS.Array();
+ args.addElement(new Integer(bytesDownloaded));
+ args.addElement(new Integer(is instanceof KnownLength ? ((KnownLength)is).getLength() : 0));
+ // FIXME
+ //new JS.Context(callback, null, args).resume();
+ } });
+ return ret;
+ }
+ };