1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
8 import java.util.zip.*;
9 import org.ibex.util.*;
10 import org.ibex.net.*;
13 * Essentiall an InputStream "factory". You can repeatedly ask a
14 * Fountain for an InputStream, and each InputStream you get back will
15 * be totally independent of the others (ie separate stream position
16 * and state) although they draw from the same data source.
18 public abstract class Fountain extends JS.Immutable implements JS.Cloneable {
20 // Public Interface //////////////////////////////////////////////////////////////////////////////
22 public static InputStream getInputStream(Object js) throws IOException { return ((Fountain)((JS)js).unclone()).getInputStream();}
23 public static class NotCacheableException extends Exception { }
25 // streams are "sealed" by default to prevent accidental object leakage
26 private Cache getCache = new Cache(100, true);
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 Fountain {
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(Basket.List path) throws NotCacheableException { return url; }
46 public InputStream getInputStream() throws IOException { return new org.ibex.net.HTTP(url).GET(null, null); }
50 public static class ByteArray extends Fountain {
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 Fountain {
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 Fountain {
71 private Fountain parent;
73 public Zip(Fountain parent) { this(parent, null); }
74 public Zip(Fountain 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 /* temporarily disabled due to dependency issues
93 public static class Cab extends Fountain {
94 private Fountain parent;
96 public Cab(Fountain parent) { this(parent, null); }
97 public Cab(Fountain parent, String path) { this.parent = parent; this.path = path; }
98 public String getCacheKey() throws NotCacheableException { return parent.getCacheKey() + "!cab:"; }
99 public Object _get(Object key) { return new Cab(parent, path==null?(String)key:path+'/'+(String)key); }
100 public InputStream getInputStream() throws IOException { return new MSPack(parent.getInputStream()).getInputStream(path); }
104 /** the Builtin resource */
105 /* temporarily disabled due to dependency issues
106 public static class Builtin extends Fountain {
107 public String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); }
108 public InputStream getInputStream() throws IOException { return Platform.getBuiltinInputStream(); }
112 /** shadow resource which replaces the graft */
113 /* temporarily disabled due to dependency issues
114 public static class ProgressWatcher extends Fountain {
115 final Fountain watchee;
117 public ProgressWatcher(Fountain watchee, JS callback) { this.watchee = watchee; this.callback = callback; }
118 public String getCacheKey() throws NotCacheableException { return watchee.getCacheKey(); }
119 public InputStream getInputStream() throws IOException {
120 final InputStream is = watchee.getInputStream();
121 return new FilterInputStream(is) {
122 int bytesDownloaded = 0;
123 public int read() throws IOException {
124 int ret = super.read();
125 if (ret != -1) bytesDownloaded++;
128 public int read(byte[] b, int off, int len) throws IOException {
129 int ret = super.read(b, off, len);
130 if (ret != 1) bytesDownloaded += ret;
131 Scheduler.add(new Task() { public void perform() throws IOException, JSExn {
132 callback.call(N(bytesDownloaded),
133 N(is instanceof KnownLength ? ((KnownLength)is).getLength() : 0), null, null, 2);
142 /** subclass from this if you want a CachedInputStream for each path */
143 /* temporarily disabled due to dependency issues
144 public static class CachedStream extends Fountain {
145 private Fountain parent;
146 private boolean disk = false;
148 public String getCacheKey() throws NotCacheableException { return key; }
149 CachedInputStream cis = null;
150 public CachedStream(Fountain p, String s, boolean d) throws NotCacheableException {
151 this.parent = p; this.disk = d; this.key = p.getCacheKey();
153 public InputStream getInputStream() throws IOException {
154 if (cis != null) return cis.getInputStream();
156 cis = new CachedInputStream(parent.getInputStream());
158 java.io.File f = org.ibex.core.LocalStorage.Cache.getCacheFileForKey(key);
159 if (f.exists()) return new FileInputStream(f);
160 cis = new CachedInputStream(parent.getInputStream(), f);
162 return cis.getInputStream();