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