public static Res stringToRes(String url) { return stringToRes(url, false); }
public static Res stringToRes(String url, boolean permitLocalFilesystem) {
if (url.indexOf('!') != -1)
- return (Res)(new Zip(stringToRes(url.substring(0, url.lastIndexOf('!')))).get(url.substring(url.lastIndexOf('!') + 1)));
+ return (Res)(new Zip(stringToRes(url.substring(0, url.lastIndexOf('!')))).
+ get(url.substring(url.lastIndexOf('!') + 1)));
if (url.startsWith("http://")) return new HTTP(url);
if (url.startsWith("https://")) return new HTTP(url);
if (url.startsWith("file:") && permitLocalFilesystem) return new File(url.substring(5));
throw new JS.Exn("invalid resource specifier " + url);
}
+ /** subclass from this if you want a CachedInputStream for each path */
+ public static abstract class CachedRes extends Res {
+ private Hash cachedInputStreams = new Hash();
+ abstract InputStream _getInputStream(String path) throws IOException;
+ public final InputStream getInputStream(String path) throws IOException {
+ CachedInputStream cis = (CachedInputStream)cachedInputStreams.get(path);
+ if (cis == null) {
+ cis = new CachedInputStream(_getInputStream(path));
+ cachedInputStreams.put(path, cis);
+ }
+ return cis.getInputStream();
+ }
+ }
+
/** HTTP or HTTPS resource */
- public static class HTTP extends Res {
+ public static class HTTP extends CachedRes {
private String url;
HTTP(String url) { this.url = url; }
- public InputStream getInputStream(String path) throws IOException { return new org.xwt.HTTP(url + path).GET(); }
+ public InputStream _getInputStream(String path) throws IOException {
+ return new org.xwt.HTTP(url + path).GET(); }
}
/** byte arrays */