1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
6 import java.util.zip.*;
9 import org.xwt.translators.MSPack;
10 import org.bouncycastle.util.encoders.Base64;
12 /** Base class for XWT resources */
13 public abstract class Stream extends JS.Cloneable {
15 // Public Interface //////////////////////////////////////////////////////////////////////////////
17 public static InputStream getInputStream(Object js) throws IOException { return ((Stream)((JS)js).unclone()).getInputStream();}
18 public static class NotCacheableException extends Exception { }
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; }
24 // Private Interface //////////////////////////////////////////////////////////////////////////////
26 protected abstract InputStream getInputStream() throws IOException;
27 protected String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); }
29 /** HTTP or HTTPS resource */
30 public static class HTTP extends Stream {
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(); }
39 public static class ByteArray extends Stream {
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); }
48 public static class File extends Stream {
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); }
57 /** "unwrap" a Zip archive */
58 public static class Zip extends Stream {
59 private Stream parent;
61 Zip(Stream parent) { this(parent, null); }
62 Zip(Stream parent, String path) {
64 while (path != null && path.startsWith("/")) path = path.substring(1);
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());
79 /** "unwrap" a Cab archive */
80 public static class Cab extends Stream {
81 private Stream parent;
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); }
90 /** the Builtin resource */
91 public static class Builtin extends Stream {
93 public String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); }
94 public InputStream getInputStream() throws IOException { return Platform.getBuiltinInputStream(); }
97 /** shadow resource which replaces the graft */
98 public static class ProgressWatcher extends Stream {
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++;
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);
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;
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();
135 public InputStream getInputStream() throws IOException {
136 if (cis != null) return cis.getInputStream();
138 cis = new CachedInputStream(parent.getInputStream());
140 java.io.File f = LocalStorage.Cache.getCacheFileForKey(key);
141 if (f.exists()) return new FileInputStream(f);
142 cis = new CachedInputStream(parent.getInputStream(), f);
144 return cis.getInputStream();