licensing update to APSL 2.0
[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.util.*;
10 import org.ibex.net.*;
11
12 /**
13  *   Essentiall an InputStream "factory".  You can repeatedly ask a
14  *   Fountain for an InputStream, and each InputStream you get back will
15  *   be totally independent of the others (ie separate stream position
16  *   and state) although they draw from the same data source.
17  */
18 public abstract class Fountain extends JS.O implements JS.Cloneable {
19
20     // Public Interface //////////////////////////////////////////////////////////////////////////////
21
22     public static InputStream getInputStream(Object js) throws IOException { return ((Fountain)((JS)js).unclone()).getInputStream();}
23     public static class NotCacheableException extends Exception { }
24
25     // streams are "sealed" by default to prevent accidental object leakage
26     public void put(Object key, Object val) { }
27     private Cache getCache = new Cache(100);
28     protected Object _get(Object key) { return null; }
29     public final Object get(Object key) {
30         Object ret = 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 "Stream.HTTP:" + url; }
44         public HTTP(String url) { while (url.endsWith("/")) url = url.substring(0, url.length() - 1); this.url = url; }
45         public Object _get(Object key) { return new HTTP(url + "/" + (String)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 Object _get(Object key) { return new File(path + java.io.File.separatorChar + (String)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 Object _get(Object key) { return new Zip(parent, path==null?(String)key:path+'/'+(String)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     /* temporarily disabled due to dependency issues
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 Object _get(Object key) { 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     /** the Builtin resource */
106     /* temporarily disabled due to dependency issues
107     public static class Builtin extends Fountain {
108         public String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); }
109         public InputStream getInputStream() throws IOException { return Platform.getBuiltinInputStream(); }
110     }
111     */
112
113     /** shadow resource which replaces the graft */
114     /* temporarily disabled due to dependency issues
115     public static class ProgressWatcher extends Fountain {
116         final Fountain watchee;
117         JS callback;
118         public ProgressWatcher(Fountain watchee, JS callback) { this.watchee = watchee; this.callback = callback; }
119         public String getCacheKey() throws NotCacheableException { return watchee.getCacheKey(); }
120         public InputStream getInputStream() throws IOException {
121             final InputStream is = watchee.getInputStream();
122             return new FilterInputStream(is) {
123                     int bytesDownloaded = 0;
124                     public int read() throws IOException {
125                         int ret = super.read();
126                         if (ret != -1) bytesDownloaded++;
127                         return ret;
128                     }
129                     public int read(byte[] b, int off, int len) throws IOException {
130                         int ret = super.read(b, off, len);
131                         if (ret != 1) bytesDownloaded += ret;
132                         Scheduler.add(new Task() { public void perform() throws IOException, JSExn {
133                             callback.call(N(bytesDownloaded),
134                                           N(is instanceof KnownLength ? ((KnownLength)is).getLength() : 0), null, null, 2);
135                         } });
136                         return ret;
137                     }
138                 };
139         }
140     }
141     */
142
143     /** subclass from this if you want a CachedInputStream for each path */
144     /* temporarily disabled due to dependency issues
145     public static class CachedStream extends Fountain {
146         private Fountain parent;
147         private boolean disk = false;
148         private String key;
149         public String getCacheKey() throws NotCacheableException { return key; }
150         CachedInputStream cis = null;
151         public CachedStream(Fountain p, String s, boolean d) throws NotCacheableException {
152             this.parent = p; this.disk = d; this.key = p.getCacheKey();
153         }
154         public InputStream getInputStream() throws IOException {
155             if (cis != null) return cis.getInputStream();
156             if (!disk) {
157                 cis = new CachedInputStream(parent.getInputStream());
158             } else {
159                 java.io.File f = org.ibex.core.LocalStorage.Cache.getCacheFileForKey(key);
160                 if (f.exists()) return new FileInputStream(f);
161                 cis = new CachedInputStream(parent.getInputStream(), f);
162             }
163             return cis.getInputStream();
164         }
165     }
166     */
167 }