new PixelBuffer API (mainly tons of renames)
[org.ibex.core.git] / src / org / ibex / core / Stream.java
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the GNU General Public License version 2 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 package org.ibex.core;
6
7 import java.io.*;
8 import java.util.zip.*;
9 import org.ibex.js.*;
10 import org.ibex.util.*;
11 import org.ibex.plat.*;
12 import org.ibex.net.*;
13
14 /**
15  *   Essentiall an InputStream "factory".  You can repeatedly ask a
16  *   Stream for an InputStream, and each InputStream you get back will
17  *   be totally independent of the others (ie separate stream position
18  *   and state) although they draw from the same data source.
19  */
20 public abstract class Stream extends JS.Obj implements JS.Cloneable {
21
22     // Public Interface //////////////////////////////////////////////////////////////////////////////
23
24     public static InputStream getInputStream(Object js) throws IOException { return ((Stream)((JS)js).unclone()).getInputStream();}
25     public static class NotCacheableException extends Exception { }
26
27     // streams are "sealed" by default to prevent accidental object leakage
28     public void put(Object key, Object val) { }
29     private Cache getCache = new Cache(100, true);
30     protected JS _get(JS key) throws JSExn { return null; }
31     public final JS get(JS key) throws JSExn {
32         JS ret = (JS)getCache.get(key);
33         if (ret == null) getCache.put(key, ret = _get(key));
34         return ret;
35     }
36
37     // Private Interface //////////////////////////////////////////////////////////////////////////////
38
39     public abstract InputStream getInputStream() throws IOException;
40     protected String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); }
41
42     /** HTTP or HTTPS resource */
43     public static class HTTP extends Stream {
44         private String url;
45         //public String toString() { return "Stream.HTTP:" + url; }
46         public HTTP(String url) { while (url.endsWith("/")) url = url.substring(0, url.length() - 1); this.url = url; }
47         public JS _get(JS key) throws JSExn { return new HTTP(url + "/" + JSU.toString(key)); }
48         public String getCacheKey(Vec path) throws NotCacheableException { return url; }
49         public InputStream getInputStream() throws IOException { return new org.ibex.net.HTTP(url).GET(null, null); }
50     }
51
52     /** byte arrays */
53     public static class ByteArray extends Stream {
54         private byte[] bytes;
55         private String cacheKey;
56         public ByteArray(byte[] bytes, String cacheKey) { this.bytes = bytes; this.cacheKey = cacheKey; }
57         public String getCacheKey() throws NotCacheableException {
58             if (cacheKey == null) throw new NotCacheableException(); return cacheKey; }
59         public InputStream getInputStream() throws IOException { return new ByteArrayInputStream(bytes); }
60     }
61
62     /** a file */
63     public static class File extends Stream {
64         private String path;
65         public File(String path) { this.path = path; }
66         //public String toString() { return "file:" + path; }
67         public String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); /* already on disk */ }
68         public InputStream getInputStream() throws IOException { return new FileInputStream(path); }
69         public JS _get(JS key) throws JSExn {
70             System.out.println("get: " + JSU.str(key));
71             return new File(path + java.io.File.separatorChar + JSU.toString(key)); }
72     }
73
74     /** "unwrap" a Zip archive */
75     public static class Zip extends Stream {
76         private Stream parent;
77         private String path;
78         public Zip(Stream parent) { this(parent, null); }
79         public Zip(Stream parent, String path) {
80             while(path != null && path.startsWith("/")) path = path.substring(1);
81             this.parent = parent;
82             this.path = path;
83         }
84         public String getCacheKey() throws NotCacheableException { return parent.getCacheKey() + "!zip:"; }
85         public JS _get(JS key) throws JSExn { return new Zip(parent, path==null?JSU.toString(key):path+'/'+JSU.toString(key)); }
86         public InputStream getInputStream() throws IOException {
87             InputStream pis = parent.getInputStream();
88             ZipInputStream zis = new ZipInputStream(pis);
89             ZipEntry ze = zis.getNextEntry();
90             while(ze != null && !ze.getName().equals(path)) ze = zis.getNextEntry();
91             if (ze == null) throw new IOException("requested file (" + path + ") not found in archive");
92             return new KnownLength.KnownLengthInputStream(zis, (int)ze.getSize());
93         }
94     }
95
96     /** "unwrap" a Cab archive */
97     /*
98     public static class Cab extends Stream {
99         private Stream parent;
100         private String path;
101         public Cab(Stream parent) { this(parent, null); }
102         public Cab(Stream parent, String path) { this.parent = parent; this.path = path; }
103         public String getCacheKey() throws NotCacheableException { return parent.getCacheKey() + "!cab:"; }
104         public JS _get(JS key) throws JSExn { return new Cab(parent, path==null?(String)key:path+'/'+(String)key); }
105         public InputStream getInputStream() throws IOException { return new MSPack(parent.getInputStream()).getInputStream(path); }
106     }
107     */
108
109     /** the Builtin resource */
110     public static class Builtin extends Stream {
111         public String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); }
112         public InputStream getInputStream() throws IOException { return Platform.getBuiltinInputStream(); }
113     }
114
115     /** the Builtin resource */
116     public static class FromInputStream extends Stream {
117         private final InputStream is;
118         public FromInputStream(InputStream is) { this.is = is; }
119         public String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); }
120         public InputStream getInputStream() throws IOException { return is; }
121     }
122
123     /** shadow resource which replaces the graft */
124     public static class ProgressWatcher extends Stream {
125         private final JS[] callargs = new JS[2];
126         final Stream watchee;
127         JS callback;
128         public ProgressWatcher(Stream watchee, JS callback) { this.watchee = watchee; this.callback = callback; }
129         public String getCacheKey() throws NotCacheableException { return watchee.getCacheKey(); }
130         public InputStream getInputStream() throws IOException {
131             final InputStream is = watchee.getInputStream();
132             return new FilterInputStream(is) {
133                     int bytesDownloaded = 0;
134                     public int read() throws IOException {
135                         int ret = super.read();
136                         if (ret != -1) bytesDownloaded++;
137                         return ret;
138                     }
139                     public int read(byte[] b, int off, int len) throws IOException {
140                         int ret = super.read(b, off, len);
141                         if (ret != 1) bytesDownloaded += ret;
142                         Scheduler.add(new Callable() {
143                             public Object run(Object o) throws IOException, JSExn {
144                                 try {
145                                     int len = is instanceof KnownLength.KnownLengthInputStream ?
146                                                 ((KnownLength.KnownLengthInputStream)is).getLength() : 0;
147                                     callargs[0] = JSU.N(bytesDownloaded);
148                                     callargs[1] = JSU.N(len);
149                                     callback.call(callargs);
150                                 } finally { callargs[0] = callargs[1] = null; }
151                                 return null;
152                         } });
153                         return ret;
154                     }
155                 };
156         }
157     }
158
159     /** subclass from this if you want a CachedInputStream for each path */
160     public static class CachedStream extends Stream {
161         private Stream parent;
162         private boolean disk = false;
163         private String key;
164         public String getCacheKey() throws NotCacheableException { return key; }
165         CachedInputStream cis = null;
166         public CachedStream(Stream p, String s, boolean d) throws NotCacheableException {
167             this.parent = p; this.disk = d; this.key = p.getCacheKey();
168         }
169         public InputStream getInputStream() throws IOException {
170             if (cis != null) return cis.getInputStream();
171             if (!disk) {
172                 cis = new CachedInputStream(parent.getInputStream());
173             } else {
174                 java.io.File f = org.ibex.core.LocalStorage.Cache.getCacheFileForKey(key);
175                 if (f.exists()) return new FileInputStream(f);
176                 cis = new CachedInputStream(parent.getInputStream(), f);
177             }
178             return cis.getInputStream();
179         }
180     }
181 }