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.*;
10 import org.ibex.util.*;
11 import org.ibex.net.*;
14 * Essentiall an InputStream "factory". You can repeatedly ask a
15 * Fountain for an InputStream, and each InputStream you get back will
16 * be totally independent of the others (ie separate stream position
17 * and state) although they draw from the same data source.
19 public abstract class Fountain extends JS.Obj implements JS.Cloneable {
21 // Public Interface //////////////////////////////////////////////////////////////////////////////
23 public static InputStream getInputStream(Object js) throws IOException { return ((Fountain)((JS)js).unclone()).getInputStream();}
24 public static class NotCacheableException extends Exception { }
26 // streams are "sealed" by default to prevent accidental object leakage
27 private Cache getCache = new Cache(100, true);
28 protected JS _get(JS key) throws JSExn { return null; }
29 public final JS get(JS key) throws JSExn {
30 JS ret = (JS)getCache.get(key);
31 if (ret == null) getCache.put(key, ret = _get(key));
35 // Private Interface //////////////////////////////////////////////////////////////////////////////
37 public abstract InputStream getInputStream() throws IOException;
38 protected String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); }
40 /** HTTP or HTTPS resource */
41 public static class HTTP extends Fountain {
43 //public String toString() { return "Fountain.HTTP:" + url; }
44 public HTTP(String url) { while (url.endsWith("/")) url = url.substring(0, url.length() - 1); this.url = url; }
45 public JS _get(JS key) throws JSExn { return new HTTP(url + "/" + JSU.toString(key)); }
46 public String getCacheKey(Vec path) throws NotCacheableException { return url; }
47 public InputStream getInputStream() throws IOException { return new org.ibex.net.HTTP(url).GET(null, null); }
51 public static class ByteArray extends Fountain {
53 private String cacheKey;
54 public ByteArray(byte[] bytes, String cacheKey) { this.bytes = bytes; this.cacheKey = cacheKey; }
55 public String getCacheKey() throws NotCacheableException {
56 if (cacheKey == null) throw new NotCacheableException(); return cacheKey; }
57 public InputStream getInputStream() throws IOException { return new ByteArrayInputStream(bytes); }
61 public static class File extends Fountain {
63 public File(String path) { this.path = path; }
64 //public String toString() { return "file:" + path; }
65 public String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); /* already on disk */ }
66 public InputStream getInputStream() throws IOException { return new FileInputStream(path); }
67 public JS _get(JS key) throws JSExn { return new File(path + java.io.File.separatorChar + JSU.toString(key)); }
70 /** "unwrap" a Zip archive */
71 public static class Zip extends Fountain {
72 private Fountain parent;
74 public Zip(Fountain parent) { this(parent, null); }
75 public Zip(Fountain parent, String path) {
76 while(path != null && path.startsWith("/")) path = path.substring(1);
80 public String getCacheKey() throws NotCacheableException { return parent.getCacheKey() + "!zip:"; }
81 public JS _get(JS key) throws JSExn { return new Zip(parent, path==null?JSU.toString(key):path+'/'+JSU.toString(key)); }
82 public InputStream getInputStream() throws IOException {
83 InputStream pis = parent.getInputStream();
84 ZipInputStream zis = new ZipInputStream(pis);
85 ZipEntry ze = zis.getNextEntry();
86 while(ze != null && !ze.getName().equals(path)) ze = zis.getNextEntry();
87 if (ze == null) throw new IOException("requested file (" + path + ") not found in archive");
88 return new KnownLength.KnownLengthInputStream(zis, (int)ze.getSize());
92 /** "unwrap" a Cab archive */
94 public static class Cab extends Fountain {
95 private Fountain parent;
97 public Cab(Fountain parent) { this(parent, null); }
98 public Cab(Fountain parent, String path) { this.parent = parent; this.path = path; }
99 public String getCacheKey() throws NotCacheableException { return parent.getCacheKey() + "!cab:"; }
100 public JS _get(JS key) throws JSExn { return new Cab(parent, path==null?(String)key:path+'/'+(String)key); }
101 public InputStream getInputStream() throws IOException { return new MSPack(parent.getInputStream()).getInputStream(path); }
105 public static class FromInputStream extends Fountain {
106 private final InputStream is;
107 public FromInputStream(InputStream is) { this.is = is; }
108 public String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); }
109 public InputStream getInputStream() throws IOException { return is; }
112 /** shadow resource which replaces the graft */
113 public static class ProgressWatcher extends Fountain {
114 private final JS[] callargs = new JS[2];
115 final Fountain watchee;
117 public ProgressWatcher(Fountain watchee, Callable 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 callargs[0] = JSU.N(bytesDownloaded);
132 callargs[1] = JSU.N(is instanceof KnownLength.KnownLengthInputStream ?
133 ((KnownLength.KnownLengthInputStream)is).getLength() : 0);
135 callback.run(callargs);
136 } catch (Exception e) {
137 Log.warn(ProgressWatcher.class, e);
145 /** subclass from this if you want a CachedInputStream for each path */
147 public static class CachedFountain extends Fountain {
148 private Fountain parent;
149 private boolean disk = false;
151 public String getCacheKey() throws NotCacheableException { return key; }
152 CachedInputStream cis = null;
153 public CachedFountain(Fountain p, String s, boolean d) throws NotCacheableException {
154 this.parent = p; this.disk = d; this.key = p.getCacheKey();
156 public InputStream getInputStream() throws IOException {
157 if (cis != null) return cis.getInputStream();
159 cis = new CachedInputStream(parent.getInputStream());
161 java.io.File f = org.ibex.plat.Platform.LocalStorage.Cache.getCacheFileForKey(key);
162 if (f.exists()) return new FileInputStream(f);
163 cis = new CachedInputStream(parent.getInputStream(), f);
165 return cis.getInputStream();