- if (str.endsWith(".gif")) ret = GIF.decode(new HTTP(str).getInputStream(), str);
- else ret = PNG.decode(new HTTP(str).getInputStream(), str);
+ final HTTP http = new HTTP(str);
+ final int contentLength = http.getContentLength();
+ InputStream is = new FilterInputStream(http.getInputStream()) {
+ int bytesDownloaded = 0;
+ boolean clear = true;
+ public int read() throws IOException {
+ bytesDownloaded++;
+ return super.read();
+ }
+ public int read(byte[] b, int off, int len) throws IOException {
+ int ret = super.read(b, off, len);
+ if (ret != -1) bytesDownloaded += ret;
+ if (clear && callback != null) {
+ clear = false;
+ ThreadMessage.newthread(new JSObject.JSFunction() {
+ public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
+ try {
+ callback.call(cx, null, null, new Object[] {
+ new Double(bytesDownloaded), new Double(contentLength) });
+ } finally {
+ clear = true;
+ }
+ return null;
+ }
+ });
+ }
+ return ret;
+ }
+ };
+
+ if (str.endsWith(".gif")) ret = GIF.decode(is, str);
+ else ret = PNG.decode(is, str);