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.ibex.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 {
97 public String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); }
98 public InputStream getInputStream() throws IOException { return Platform.getBuiltinInputStream(); }
101 /** shadow resource which replaces the graft */
102 public static class ProgressWatcher extends Stream {
103 final Stream watchee;
105 ProgressWatcher(Stream watchee, JS callback) { this.watchee = watchee; this.callback = callback; }
106 public String getCacheKey() throws NotCacheableException { return watchee.getCacheKey(); }
107 public InputStream getInputStream() throws IOException {
108 final InputStream is = watchee.getInputStream();
109 return new FilterInputStream(is) {
110 int bytesDownloaded = 0;
111 public int read() throws IOException {
112 int ret = super.read();
113 if (ret != -1) bytesDownloaded++;
116 public int read(byte[] b, int off, int len) throws IOException {
117 int ret = super.read(b, off, len);
118 if (ret != 1) bytesDownloaded += ret;
119 Scheduler.add(new Scheduler.Task() { public void perform() throws IOException, JSExn {
120 callback.call(N(bytesDownloaded),
121 N(is instanceof KnownLength ? ((KnownLength)is).getLength() : 0), null, null, 2);
129 /** subclass from this if you want a CachedInputStream for each path */
130 public static class CachedStream extends Stream {
131 private Stream parent;
132 private boolean disk = false;
134 public String getCacheKey() throws NotCacheableException { return key; }
135 CachedInputStream cis = null;
136 public CachedStream(Stream p, String s, boolean d) throws NotCacheableException {
137 this.parent = p; this.disk = d; this.key = p.getCacheKey();
139 public InputStream getInputStream() throws IOException {
140 if (cis != null) return cis.getInputStream();
142 cis = new CachedInputStream(parent.getInputStream());
144 java.io.File f = LocalStorage.Cache.getCacheFileForKey(key);
145 if (f.exists()) return new FileInputStream(f);
146 cis = new CachedInputStream(parent.getInputStream(), f);
148 return cis.getInputStream();