fix nasty bug in numerical comparison in Interpreter
[org.ibex.js.git] / src / org / ibex / js / Fountain.java
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.
4
5 package org.ibex.js;
6
7 import java.io.*;
8 import java.util.zip.*;
9 import org.ibex.js.*;
10 import org.ibex.util.*;
11 import org.ibex.net.*;
12
13 /**
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.
18  */
19 public abstract class Fountain extends JS.Obj implements JS.Cloneable {
20
21     // Public Interface //////////////////////////////////////////////////////////////////////////////
22
23     public static InputStream getInputStream(Object js) throws IOException { return ((Fountain)((JS)js).unclone()).getInputStream();}
24     public static class NotCacheableException extends Exception { }
25
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));
32         return ret;
33     }
34
35     // Private Interface //////////////////////////////////////////////////////////////////////////////
36
37     public abstract InputStream getInputStream() throws IOException;
38     protected String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); }
39
40     /** HTTP or HTTPS resource */
41     public static class HTTP extends Fountain {
42         private String url;
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); }
48     }
49
50     /** byte arrays */
51     public static class ByteArray extends Fountain {
52         private byte[] bytes;
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); }
58     }
59
60     /** a file */
61     public static class File extends Fountain {
62         private String path;
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)); }
68     }
69
70     /** "unwrap" a Zip archive */
71     public static class Zip extends Fountain {
72         private Fountain parent;
73         private String path;
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);
77             this.parent = parent;
78             this.path = path;
79         }
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());
89         }
90     }
91
92     /** "unwrap" a Cab archive */
93     /*
94     public static class Cab extends Fountain {
95         private Fountain parent;
96         private String path;
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); }
102     }
103     */
104
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; }
110     }
111
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;
116         Callable callback;
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++;
126                         return ret;
127                     }
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);
134                         try {
135                             callback.run(callargs);
136                         } catch (Exception e) {
137                             Log.warn(ProgressWatcher.class, e);
138                         }
139                         return ret;
140                     }
141                 };
142         }
143     }
144
145     /** subclass from this if you want a CachedInputStream for each path */
146     /*
147     public static class CachedFountain extends Fountain {
148         private Fountain parent;
149         private boolean disk = false;
150         private String key;
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();
155         }
156         public InputStream getInputStream() throws IOException {
157             if (cis != null) return cis.getInputStream();
158             if (!disk) {
159                 cis = new CachedInputStream(parent.getInputStream());
160             } else {
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);
164             }
165             return cis.getInputStream();
166         }
167     }
168     */
169 }