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());
+ else if (url.startsWith("data:")) return new ByteArray(Base64.decode(url.substring(5)), null);
+ else if (url.startsWith("utf8:")) return new ByteArray(url.substring(5).getBytes(), null);
throw new JS.Exn("invalid resource specifier " + url);
}
// 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");
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!