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