+ if (url.startsWith("cab:")) return new Cab(stringToRes(url.substring(4)));
+ if (url.startsWith("data:")) return new ByteArray(Base64.decode(url.substring(5)));
+ if (url.startsWith("utf8:")) return new ByteArray(url.substring(5).getBytes());
+ throw new JS.Exn("invalid resource specifier " + url);
+ }
+
+ /** subclass from this if you want a CachedInputStream for each path */
+ public static class CachedRes extends Res {
+ private Res parent;
+ private boolean disk = false;
+
+ // FIXME: security concern here
+ private String subdir = null;
+
+ public String getDescriptiveName() { return parent.getDescriptiveName(); }
+ private Hash cachedInputStreams = new Hash();
+ public CachedRes(Res parent, String subdir, boolean disk) {
+ this.parent = parent; this.disk = disk; this.subdir = subdir;
+ }
+ public InputStream getInputStream(String path) throws IOException {
+ CachedInputStream cis = (CachedInputStream)cachedInputStreams.get(path);
+ if (cis == null) {
+ java.io.File f = null;
+ if (disk) {
+ // FIXME ugly
+ // FIXME need separate hash for disk/nondisk
+ f = new java.io.File(System.getProperty("user.home") +
+ java.io.File.separatorChar + ".xwt" +
+ java.io.File.separatorChar + "caches" +
+ java.io.File.separatorChar + subdir +
+ java.io.File.separatorChar +
+ new String(Base64.encode(parent.getDescriptiveName().getBytes())));
+ Log.log(this, "caching resource in " + f);
+ new java.io.File(f.getParent()).mkdirs();
+ if (f.exists()) return new FileInputStream(f);
+ }
+ cis = new CachedInputStream(parent.getInputStream(path), f);
+ cachedInputStreams.put(path, cis);
+ }
+ return cis.getInputStream();
+ }