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