8ed5759a08ecbe1b4c8706aa35c7f54192e69e70
[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.js;
3
4 import java.io.*;
5 import java.util.zip.*;
6 import org.ibex.util.*;
7 import org.ibex.plat.*;
8 import org.ibex.net.*;
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 implements JS.Cloneable {
17
18     // Public Interface //////////////////////////////////////////////////////////////////////////////
19
20     /*public static InputStream getInputStream(JS js) throws IOException { return ((Stream)js.unclone()).getInputStream();}*/
21     public static class NotCacheableException extends Exception { }
22
23     private Cache getCache = new Cache(100);
24     public abstract JS _get(String key);
25     public final JS get(JS key) throws JSExn {
26         JS ret = (JS) getCache.get(key);
27         if (ret == null) getCache.put(key, ret = _get(JS.toString(key)));
28         return ret;
29     }
30
31     // Private Interface //////////////////////////////////////////////////////////////////////////////
32
33     static String getCacheKey(JS s) throws NotCacheableException {
34         if(s instanceof Stream) return ((Stream)s).getCacheKey();
35         throw new NotCacheableException();
36     }
37     
38     public abstract InputStream getInputStream() throws IOException;
39     protected String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); }
40
41     /** HTTP or HTTPS resource */
42     // FEATURE: Only instansiate only ibex.net.HTTP, share with all substreams
43     public static class HTTP extends Stream {
44         private String url;
45         public HTTP(String url) { while (url.endsWith("/")) url = url.substring(0, url.length() - 1); this.url = url; }
46         public JS _get(String key) { return new HTTP(url + "/" + 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(); }
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         public JS _get(String key) { return null; }
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 getCacheKey() throws NotCacheableException { throw new NotCacheableException(); /* already on disk */ }
67         public InputStream getInputStream() throws IOException { return new FileInputStream(path); }
68         public JS _get(String key) { return new File(path + java.io.File.separatorChar + key); }
69     }
70
71     /** "unwrap" a Zip archive */
72     public static class Zip extends Stream {
73         private JS parent;
74         private String path;
75         public Zip(JS parent) { this(parent, null); }
76         public Zip(JS parent, String path) {
77             while(path != null && path.startsWith("/")) path = path.substring(1);
78             this.parent = parent;
79             this.path = path;
80         }
81         public String getCacheKey() throws NotCacheableException { return getCacheKey(parent) + "!zip:"; }
82         public JS _get(String key) { return new Zip(parent, path==null?key:path+'/'+key); }
83         public InputStream getInputStream() throws IOException {
84             InputStream pis = parent.getInputStream();
85             ZipInputStream zis = new ZipInputStream(pis);
86             ZipEntry ze = zis.getNextEntry();
87             while(ze != null && !ze.getName().equals(path)) ze = zis.getNextEntry();
88             if (ze == null) throw new IOException("requested file (" + path + ") not found in archive");
89             return new KnownLength.KnownLengthInputStream(zis, (int)ze.getSize());
90         }
91     }
92
93     /** "unwrap" a Cab archive */
94     public static class Cab extends Stream {
95         private JS parent;
96         private String path;
97         public Cab(JS parent) { this(parent, null); }
98         public Cab(JS parent, String path) { this.parent = parent; this.path = path; }
99         public String getCacheKey() throws NotCacheableException { return getCacheKey(parent) + "!cab:"; }
100         public JS _get(String key) { return new Cab(parent, path==null?key:path+'/'+key); }
101         public InputStream getInputStream() throws IOException { return new MSPack(parent.getInputStream()).getInputStream(path); }
102     }
103
104     /** the Builtin resource */
105     public static class Builtin extends Stream {
106         public String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); }
107         public InputStream getInputStream() throws IOException { return Platform.getBuiltinInputStream(); }
108         public JS _get(String key) { return null; }
109     }
110
111     /** shadow resource which replaces the graft */
112     public static class ProgressWatcher extends Stream {
113         final JS watchee;
114         JS callback;
115         public ProgressWatcher(JS watchee, JS callback) { this.watchee = watchee; this.callback = callback; }
116         public String getCacheKey() throws NotCacheableException { return getCacheKey(watchee); }
117         public InputStream getInputStream() throws IOException {
118             final InputStream is = watchee.getInputStream();
119             return new FilterInputStream(is) {
120                     int bytesDownloaded = 0;
121                     public int read() throws IOException {
122                         int ret = super.read();
123                         if (ret != -1) bytesDownloaded++;
124                         return ret;
125                     }
126                     public int read(byte[] b, int off, int len) throws IOException {
127                         int ret = super.read(b, off, len);
128                         if (ret != 1) bytesDownloaded += ret;
129                         Scheduler.add(new Task() { public void perform() throws IOException, JSExn {
130                             callback.call(N(bytesDownloaded),
131                                           N(is instanceof KnownLength ? ((KnownLength)is).getLength() : 0), null, null, 2);
132                         } });
133                         return ret;
134                     }
135                 };
136         }
137         public JS _get(String s) { return null; }
138     }
139
140     /** subclass from this if you want a CachedInputStream for each path */
141     public static class CachedStream extends Stream {
142         private JS parent;
143         private boolean disk = false;
144         private String key;
145         private String s;
146         public String getCacheKey() throws NotCacheableException { return key; }
147         CachedInputStream cis = null;
148         public CachedStream(JS p, String s, boolean d) throws NotCacheableException {
149             this.parent = p; this.s = s; this.disk = d; this.key = getCacheKey(p);
150         }
151         public InputStream getInputStream() throws IOException {
152             if (cis != null) return cis.getInputStream();
153             if (!disk) {
154                 cis = new CachedInputStream(parent.getInputStream());
155             } else {
156                 // FEATURE: Move LocalStorage into org.ibex.js or move this out
157                 java.io.File f = org.ibex.core.LocalStorage.Cache.getCacheFileForKey(key);
158                 if (f.exists()) return new FileInputStream(f);
159                 cis = new CachedInputStream(parent.getInputStream(), f);
160             }
161             return cis.getInputStream();
162         }
163         public JS _get(String s) { return null; }
164     }
165 }