remove JS.call(JS[]), JS.getInputStream()
[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     private Cache getCache = new Cache(100, true);
29     protected JS _get(JS key) throws JSExn { return null; }
30     public final JS get(JS key) throws JSExn {
31         JS ret = (JS)getCache.get(key);
32         if (ret == null) getCache.put(key, ret = _get(key));
33         return ret;
34     }
35
36     // Private Interface //////////////////////////////////////////////////////////////////////////////
37
38     public abstract InputStream getInputStream() throws IOException;
39     protected String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); }
40
41     /** HTTP or HTTPS resource */
42     public static class HTTP extends Stream {
43         private String url;
44         //public String toString() { return "Stream.HTTP:" + url; }
45         public HTTP(String url) { while (url.endsWith("/")) url = url.substring(0, url.length() - 1); this.url = url; }
46         public JS _get(JS key) throws JSExn { return new HTTP(url + "/" + JSU.toString(key)); }
47         public String getCacheKey(Vec path) throws NotCacheableException { return url; }
48         public InputStream getInputStream() throws IOException { return new org.ibex.net.HTTP(url).GET(null, null); }
49     }
50
51     /** byte arrays */
52     public static class ByteArray extends Stream {
53         private byte[] bytes;
54         private String cacheKey;
55         public ByteArray(byte[] bytes, String cacheKey) { this.bytes = bytes; this.cacheKey = cacheKey; }
56         public String getCacheKey() throws NotCacheableException {
57             if (cacheKey == null) throw new NotCacheableException(); return cacheKey; }
58         public InputStream getInputStream() throws IOException { return new ByteArrayInputStream(bytes); }
59     }
60
61     /** a file */
62     public static class File extends Stream {
63         private String path;
64         public File(String path) { this.path = path; }
65         //public String toString() { return "file:" + path; }
66         public String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); /* already on disk */ }
67         public InputStream getInputStream() throws IOException { return new FileInputStream(path); }
68         public JS _get(JS key) throws JSExn {
69             System.out.println("get: " + JSU.str(key));
70             return new File(path + java.io.File.separatorChar + JSU.toString(key)); }
71     }
72
73     /** "unwrap" a Zip archive */
74     public static class Zip extends Stream {
75         private Stream parent;
76         private String path;
77         public Zip(Stream parent) { this(parent, null); }
78         public Zip(Stream parent, String path) {
79             while(path != null && path.startsWith("/")) path = path.substring(1);
80             this.parent = parent;
81             this.path = path;
82         }
83         public String getCacheKey() throws NotCacheableException { return parent.getCacheKey() + "!zip:"; }
84         public JS _get(JS key) throws JSExn { return new Zip(parent, path==null?JSU.toString(key):path+'/'+JSU.toString(key)); }
85         public InputStream getInputStream() throws IOException {
86             InputStream pis = parent.getInputStream();
87             ZipInputStream zis = new ZipInputStream(pis);
88             ZipEntry ze = zis.getNextEntry();
89             while(ze != null && !ze.getName().equals(path)) ze = zis.getNextEntry();
90             if (ze == null) throw new IOException("requested file (" + path + ") not found in archive");
91             return new KnownLength.KnownLengthInputStream(zis, (int)ze.getSize());
92         }
93     }
94
95     /** "unwrap" a Cab archive */
96     /*
97     public static class Cab extends Stream {
98         private Stream parent;
99         private String path;
100         public Cab(Stream parent) { this(parent, null); }
101         public Cab(Stream parent, String path) { this.parent = parent; this.path = path; }
102         public String getCacheKey() throws NotCacheableException { return parent.getCacheKey() + "!cab:"; }
103         public JS _get(JS key) throws JSExn { return new Cab(parent, path==null?(String)key:path+'/'+(String)key); }
104         public InputStream getInputStream() throws IOException { return new MSPack(parent.getInputStream()).getInputStream(path); }
105     }
106     */
107
108     /** the Builtin resource */
109     public static class Builtin extends Stream {
110         public String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); }
111         public InputStream getInputStream() throws IOException { return Platform.getBuiltinInputStream(); }
112     }
113
114     /** the Builtin resource */
115     public static class FromInputStream extends Stream {
116         private final InputStream is;
117         public FromInputStream(InputStream is) { this.is = is; }
118         public String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); }
119         public InputStream getInputStream() throws IOException { return is; }
120     }
121
122     /** shadow resource which replaces the graft */
123     public static class ProgressWatcher extends Stream {
124         private final JS[] callargs = new JS[2];
125         final Stream watchee;
126         JS callback;
127         public ProgressWatcher(Stream watchee, JS callback) { this.watchee = watchee; this.callback = callback; }
128         public String getCacheKey() throws NotCacheableException { return watchee.getCacheKey(); }
129         public InputStream getInputStream() throws IOException {
130             final InputStream is = watchee.getInputStream();
131             return new FilterInputStream(is) {
132                     int bytesDownloaded = 0;
133                     public int read() throws IOException {
134                         int ret = super.read();
135                         if (ret != -1) bytesDownloaded++;
136                         return ret;
137                     }
138                     public int read(byte[] b, int off, int len) throws IOException {
139                         int ret = super.read(b, off, len);
140                         if (ret != 1) bytesDownloaded += ret;
141                         Scheduler.add(new Callable() {
142                             public Object run(Object o) throws IOException, JSExn {
143                                 try {
144                                     int len = is instanceof KnownLength.KnownLengthInputStream ?
145                                                 ((KnownLength.KnownLengthInputStream)is).getLength() : 0;
146                                     callargs[0] = JSU.N(bytesDownloaded);
147                                     callargs[1] = JSU.N(len);
148                                     callback.call(null, callargs);
149                                 } finally { callargs[0] = callargs[1] = null; }
150                                 return null;
151                         } });
152                         return ret;
153                     }
154                 };
155         }
156     }
157
158     /** subclass from this if you want a CachedInputStream for each path */
159     public static class CachedStream extends Stream {
160         private Stream parent;
161         private boolean disk = false;
162         private String key;
163         public String getCacheKey() throws NotCacheableException { return key; }
164         CachedInputStream cis = null;
165         public CachedStream(Stream p, String s, boolean d) throws NotCacheableException {
166             this.parent = p; this.disk = d; this.key = p.getCacheKey();
167         }
168         public InputStream getInputStream() throws IOException {
169             if (cis != null) return cis.getInputStream();
170             if (!disk) {
171                 cis = new CachedInputStream(parent.getInputStream());
172             } else {
173                 java.io.File f = org.ibex.core.LocalStorage.Cache.getCacheFileForKey(key);
174                 if (f.exists()) return new FileInputStream(f);
175                 cis = new CachedInputStream(parent.getInputStream(), f);
176             }
177             return cis.getInputStream();
178         }
179     }
180 }