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(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) { }
25 private Cache getCache = new Cache(100);
26 protected Object _get(Object key) { return null; }
27 public final Object get(Object key) {
28 Object ret = getCache.get(key);
29 if (ret == null) getCache.put(key, ret = _get(key));
33 // Private Interface //////////////////////////////////////////////////////////////////////////////
35 public abstract InputStream getInputStream() throws IOException;
36 protected String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); }
38 /** HTTP or HTTPS resource */
39 public static class HTTP extends Stream {
41 public String toString() { return "Stream.HTTP:" + url; }
42 public HTTP(String url) { while (url.endsWith("/")) url = url.substring(0, url.length() - 1); this.url = url; }
43 public Object _get(Object key) { return new HTTP(url + "/" + (String)key); }
44 public String getCacheKey(Vec path) throws NotCacheableException { return url; }
45 public InputStream getInputStream() throws IOException { return new org.ibex.net.HTTP(url).GET(); }
49 public static class ByteArray extends Stream {
51 private String cacheKey;
52 public ByteArray(byte[] bytes, String cacheKey) { this.bytes = bytes; this.cacheKey = cacheKey; }
53 public String getCacheKey() throws NotCacheableException {
54 if (cacheKey == null) throw new NotCacheableException(); return cacheKey; }
55 public InputStream getInputStream() throws IOException { return new ByteArrayInputStream(bytes); }
59 public static class File extends Stream {
61 public File(String path) { this.path = path; }
62 public String toString() { return "file:" + path; }
63 public String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); /* already on disk */ }
64 public InputStream getInputStream() throws IOException { return new FileInputStream(path); }
65 public Object _get(Object key) { return new File(path + java.io.File.separatorChar + (String)key); }
68 /** "unwrap" a Zip archive */
69 public static class Zip extends Stream {
70 private Stream parent;
72 public Zip(Stream parent) { this(parent, null); }
73 public Zip(Stream parent, String path) {
74 while(path != null && path.startsWith("/")) path = path.substring(1);
78 public String getCacheKey() throws NotCacheableException { return parent.getCacheKey() + "!zip:"; }
79 public Object _get(Object key) { return new Zip(parent, path==null?(String)key:path+'/'+(String)key); }
80 public InputStream getInputStream() throws IOException {
81 InputStream pis = parent.getInputStream();
82 ZipInputStream zis = new ZipInputStream(pis);
83 ZipEntry ze = zis.getNextEntry();
84 while(ze != null && !ze.getName().equals(path)) ze = zis.getNextEntry();
85 if (ze == null) throw new IOException("requested file (" + path + ") not found in archive");
86 return new KnownLength.KnownLengthInputStream(zis, (int)ze.getSize());
90 /** "unwrap" a Cab archive */
91 public static class Cab extends Stream {
92 private Stream parent;
94 public Cab(Stream parent) { this(parent, null); }
95 public Cab(Stream parent, String path) { this.parent = parent; this.path = path; }
96 public String getCacheKey() throws NotCacheableException { return parent.getCacheKey() + "!cab:"; }
97 public Object _get(Object key) { return new Cab(parent, path==null?(String)key:path+'/'+(String)key); }
98 public InputStream getInputStream() throws IOException { return new MSPack(parent.getInputStream()).getInputStream(path); }
101 /** the Builtin resource */
102 public static class Builtin extends Stream {
103 public String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); }
104 public InputStream getInputStream() throws IOException { return Platform.getBuiltinInputStream(); }
107 /** shadow resource which replaces the graft */
108 public static class ProgressWatcher extends Stream {
109 final Stream watchee;
111 public ProgressWatcher(Stream watchee, JS callback) { this.watchee = watchee; this.callback = callback; }
112 public String getCacheKey() throws NotCacheableException { return watchee.getCacheKey(); }
113 public InputStream getInputStream() throws IOException {
114 final InputStream is = watchee.getInputStream();
115 return new FilterInputStream(is) {
116 int bytesDownloaded = 0;
117 public int read() throws IOException {
118 int ret = super.read();
119 if (ret != -1) bytesDownloaded++;
122 public int read(byte[] b, int off, int len) throws IOException {
123 int ret = super.read(b, off, len);
124 if (ret != 1) bytesDownloaded += ret;
125 Scheduler.add(new Task() { public void perform() throws IOException, JSExn {
126 callback.call(N(bytesDownloaded),
127 N(is instanceof KnownLength ? ((KnownLength)is).getLength() : 0), null, null, 2);
135 /** subclass from this if you want a CachedInputStream for each path */
136 public static class CachedStream extends Stream {
137 private Stream parent;
138 private boolean disk = false;
140 public String getCacheKey() throws NotCacheableException { return key; }
141 CachedInputStream cis = null;
142 public CachedStream(Stream p, String s, boolean d) throws NotCacheableException {
143 this.parent = p; this.disk = d; this.key = p.getCacheKey();
145 public InputStream getInputStream() throws IOException {
146 if (cis != null) return cis.getInputStream();
148 cis = new CachedInputStream(parent.getInputStream());
150 java.io.File f = org.ibex.core.LocalStorage.Cache.getCacheFileForKey(key);
151 if (f.exists()) return new FileInputStream(f);
152 cis = new CachedInputStream(parent.getInputStream(), f);
154 return cis.getInputStream();