public class Picture {
public Picture() { this.res = null; }
- public Picture(Res r) { this.res = r; }
- private static Cache cache = new Cache(100); ///< Picture, keyed by the Res that loaded them
+ public Picture(Stream r) { this.res = r; }
+ private static Cache cache = new Cache(100); ///< Picture, keyed by the Stream that loaded them
- public Res res = null; ///< the resource we were loaded from
+ public Stream res = null; ///< the resource we were loaded from
public int width = -1; ///< the width of the image
public int height = -1; ///< the height of the image
public int[] data = null; ///< argb samples
public boolean isLoaded = false; ///< true iff the image is fully loaded
/** turns a resource into a Picture.Source and passes it to the callback */
- public static Picture load(final Res r, final Scheduler.Task callback) {
+ public static Picture load(final Stream r, final Scheduler.Task callback) {
Picture ret = (Picture)cache.get(r);
if (ret == null) cache.put(r, ret = Platform.createPicture(r));
final Picture p = ret;
in = r.getInputStream();
} catch (IOException e) {
in = null;
- if (r instanceof Res.Ref) {
+ if (r instanceof Stream.Ref) {
// add extensions to the resource, looking for the image
- Res.Ref ref = (Res.Ref)r;
- Res newr;
+ Stream.Ref ref = (Stream.Ref)r;
+ Stream newr;
String[] exts = new String[] { ".png", ".jpeg", ".gif" };
for (int i=0; i < exts.length && in == null; i++) {
/** Base class for XWT resources */
-public abstract class Res extends JS {
+public abstract class Stream extends JS {
/** return a resource for a given url */
- public static final Res fromURL(String url) throws JSExn {
- if (url.startsWith("http://")) return new Res.HTTP(url);
- else if (url.startsWith("https://")) return new Res.HTTP(url);
- else if (url.startsWith("data:")) return new Res.ByteArray(Base64.decode(url.substring(5)), null);
- else if (url.startsWith("utf8:")) return new Res.ByteArray(url.substring(5).getBytes(), null);
+ public static final Stream fromURL(String url) throws JSExn {
+ if (url.startsWith("http://")) return new Stream.HTTP(url);
+ else if (url.startsWith("https://")) return new Stream.HTTP(url);
+ else if (url.startsWith("data:")) return new Stream.ByteArray(Base64.decode(url.substring(5)), null);
+ else if (url.startsWith("utf8:")) return new Stream.ByteArray(url.substring(5).getBytes(), null);
throw new JSExn("invalid resource specifier " + url);
}
public final InputStream getInputStream() throws IOException { return getInputStream(""); }
public abstract InputStream getInputStream(String path) throws IOException;
- public Res addExtension(String extension) { return new Ref(this, extension); }
+ public Stream addExtension(String extension) { return new Ref(this, extension); }
public Object get(Object key) throws JSExn {
if ("".equals(key)) {
public String getCacheKey() throws NotCacheableException { throw notCacheable; }
/** subclass from this if you want a CachedInputStream for each path */
- public static class CachedRes extends Res {
- private Res parent;
+ public static class CachedStream extends Stream {
+ private Stream 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 {
+ public CachedStream(Stream p, String s, boolean d) throws NotCacheableException {
this.parent = p; this.disk = d; this.key = p.getCacheKey();
}
public InputStream getInputStream(String path) throws IOException {
// Useful Subclasses //////////////////////////////////////////////////////////////////////
/** HTTP or HTTPS resource */
- public static class HTTP extends Res {
+ public static class HTTP extends Stream {
private String url;
HTTP(String url) { while (url.endsWith("/")) url = url.substring(0, url.length() - 1); this.url = url; }
public String toString() { return url; }
}
/** byte arrays */
- public static class ByteArray extends Res {
+ public static class ByteArray extends Stream {
private byte[] bytes;
private String cacheKey = null;
ByteArray(byte[] bytes, String cacheKey) { this.bytes = bytes; this.cacheKey = cacheKey; }
}
/** a file */
- public static class File extends Res {
+ public static class File extends Stream {
private String path;
File(String path) {
while (path.endsWith(java.io.File.separatorChar + "")) path = path.substring(0, path.length() - 1);
}
/** "unwrap" a Zip archive */
- public static class Zip extends Res {
- private Res parent;
- Zip(Res parent) { this.parent = parent; }
+ public static class Zip extends Stream {
+ private Stream parent;
+ Zip(Stream 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 {
}
/** "unwrap" a Cab archive */
- public static class Cab extends Res {
- private Res parent;
- Cab(Res parent) { this.parent = parent; }
+ public static class Cab extends Stream {
+ private Stream parent;
+ Cab(Stream 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 {
}
/** the Builtin resource */
- public static class Builtin extends Res {
+ public static class Builtin extends Stream {
public Builtin() { };
public String getCacheKey() throws NotCacheableException { throw notCacheable; } // not cacheable
public String toString() { return "builtin:"; }
}
/** what you get when you reference a subresource */
- public static class Ref extends Res {
- Res parent;
+ public static class Ref extends Stream {
+ Stream parent;
Object key;
public String toString() { return parent.toString() + "/" + key; }
- Ref(Res parent, Object key) { this.parent = parent; this.key = key; }
+ Ref(Stream parent, Object key) { this.parent = parent; this.key = key; }
public String getCacheKey() throws NotCacheableException { return parent.getCacheKey() + "/" + key; }
- public Res addExtension(String extension) {
+ public Stream addExtension(String extension) {
return ((String)key).endsWith(extension) ? this : new Ref(parent, key + extension); }
public InputStream getInputStream(String path) throws IOException { return parent.getInputStream("/" + key + path); }
}
/** provides redirection of a specified key */
- public static class Graft extends Res {
- Res graftee;
+ public static class Graft extends Stream {
+ Stream graftee;
Object replaced_key, replaced_val;
- Graft(Res graftee, Object key, Object val) { this.graftee = graftee; this.replaced_key = key; this.replaced_val = val; }
+ Graft(Stream graftee, Object key, Object val) { this.graftee = graftee; this.replaced_key = key; this.replaced_val = val; }
public boolean equals(Object o) { return this == o || graftee.equals(o); }
public int hashCode() { return graftee.hashCode(); }
public InputStream getInputStream(String s) throws IOException { return graftee.getInputStream(s); }
}
/** shadow resource which replaces the graft */
- public static class ProgressWatcher extends Res {
- final Res watchee;
+ public static class ProgressWatcher extends Stream {
+ final Stream watchee;
JSFunction callback;
- ProgressWatcher(Res watchee, JSFunction callback) { this.watchee = watchee; this.callback = callback; }
+ ProgressWatcher(Stream 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 {