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