1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
5 import java.util.zip.*;
6 import org.ibex.util.*;
7 import org.ibex.plat.*;
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 implements JS.Cloneable {
18 // Public Interface //////////////////////////////////////////////////////////////////////////////
20 /*public static InputStream getInputStream(JS js) throws IOException { return ((Stream)js.unclone()).getInputStream();}*/
21 public static class NotCacheableException extends Exception { }
23 private Cache getCache = new Cache(100);
24 public abstract JS _get(String key);
25 public final JS get(JS key) throws JSExn {
26 JS ret = (JS) getCache.get(key);
27 if (ret == null) getCache.put(key, ret = _get(JS.toString(key)));
31 // Private Interface //////////////////////////////////////////////////////////////////////////////
33 static String getCacheKey(JS s) throws NotCacheableException {
34 if(s instanceof Stream) return ((Stream)s).getCacheKey();
35 throw new NotCacheableException();
38 public abstract InputStream getInputStream() throws IOException;
39 protected String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); }
41 /** HTTP or HTTPS resource */
42 // FEATURE: Only instansiate only ibex.net.HTTP, share with all substreams
43 public static class HTTP extends Stream {
45 public HTTP(String url) { while (url.endsWith("/")) url = url.substring(0, url.length() - 1); this.url = url; }
46 public JS _get(String key) { return new HTTP(url + "/" + key); }
47 public String getCacheKey(Vec path) throws NotCacheableException { return url; }
48 public InputStream getInputStream() throws IOException { return new org.ibex.net.HTTP(url).GET(); }
52 public static class ByteArray extends Stream {
54 private String cacheKey;
55 public ByteArray(byte[] bytes, String cacheKey) { this.bytes = bytes; this.cacheKey = cacheKey; }
56 public String getCacheKey() throws NotCacheableException {
57 if (cacheKey == null) throw new NotCacheableException(); return cacheKey; }
58 public InputStream getInputStream() throws IOException { return new ByteArrayInputStream(bytes); }
59 public JS _get(String key) { return null; }
63 public static class File extends Stream {
65 public File(String path) { this.path = path; }
66 public String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); /* already on disk */ }
67 public InputStream getInputStream() throws IOException { return new FileInputStream(path); }
68 public JS _get(String key) { return new File(path + java.io.File.separatorChar + key); }
71 /** "unwrap" a Zip archive */
72 public static class Zip extends Stream {
75 public Zip(JS parent) { this(parent, null); }
76 public Zip(JS parent, String path) {
77 while(path != null && path.startsWith("/")) path = path.substring(1);
81 public String getCacheKey() throws NotCacheableException { return getCacheKey(parent) + "!zip:"; }
82 public JS _get(String key) { return new Zip(parent, path==null?key:path+'/'+key); }
83 public InputStream getInputStream() throws IOException {
84 InputStream pis = parent.getInputStream();
85 ZipInputStream zis = new ZipInputStream(pis);
86 ZipEntry ze = zis.getNextEntry();
87 while(ze != null && !ze.getName().equals(path)) ze = zis.getNextEntry();
88 if (ze == null) throw new IOException("requested file (" + path + ") not found in archive");
89 return new KnownLength.KnownLengthInputStream(zis, (int)ze.getSize());
93 /** "unwrap" a Cab archive */
94 public static class Cab extends Stream {
97 public Cab(JS parent) { this(parent, null); }
98 public Cab(JS parent, String path) { this.parent = parent; this.path = path; }
99 public String getCacheKey() throws NotCacheableException { return getCacheKey(parent) + "!cab:"; }
100 public JS _get(String key) { return new Cab(parent, path==null?key:path+'/'+key); }
101 public InputStream getInputStream() throws IOException { return new MSPack(parent.getInputStream()).getInputStream(path); }
104 /** the Builtin resource */
105 public static class Builtin extends Stream {
106 public String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); }
107 public InputStream getInputStream() throws IOException { return Platform.getBuiltinInputStream(); }
108 public JS _get(String key) { return null; }
111 /** shadow resource which replaces the graft */
112 public static class ProgressWatcher extends Stream {
115 public ProgressWatcher(JS watchee, JS callback) { this.watchee = watchee; this.callback = callback; }
116 public String getCacheKey() throws NotCacheableException { return getCacheKey(watchee); }
117 public InputStream getInputStream() throws IOException {
118 final InputStream is = watchee.getInputStream();
119 return new FilterInputStream(is) {
120 int bytesDownloaded = 0;
121 public int read() throws IOException {
122 int ret = super.read();
123 if (ret != -1) bytesDownloaded++;
126 public int read(byte[] b, int off, int len) throws IOException {
127 int ret = super.read(b, off, len);
128 if (ret != 1) bytesDownloaded += ret;
129 Scheduler.add(new Task() { public void perform() throws IOException, JSExn {
130 callback.call(N(bytesDownloaded),
131 N(is instanceof KnownLength ? ((KnownLength)is).getLength() : 0), null, null, 2);
137 public JS _get(String s) { return null; }
140 /** subclass from this if you want a CachedInputStream for each path */
141 public static class CachedStream extends Stream {
143 private boolean disk = false;
146 public String getCacheKey() throws NotCacheableException { return key; }
147 CachedInputStream cis = null;
148 public CachedStream(JS p, String s, boolean d) throws NotCacheableException {
149 this.parent = p; this.s = s; this.disk = d; this.key = getCacheKey(p);
151 public InputStream getInputStream() throws IOException {
152 if (cis != null) return cis.getInputStream();
154 cis = new CachedInputStream(parent.getInputStream());
156 // FEATURE: Move LocalStorage into org.ibex.js or move this out
157 java.io.File f = org.ibex.core.LocalStorage.Cache.getCacheFileForKey(key);
158 if (f.exists()) return new FileInputStream(f);
159 cis = new CachedInputStream(parent.getInputStream(), f);
161 return cis.getInputStream();
163 public JS _get(String s) { return null; }