initial checkin
[org.ibex.nanogoat.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.ibex.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 String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); }
98         public InputStream getInputStream() throws IOException { return Platform.getBuiltinInputStream(); }
99     }
100
101     /** shadow resource which replaces the graft */
102     public static class ProgressWatcher extends Stream {
103         final Stream watchee;
104         JS callback;
105         ProgressWatcher(Stream watchee, JS callback) { this.watchee = watchee; this.callback = callback; }
106         public String getCacheKey() throws NotCacheableException { return watchee.getCacheKey(); }
107         public InputStream getInputStream() throws IOException {
108             final InputStream is = watchee.getInputStream();
109             return new FilterInputStream(is) {
110                     int bytesDownloaded = 0;
111                     public int read() throws IOException {
112                         int ret = super.read();
113                         if (ret != -1) bytesDownloaded++;
114                         return ret;
115                     }
116                     public int read(byte[] b, int off, int len) throws IOException {
117                         int ret = super.read(b, off, len);
118                         if (ret != 1) bytesDownloaded += ret;
119                         Scheduler.add(new Scheduler.Task() { public void perform() throws IOException, JSExn {
120                             callback.call(N(bytesDownloaded),
121                                           N(is instanceof KnownLength ? ((KnownLength)is).getLength() : 0), null, null, 2);
122                         } });
123                         return ret;
124                     }
125                 };
126         }
127     }
128
129     /** subclass from this if you want a CachedInputStream for each path */
130     public static class CachedStream extends Stream {
131         private Stream parent;
132         private boolean disk = false;
133         private String key;
134         public String getCacheKey() throws NotCacheableException { return key; }
135         CachedInputStream cis = null;
136         public CachedStream(Stream p, String s, boolean d) throws NotCacheableException {
137             this.parent = p; this.disk = d; this.key = p.getCacheKey();
138         }
139         public InputStream getInputStream() throws IOException {
140             if (cis != null) return cis.getInputStream();
141             if (!disk) {
142                 cis = new CachedInputStream(parent.getInputStream());
143             } else {
144                 java.io.File f = LocalStorage.Cache.getCacheFileForKey(key);
145                 if (f.exists()) return new FileInputStream(f);
146                 cis = new CachedInputStream(parent.getInputStream(), f);
147             }
148             return cis.getInputStream();
149         }
150     }
151 }