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.*;
9 import org.ibex.core.*;
12 * Essentiall an InputStream "factory". You can repeatedly ask a
13 * Stream for an InputStream, and each InputStream you get back will
14 * be totally independent of the others (ie separate stream position
15 * and state) although they draw from the same data source.
17 public abstract class Stream extends JS.Cloneable {
19 // Public Interface //////////////////////////////////////////////////////////////////////////////
21 public static InputStream getInputStream(Object js) throws IOException { return ((Stream)((JS)js).unclone()).getInputStream();}
22 public static class NotCacheableException extends Exception { }
24 // streams are "sealed" by default to prevent accidental object leakage
25 public void put(Object key, Object val) { }
26 private Cache getCache = new Cache(100);
27 protected Object _get(Object key) { return null; }
28 public final Object get(Object key) {
29 Object ret = getCache.get(key);
30 if (ret == null) getCache.put(key, ret = _get(key));
34 // Private Interface //////////////////////////////////////////////////////////////////////////////
36 public abstract InputStream getInputStream() throws IOException;
37 protected String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); }
39 /** HTTP or HTTPS resource */
40 public static class HTTP extends Stream {
42 public String toString() { return "Stream.HTTP:" + url; }
43 public HTTP(String url) { while (url.endsWith("/")) url = url.substring(0, url.length() - 1); this.url = url; }
44 public Object _get(Object key) { return new HTTP(url + "/" + (String)key); }
45 public String getCacheKey(Vec path) throws NotCacheableException { return url; }
46 public InputStream getInputStream() throws IOException { return new org.ibex.net.HTTP(url).GET(); }
50 public static class ByteArray extends Stream {
52 private String cacheKey;
53 public ByteArray(byte[] bytes, String cacheKey) { this.bytes = bytes; this.cacheKey = cacheKey; }
54 public String getCacheKey() throws NotCacheableException {
55 if (cacheKey == null) throw new NotCacheableException(); return cacheKey; }
56 public InputStream getInputStream() throws IOException { return new ByteArrayInputStream(bytes); }
60 public static class File extends Stream {
62 public File(String path) { this.path = path; }
63 public String toString() { return "file:" + path; }
64 public String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); /* already on disk */ }
65 public InputStream getInputStream() throws IOException { return new FileInputStream(path); }
66 public Object _get(Object key) { return new File(path + java.io.File.separatorChar + (String)key); }
69 /** "unwrap" a Zip archive */
70 public static class Zip extends Stream {
71 private Stream parent;
73 public Zip(Stream parent) { this(parent, null); }
74 public Zip(Stream parent, String path) {
75 while(path != null && path.startsWith("/")) path = path.substring(1);
79 public String getCacheKey() throws NotCacheableException { return parent.getCacheKey() + "!zip:"; }
80 public Object _get(Object key) { return new Zip(parent, path==null?(String)key:path+'/'+(String)key); }
81 public InputStream getInputStream() throws IOException {
82 InputStream pis = parent.getInputStream();
83 ZipInputStream zis = new ZipInputStream(pis);
84 ZipEntry ze = zis.getNextEntry();
85 while(ze != null && !ze.getName().equals(path)) ze = zis.getNextEntry();
86 if (ze == null) throw new IOException("requested file (" + path + ") not found in archive");
87 return new KnownLength.KnownLengthInputStream(zis, (int)ze.getSize());
91 /** "unwrap" a Cab archive */
92 public static class Cab extends Stream {
93 private Stream parent;
95 public Cab(Stream parent) { this(parent, null); }
96 public Cab(Stream parent, String path) { this.parent = parent; this.path = path; }
97 public String getCacheKey() throws NotCacheableException { return parent.getCacheKey() + "!cab:"; }
98 public Object _get(Object key) { return new Cab(parent, path==null?(String)key:path+'/'+(String)key); }
99 public InputStream getInputStream() throws IOException { return new MSPack(parent.getInputStream()).getInputStream(path); }
102 /** the Builtin resource */
103 public static class Builtin extends Stream {
104 public String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); }
105 public InputStream getInputStream() throws IOException { return Platform.getBuiltinInputStream(); }
108 /** shadow resource which replaces the graft */
109 public static class ProgressWatcher extends Stream {
110 final Stream watchee;
112 public ProgressWatcher(Stream watchee, JS callback) { this.watchee = watchee; this.callback = callback; }
113 public String getCacheKey() throws NotCacheableException { return watchee.getCacheKey(); }
114 public InputStream getInputStream() throws IOException {
115 final InputStream is = watchee.getInputStream();
116 return new FilterInputStream(is) {
117 int bytesDownloaded = 0;
118 public int read() throws IOException {
119 int ret = super.read();
120 if (ret != -1) bytesDownloaded++;
123 public int read(byte[] b, int off, int len) throws IOException {
124 int ret = super.read(b, off, len);
125 if (ret != 1) bytesDownloaded += ret;
126 Scheduler.add(new Task() { public void perform() throws IOException, JSExn {
127 callback.call(N(bytesDownloaded),
128 N(is instanceof KnownLength ? ((KnownLength)is).getLength() : 0), null, null, 2);
136 /** subclass from this if you want a CachedInputStream for each path */
137 public static class CachedStream extends Stream {
138 private Stream parent;
139 private boolean disk = false;
141 public String getCacheKey() throws NotCacheableException { return key; }
142 CachedInputStream cis = null;
143 public CachedStream(Stream p, String s, boolean d) throws NotCacheableException {
144 this.parent = p; this.disk = d; this.key = p.getCacheKey();
146 public InputStream getInputStream() throws IOException {
147 if (cis != null) return cis.getInputStream();
149 cis = new CachedInputStream(parent.getInputStream());
151 java.io.File f = org.ibex.core.LocalStorage.Cache.getCacheFileForKey(key);
152 if (f.exists()) return new FileInputStream(f);
153 cis = new CachedInputStream(parent.getInputStream(), f);
155 return cis.getInputStream();