1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
5 import java.util.zip.*;
7 import org.ibex.util.*;
8 import org.ibex.plat.*;
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.O implements 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 JS _get(JS key) throws JSExn { return null; }
28 public final JS get(JS key) throws JSExn {
29 JS ret = (JS)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 JS _get(JS key) throws JSExn { return new HTTP(url + "/" + JS.toString(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(null, null); }
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 JS _get(JS key) throws JSExn {
67 System.out.println("get: " + JS.debugToString(key));
68 return new File(path + java.io.File.separatorChar + JS.toString(key)); }
71 /** "unwrap" a Zip archive */
72 public static class Zip extends Stream {
73 private Stream parent;
75 public Zip(Stream parent) { this(parent, null); }
76 public Zip(Stream parent, String path) {
77 while(path != null && path.startsWith("/")) path = path.substring(1);
81 public String getCacheKey() throws NotCacheableException { return parent.getCacheKey() + "!zip:"; }
82 public JS _get(JS key) throws JSExn { return new Zip(parent, path==null?JS.toString(key):path+'/'+JS.toString(key)); }
83 public InputStream getInputStream() throws IOException {
84 InputStream pis = parent.getInputStream();
85 ZipInputStream zis = new ZipInputStream(pis);
86 ZipEntry ze = zis.getNextEntry();
87 while(ze != null && !ze.getName().equals(path)) ze = zis.getNextEntry();
88 if (ze == null) throw new IOException("requested file (" + path + ") not found in archive");
89 return new KnownLength.KnownLengthInputStream(zis, (int)ze.getSize());
93 /** "unwrap" a Cab archive */
95 public static class Cab extends Stream {
96 private Stream parent;
98 public Cab(Stream parent) { this(parent, null); }
99 public Cab(Stream parent, String path) { this.parent = parent; this.path = path; }
100 public String getCacheKey() throws NotCacheableException { return parent.getCacheKey() + "!cab:"; }
101 public JS _get(JS key) throws JSExn { return new Cab(parent, path==null?(String)key:path+'/'+(String)key); }
102 public InputStream getInputStream() throws IOException { return new MSPack(parent.getInputStream()).getInputStream(path); }
106 /** the Builtin resource */
107 public static class Builtin extends Stream {
108 public String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); }
109 public InputStream getInputStream() throws IOException { return Platform.getBuiltinInputStream(); }
112 /** shadow resource which replaces the graft */
113 public static class ProgressWatcher extends Stream {
114 final Stream watchee;
116 public ProgressWatcher(Stream watchee, JS callback) { this.watchee = watchee; this.callback = callback; }
117 public String getCacheKey() throws NotCacheableException { return watchee.getCacheKey(); }
118 public InputStream getInputStream() throws IOException {
119 final InputStream is = watchee.getInputStream();
120 return new FilterInputStream(is) {
121 int bytesDownloaded = 0;
122 public int read() throws IOException {
123 int ret = super.read();
124 if (ret != -1) bytesDownloaded++;
127 public int read(byte[] b, int off, int len) throws IOException {
128 int ret = super.read(b, off, len);
129 if (ret != 1) bytesDownloaded += ret;
130 Scheduler.add(new Task() { public void perform() throws IOException, JSExn {
131 callback.call(N(bytesDownloaded),
132 N(is instanceof KnownLength ? ((KnownLength)is).getLength() : 0), null, null, 2);
140 /** subclass from this if you want a CachedInputStream for each path */
141 public static class CachedStream extends Stream {
142 private Stream parent;
143 private boolean disk = false;
145 public String getCacheKey() throws NotCacheableException { return key; }
146 CachedInputStream cis = null;
147 public CachedStream(Stream p, String s, boolean d) throws NotCacheableException {
148 this.parent = p; this.disk = d; this.key = p.getCacheKey();
150 public InputStream getInputStream() throws IOException {
151 if (cis != null) return cis.getInputStream();
153 cis = new CachedInputStream(parent.getInputStream());
155 java.io.File f = org.ibex.core.LocalStorage.Cache.getCacheFileForKey(key);
156 if (f.exists()) return new FileInputStream(f);
157 cis = new CachedInputStream(parent.getInputStream(), f);
159 return cis.getInputStream();