-// Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
+// Copyright 2000-2005 the Contributors, as shown in the revision logs.
+// Licensed under the GNU General Public License version 2 ("the License").
+// You may not use this file except in compliance with the License.
+
package org.ibex.core;
import java.io.*;
* be totally independent of the others (ie separate stream position
* and state) although they draw from the same data source.
*/
-public abstract class Stream extends JS.O implements JS.Cloneable {
+public abstract class Stream extends JS.Obj implements JS.Cloneable {
// Public Interface //////////////////////////////////////////////////////////////////////////////
// streams are "sealed" by default to prevent accidental object leakage
public void put(Object key, Object val) { }
- private Cache getCache = new Cache(100);
- protected Object _get(Object key) { return null; }
- public final Object get(Object key) {
- Object ret = getCache.get(key);
+ private Cache getCache = new Cache(100, true);
+ protected JS _get(JS key) throws JSExn { return null; }
+ public final JS get(JS key) throws JSExn {
+ JS ret = (JS)getCache.get(key);
if (ret == null) getCache.put(key, ret = _get(key));
return ret;
}
private String url;
//public String toString() { return "Stream.HTTP:" + url; }
public HTTP(String url) { while (url.endsWith("/")) url = url.substring(0, url.length() - 1); this.url = url; }
- public Object _get(Object key) { return new HTTP(url + "/" + (String)key); }
+ public JS _get(JS key) throws JSExn { return new HTTP(url + "/" + JSU.toString(key)); }
public String getCacheKey(Vec path) throws NotCacheableException { return url; }
public InputStream getInputStream() throws IOException { return new org.ibex.net.HTTP(url).GET(null, null); }
}
//public String toString() { return "file:" + path; }
public String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); /* already on disk */ }
public InputStream getInputStream() throws IOException { return new FileInputStream(path); }
- public Object _get(Object key) { return new File(path + java.io.File.separatorChar + (String)key); }
+ public JS _get(JS key) throws JSExn {
+ System.out.println("get: " + JSU.str(key));
+ return new File(path + java.io.File.separatorChar + JSU.toString(key)); }
}
/** "unwrap" a Zip archive */
this.path = path;
}
public String getCacheKey() throws NotCacheableException { return parent.getCacheKey() + "!zip:"; }
- public Object _get(Object key) { return new Zip(parent, path==null?(String)key:path+'/'+(String)key); }
+ public JS _get(JS key) throws JSExn { return new Zip(parent, path==null?JSU.toString(key):path+'/'+JSU.toString(key)); }
public InputStream getInputStream() throws IOException {
InputStream pis = parent.getInputStream();
ZipInputStream zis = new ZipInputStream(pis);
public Cab(Stream parent) { this(parent, null); }
public Cab(Stream parent, String path) { this.parent = parent; this.path = path; }
public String getCacheKey() throws NotCacheableException { return parent.getCacheKey() + "!cab:"; }
- public Object _get(Object key) { return new Cab(parent, path==null?(String)key:path+'/'+(String)key); }
+ public JS _get(JS key) throws JSExn { return new Cab(parent, path==null?(String)key:path+'/'+(String)key); }
public InputStream getInputStream() throws IOException { return new MSPack(parent.getInputStream()).getInputStream(path); }
}
*/
public InputStream getInputStream() throws IOException { return Platform.getBuiltinInputStream(); }
}
+ /** the Builtin resource */
+ public static class FromInputStream extends Stream {
+ private final InputStream is;
+ public FromInputStream(InputStream is) { this.is = is; }
+ public String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); }
+ public InputStream getInputStream() throws IOException { return is; }
+ }
+
/** shadow resource which replaces the graft */
public static class ProgressWatcher extends Stream {
+ private final JS[] callargs = new JS[2];
final Stream watchee;
JS callback;
public ProgressWatcher(Stream watchee, JS callback) { this.watchee = watchee; this.callback = callback; }
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 Task() { public void perform() throws IOException, JSExn {
- callback.call(N(bytesDownloaded),
- N(is instanceof KnownLength ? ((KnownLength)is).getLength() : 0), null, null, 2);
+ Scheduler.add(new Callable() {
+ public Object run(Object o) throws IOException, JSExn {
+ try {
+ int len = is instanceof KnownLength.KnownLengthInputStream ?
+ ((KnownLength.KnownLengthInputStream)is).getLength() : 0;
+ callargs[0] = JSU.N(bytesDownloaded);
+ callargs[1] = JSU.N(len);
+ callback.call(callargs);
+ } finally { callargs[0] = callargs[1] = null; }
+ return null;
} });
return ret;
}