/** so that we get the same subresource each time */
private Hash refCache = null;
- /** FIXME: needed? good idea? */
public Template t = null;
public final InputStream getInputStream() throws IOException { return getInputStream(""); }
/** if it makes sense to cache a resource, the resource must return a unique key */
public String getCacheKey() throws NotCacheableException { throw notCacheable; }
- // FIXME: general cleanup
/** subclass from this if you want a CachedInputStream for each path */
public static class CachedRes extends Res {
private Res parent;
private boolean disk = false;
private String key;
public String getCacheKey() throws NotCacheableException { return key; }
+ public String toString() { return key; }
private Hash cachedInputStreams = new Hash();
public CachedRes(Res p, String s, boolean d) throws NotCacheableException {
this.parent = p; this.disk = d; this.key = p.getCacheKey();
public static class HTTP extends Res {
private String url;
HTTP(String url) { while (url.endsWith("/")) url = url.substring(0, url.length() - 1); this.url = url; }
+ public String toString() { return url; }
public String getCacheKey() throws NotCacheableException { return url; }
public InputStream getInputStream(String path) throws IOException { return new org.xwt.HTTP(url + path).GET(); }
}
private byte[] bytes;
private String cacheKey = null;
ByteArray(byte[] bytes, String cacheKey) { this.bytes = bytes; this.cacheKey = cacheKey; }
+ public String toString() { return "byte[]"; }
public String getCacheKey() throws NotCacheableException { return cacheKey; }
public InputStream getInputStream(String path) throws IOException {
- if (!"".equals(path)) throw new JS.Exn("can't get subresources of a byte[] resource");
+ if (!"".equals(path)) throw new JSExn("can't get subresources of a byte[] resource");
return new ByteArrayInputStream(bytes);
}
}
while (path.endsWith(java.io.File.separatorChar + "")) path = path.substring(0, path.length() - 1);
this.path = path;
}
+ public String toString() { return "file:" + path; }
public String getCacheKey() throws NotCacheableException { throw notCacheable; } // already on the disk!
public InputStream getInputStream(String rest) throws IOException {
return new FileInputStream((path + rest).replace('/', java.io.File.separatorChar)); }
public static class Zip extends Res {
private Res parent;
Zip(Res parent) { this.parent = parent; }
+ public String toString() { return parent.toString() + "!zip"; }
public String getCacheKey() throws NotCacheableException { return parent.getCacheKey() + "!zip:"; }
public InputStream getInputStream(String path) throws IOException {
if (path.startsWith("/")) path = path.substring(1);
ZipInputStream zis = new ZipInputStream(pis);
ZipEntry ze = zis.getNextEntry();
while(ze != null && !ze.getName().equals(path)) ze = zis.getNextEntry();
- if (ze == null) throw new JS.Exn("requested file (" + path + ") not found in archive");
+ if (ze == null) throw new JSExn("requested file (" + path + ") not found in archive");
return new KnownLength.KnownLengthInputStream(zis, (int)ze.getSize());
}
}
public static class Cab extends Res {
private Res parent;
Cab(Res parent) { this.parent = parent; }
+ public String toString() { return parent.toString() + "!cab"; }
public String getCacheKey() throws NotCacheableException { return parent.getCacheKey() + "!cab:"; }
public InputStream getInputStream(String path) throws IOException {
if (path.startsWith("/")) path = path.substring(1);
/** the Builtin resource */
public static class Builtin extends Res {
- public Builtin() { };
- public String getCacheKey() throws NotCacheableException { throw notCacheable; } // not cacheable
- public InputStream getInputStream(String path) throws IOException {
- if (!path.equals("")) throw new IOException("the builtin resource has no subresources");
- return Platform.getBuiltinInputStream();
- }
+ public Builtin() { };
+ public String getCacheKey() throws NotCacheableException { throw notCacheable; } // not cacheable
+ public String toString() { return "builtin:"; }
+ public InputStream getInputStream(String path) throws IOException {
+ if (!path.equals("")) throw new IOException("the builtin resource has no subresources");
+ return Platform.getBuiltinInputStream();
+ }
}
/** what you get when you reference a subresource */
final Res watchee;
JSFunction callback;
ProgressWatcher(Res watchee, JSFunction callback) { this.watchee = watchee; this.callback = callback; }
+ public String toString() { return watchee.toString(); }
public String getCacheKey() throws NotCacheableException { return watchee.getCacheKey(); }
public InputStream getInputStream(String s) throws IOException {
final InputStream is = watchee.getInputStream(s);
int ret = super.read(b, off, len);
if (ret != 1) bytesDownloaded += ret;
Scheduler.add(new Scheduler.Task() { public void perform() {
- JSArray args = new JSArray();
- args.addElement(new Integer(bytesDownloaded));
- args.addElement(new Integer(is instanceof KnownLength ? ((KnownLength)is).getLength() : 0));
- callback.call(args);
+ callback.call(N(bytesDownloaded),
+ N(is instanceof KnownLength ? ((KnownLength)is).getLength() : 0), null, null, 2);
} });
return ret;
}