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