1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the GNU General Public License version 2 ("the License").
3 // You may not use this file except in compliance with the License.
8 import java.util.zip.*;
10 import org.ibex.util.*;
11 import org.ibex.plat.*;
12 import org.ibex.net.*;
15 * Essentiall an InputStream "factory". You can repeatedly ask a
16 * Stream for an InputStream, and each InputStream you get back will
17 * be totally independent of the others (ie separate stream position
18 * and state) although they draw from the same data source.
20 public abstract class Stream extends JS.Obj implements JS.Cloneable {
22 // Public Interface //////////////////////////////////////////////////////////////////////////////
24 public static InputStream getInputStream(Object js) throws IOException { return ((Stream)((JS)js).unclone()).getInputStream();}
25 public static class NotCacheableException extends Exception { }
27 // streams are "sealed" by default to prevent accidental object leakage
28 public void put(Object key, Object val) { }
29 private Cache getCache = new Cache(100);
30 protected JS _get(JS key) throws JSExn { return null; }
31 public final JS get(JS key) throws JSExn {
32 JS ret = (JS)getCache.get(key);
33 if (ret == null) getCache.put(key, ret = _get(key));
37 // Private Interface //////////////////////////////////////////////////////////////////////////////
39 public abstract InputStream getInputStream() throws IOException;
40 protected String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); }
42 /** HTTP or HTTPS resource */
43 public static class HTTP extends Stream {
45 //public String toString() { return "Stream.HTTP:" + url; }
46 public HTTP(String url) { while (url.endsWith("/")) url = url.substring(0, url.length() - 1); this.url = url; }
47 public JS _get(JS key) throws JSExn { return new HTTP(url + "/" + JS.toString(key)); }
48 public String getCacheKey(Vec path) throws NotCacheableException { return url; }
49 public InputStream getInputStream() throws IOException { return new org.ibex.net.HTTP(url).GET(null, null); }
53 public static class ByteArray extends Stream {
55 private String cacheKey;
56 public ByteArray(byte[] bytes, String cacheKey) { this.bytes = bytes; this.cacheKey = cacheKey; }
57 public String getCacheKey() throws NotCacheableException {
58 if (cacheKey == null) throw new NotCacheableException(); return cacheKey; }
59 public InputStream getInputStream() throws IOException { return new ByteArrayInputStream(bytes); }
63 public static class File extends Stream {
65 public File(String path) { this.path = path; }
66 //public String toString() { return "file:" + path; }
67 public String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); /* already on disk */ }
68 public InputStream getInputStream() throws IOException { return new FileInputStream(path); }
69 public JS _get(JS key) throws JSExn {
70 System.out.println("get: " + JS.debugToString(key));
71 return new File(path + java.io.File.separatorChar + JS.toString(key)); }
74 /** "unwrap" a Zip archive */
75 public static class Zip extends Stream {
76 private Stream parent;
78 public Zip(Stream parent) { this(parent, null); }
79 public Zip(Stream parent, String path) {
80 while(path != null && path.startsWith("/")) path = path.substring(1);
84 public String getCacheKey() throws NotCacheableException { return parent.getCacheKey() + "!zip:"; }
85 public JS _get(JS key) throws JSExn { return new Zip(parent, path==null?JS.toString(key):path+'/'+JS.toString(key)); }
86 public InputStream getInputStream() throws IOException {
87 InputStream pis = parent.getInputStream();
88 ZipInputStream zis = new ZipInputStream(pis);
89 ZipEntry ze = zis.getNextEntry();
90 while(ze != null && !ze.getName().equals(path)) ze = zis.getNextEntry();
91 if (ze == null) throw new IOException("requested file (" + path + ") not found in archive");
92 return new KnownLength.KnownLengthInputStream(zis, (int)ze.getSize());
96 /** "unwrap" a Cab archive */
98 public static class Cab extends Stream {
99 private Stream parent;
101 public Cab(Stream parent) { this(parent, null); }
102 public Cab(Stream parent, String path) { this.parent = parent; this.path = path; }
103 public String getCacheKey() throws NotCacheableException { return parent.getCacheKey() + "!cab:"; }
104 public JS _get(JS key) throws JSExn { return new Cab(parent, path==null?(String)key:path+'/'+(String)key); }
105 public InputStream getInputStream() throws IOException { return new MSPack(parent.getInputStream()).getInputStream(path); }
109 /** the Builtin resource */
110 public static class Builtin extends Stream {
111 public String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); }
112 public InputStream getInputStream() throws IOException { return Platform.getBuiltinInputStream(); }
115 /** the Builtin resource */
116 public static class FromInputStream extends Stream {
117 private final InputStream is;
118 public FromInputStream(InputStream is) { this.is = is; }
119 public String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); }
120 public InputStream getInputStream() throws IOException { return is; }
123 /** shadow resource which replaces the graft */
124 public static class ProgressWatcher extends Stream {
125 final Stream watchee;
127 public ProgressWatcher(Stream watchee, JS callback) { this.watchee = watchee; this.callback = callback; }
128 public String getCacheKey() throws NotCacheableException { return watchee.getCacheKey(); }
129 public InputStream getInputStream() throws IOException {
130 final InputStream is = watchee.getInputStream();
131 return new FilterInputStream(is) {
132 int bytesDownloaded = 0;
133 public int read() throws IOException {
134 int ret = super.read();
135 if (ret != -1) bytesDownloaded++;
138 public int read(byte[] b, int off, int len) throws IOException {
139 int ret = super.read(b, off, len);
140 if (ret != 1) bytesDownloaded += ret;
141 Scheduler.add(new Task() { public void perform() throws IOException, JSExn {
142 callback.call(N(bytesDownloaded),
143 N(is instanceof KnownLength ? ((KnownLength)is).getLength() : 0), null, null, 2);
151 /** subclass from this if you want a CachedInputStream for each path */
152 public static class CachedStream extends Stream {
153 private Stream parent;
154 private boolean disk = false;
156 public String getCacheKey() throws NotCacheableException { return key; }
157 CachedInputStream cis = null;
158 public CachedStream(Stream p, String s, boolean d) throws NotCacheableException {
159 this.parent = p; this.disk = d; this.key = p.getCacheKey();
161 public InputStream getInputStream() throws IOException {
162 if (cis != null) return cis.getInputStream();
164 cis = new CachedInputStream(parent.getInputStream());
166 java.io.File f = org.ibex.core.LocalStorage.Cache.getCacheFileForKey(key);
167 if (f.exists()) return new FileInputStream(f);
168 cis = new CachedInputStream(parent.getInputStream(), f);
170 return cis.getInputStream();