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