1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
6 import java.util.zip.*;
9 import org.xwt.translators.MSPack;
10 import org.bouncycastle.util.encoders.Base64;
13 * Essentiall an InputStream "factory". You can repeatedly ask a
14 * Stream for an InputStream, and each InputStream you get back will
15 * be totally independent of the others (ie separate stream position
16 * and state) although they draw from the same data source.
18 public abstract class Stream extends JS.Cloneable {
20 // Public Interface //////////////////////////////////////////////////////////////////////////////
22 public static InputStream getInputStream(Object js) throws IOException { return ((Stream)((JS)js).unclone()).getInputStream();}
23 public static class NotCacheableException extends Exception { }
25 // streams are "sealed" by default to prevent accidental object leakage
26 public void put(Object key, Object val) throws JSExn { }
27 public Object get(Object key) throws JSExn { return null; }
29 // Private Interface //////////////////////////////////////////////////////////////////////////////
31 protected abstract InputStream getInputStream() throws IOException;
32 protected String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); }
34 /** HTTP or HTTPS resource */
35 public static class HTTP extends Stream {
37 HTTP(String url) { while (url.endsWith("/")) url = url.substring(0, url.length() - 1); this.url = url; }
38 public Object get(Object key) throws JSExn { return new HTTP(url + "/" + (String)key); }
39 public String getCacheKey(Vec path) throws NotCacheableException { return url; }
40 public InputStream getInputStream() throws IOException { return new org.xwt.HTTP(url).GET(); }
44 public static class ByteArray extends Stream {
46 private String cacheKey;
47 ByteArray(byte[] bytes, String cacheKey) { this.bytes = bytes; this.cacheKey = cacheKey; }
48 public String getCacheKey() throws NotCacheableException {
49 if (cacheKey == null) throw new NotCacheableException(); return cacheKey; }
50 public InputStream getInputStream() throws IOException { return new ByteArrayInputStream(bytes); }
54 public static class File extends Stream {
56 File(String path) { this.path = path; }
57 public String toString() { return "file:" + path; }
58 public String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); /* already on disk */ }
59 public InputStream getInputStream() throws IOException { return new FileInputStream(path); }
60 public Object get(Object key) throws JSExn { return new File(path + java.io.File.separatorChar + (String)key); }
63 /** "unwrap" a Zip archive */
64 public static class Zip extends Stream {
65 private Stream parent;
67 Zip(Stream parent) { this(parent, null); }
68 Zip(Stream parent, String path) {
69 while(path != null && path.startsWith("/")) path = path.substring(1);
73 public String getCacheKey() throws NotCacheableException { return parent.getCacheKey() + "!zip:"; }
74 public Object get(Object key) throws JSExn { return new Zip(parent, path==null?(String)key:path+'/'+(String)key); }
75 public InputStream getInputStream() throws IOException {
76 InputStream pis = parent.getInputStream();
77 ZipInputStream zis = new ZipInputStream(pis);
78 ZipEntry ze = zis.getNextEntry();
79 while(ze != null && !ze.getName().equals(path)) ze = zis.getNextEntry();
80 if (ze == null) throw new IOException("requested file (" + path + ") not found in archive");
81 return new KnownLength.KnownLengthInputStream(zis, (int)ze.getSize());
85 /** "unwrap" a Cab archive */
86 public static class Cab extends Stream {
87 private Stream parent;
89 Cab(Stream parent) { this(parent, null); }
90 Cab(Stream parent, String path) { this.parent = parent; this.path = path; }
91 public String getCacheKey() throws NotCacheableException { return parent.getCacheKey() + "!cab:"; }
92 public Object get(Object key) throws JSExn { return new Cab(parent, path==null?(String)key:path+'/'+(String)key); }
93 public InputStream getInputStream() throws IOException { return new MSPack(parent.getInputStream()).getInputStream(path); }
96 /** the Builtin resource */
97 public static class Builtin extends Stream {
99 public String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); }
100 public InputStream getInputStream() throws IOException { return Platform.getBuiltinInputStream(); }
103 /** shadow resource which replaces the graft */
104 public static class ProgressWatcher extends Stream {
105 final Stream watchee;
107 ProgressWatcher(Stream watchee, JS callback) { this.watchee = watchee; this.callback = callback; }
108 public String getCacheKey() throws NotCacheableException { return watchee.getCacheKey(); }
109 public InputStream getInputStream() throws IOException {
110 final InputStream is = watchee.getInputStream();
111 return new FilterInputStream(is) {
112 int bytesDownloaded = 0;
113 public int read() throws IOException {
114 int ret = super.read();
115 if (ret != -1) bytesDownloaded++;
118 public int read(byte[] b, int off, int len) throws IOException {
119 int ret = super.read(b, off, len);
120 if (ret != 1) bytesDownloaded += ret;
121 Scheduler.add(new Scheduler.Task() { public void perform() throws Exception {
122 callback.call(N(bytesDownloaded),
123 N(is instanceof KnownLength ? ((KnownLength)is).getLength() : 0), null, null, 2);
131 /** subclass from this if you want a CachedInputStream for each path */
132 public static class CachedStream extends Stream {
133 private Stream parent;
134 private boolean disk = false;
136 public String getCacheKey() throws NotCacheableException { return key; }
137 CachedInputStream cis = null;
138 public CachedStream(Stream p, String s, boolean d) throws NotCacheableException {
139 this.parent = p; this.disk = d; this.key = p.getCacheKey();
141 public InputStream getInputStream() throws IOException {
142 if (cis != null) return cis.getInputStream();
144 cis = new CachedInputStream(parent.getInputStream());
146 java.io.File f = LocalStorage.Cache.getCacheFileForKey(key);
147 if (f.exists()) return new FileInputStream(f);
148 cis = new CachedInputStream(parent.getInputStream(), f);
150 return cis.getInputStream();