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