/** Base class for XWT resources */
public abstract class Res extends JS {
-
- // Public Static //////////////////////////////////////////////////////////////////////
-
- // FIXME: move to XWT.load()?
- public static Res fromString(String url) {
- if (url.startsWith("http://")) return new HTTP(url);
- else if (url.startsWith("https://")) return new HTTP(url);
- else if (url.startsWith("data:")) return new ByteArray(Base64.decode(url.substring(5)));
- else if (url.startsWith("utf8:")) return new ByteArray(url.substring(5).getBytes());
- throw new JS.Exn("invalid resource specifier " + url);
- }
-
-
// Base Class //////////////////////////////////////////////////////////////////////
public String typeName() { return "resource"; }
// Caching //////////////////////////////////////////////////////////////////////
- private static class NotCacheableException extends Exception { }
- private static NotCacheableException notCacheable = new NotCacheableException();
+ public static class NotCacheableException extends Exception { }
+ public static NotCacheableException notCacheable = new NotCacheableException();
/** if it makes sense to cache a resource, the resource must return a unique key */
public String getCacheKey() throws NotCacheableException { throw notCacheable; }
public static class CachedRes extends Res {
private Res parent;
private boolean disk = false;
- public String getCacheKey() throws NotCacheableException { return parent.getCacheKey(); }
+ private String key;
+ public String getCacheKey() throws NotCacheableException { return key; }
private Hash cachedInputStreams = new Hash();
- public CachedRes(Res p, String s, boolean d) { this.parent = p; this.disk = d; }
+ public CachedRes(Res p, String s, boolean d) throws NotCacheableException {
+ this.parent = p; this.disk = d; this.key = p.getCacheKey();
+ }
public InputStream getInputStream(String path) throws IOException {
CachedInputStream cis = (CachedInputStream)cachedInputStreams.get(path);
if (cis == null) {
java.io.File.separatorChar + ".xwt" +
java.io.File.separatorChar + "caches" +
java.io.File.separatorChar +
- new String(Base64.encode(parent.getCacheKey().getBytes())));
+ new String(Base64.encode(key.getBytes())));
Log.log(this, "caching resource in " + f);
new java.io.File(f.getParent()).mkdirs();
if (f.exists()) return new FileInputStream(f);
/** HTTP or HTTPS resource */
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; }
+ HTTP(String url) { while (url.endsWith("/")) url = url.substring(0, url.length() - 1); this.url = url; }
public String getCacheKey() throws NotCacheableException { return url; }
public InputStream getInputStream(String path) throws IOException { return new org.xwt.HTTP(url + path).GET(); }
}
public static class ByteArray extends Res {
private byte[] bytes;
private String cacheKey = null;
- ByteArray(byte[] bytes, String cacheKey) { this.bytes = bytes; this.cacheKey = cacheKey; }}
+ ByteArray(byte[] bytes, String cacheKey) { this.bytes = bytes; this.cacheKey = cacheKey; }
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);
}
}
public static class File extends Res {
private String path;
File(String path) {
- while (path.endsWith(java.io.File.separatorChar)) path = path.substring(0, path.length() - 1);
+ while (path.endsWith(java.io.File.separatorChar + "")) path = path.substring(0, path.length() - 1);
this.path = path;
}
public String getCacheKey() throws NotCacheableException { throw notCacheable; } // already on the disk!
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 Ref extends Res {
Res parent;
Object key;
+ public String toString() { return parent.toString() + "/" + key; }
Ref(Res parent, Object key) { this.parent = parent; this.key = key; }
public String getCacheKey() throws NotCacheableException { return parent.getCacheKey() + "/" + key; }
public Res addExtension(String extension) {
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;
}