- public static class Graft extends Res {
- Res graftee;
- Object replaced_key;
- Object replaced_val;
- Graft(Res graftee, Object key, Object val) {
- this.graftee = graftee; replaced_key = key; replaced_val = val; }
- public boolean equals(Object o) { return (this == o || graftee.equals(o)); }
- public Object get(Object key) {
- return replaced_key.equals(key) ? replaced_val : graftee.get(key);
- }
- }
-
- /////////////// bytestream
-
- public Object callMethod(Object method, Array args, boolean checkOnly) throws JS.Exn {
- if (method.equals("getUTF")) {
- if (checkOnly) return Boolean.TRUE;
- if (args.length() != 0) return null;
- try {
- CharArrayWriter caw = new CharArrayWriter();
- InputStream is = getInputStream();
- BufferedReader r = new BufferedReader(new InputStreamReader(is));
- char[] buf = new char[1024];
- while(true) {
- int numread = r.read(buf, 0, 1024);
- if (numread == -1) break;
- caw.write(buf, 0, numread);
- }
- return caw.toString();
- } catch (IOException e) {
- if (Log.on) Log.log(ByteStream.class, "IO Exception while reading from file");
- if (Log.on) Log.log(ByteStream.class, e);
- throw new JS.Exn("error while reading from ByteStream");
- }
- } else if (name.equals("getDOM")) {
- if (checkOnly) return Boolean.TRUE;
- if (args.length() != 0) return null;
- return new XMLHelper().doParse();
- }
- if (checkOnly) return Boolean.FALSE;
- return null;
- }
-
- private class XMLHelper extends XML {
- Vector obStack = new Vector();
- public XMLHelper() { super(BUFFER_SIZE); }
- public void startElement(XML.Element c) throws XML.SchemaException {
- JS o = new JS.Obj();
- o.put("$name", c.localName);
- for(int i=0; i<c.len; i++) o.put(c.keys[i], c.vals[i]);
- o.put("$numchildren", new Integer(0));
- obStack.addElement(o);
- }
- public void endElement(XML.Element c) throws XML.SchemaException {
- if (obStack.size() == 1) return;
- JS me = (JS)obStack.lastElement();
- obStack.setSize(obStack.size() - 1);
- JS parent = (JS)obStack.lastElement();
- int numchildren = ((Integer)parent.get("$numchildren")).intValue();
- parent.put("$numchildren", new Integer(numchildren + 1));
- parent.put(new Integer(numchildren), me);
+ public static class ProgressWatcher extends Res {
+ final Res watchee;
+ JSFunction callback;
+ ProgressWatcher(Res watchee, JSFunction callback) { this.watchee = watchee; this.callback = callback; }
+ public String getCacheKey() throws NotCacheableException { return watchee.getCacheKey(); }
+ 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() {
+ callback.call(N(bytesDownloaded),
+ N(is instanceof KnownLength ? ((KnownLength)is).getLength() : 0), null, null, 2);
+ } });
+ return ret;
+ }
+ };