X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fxwt%2FStream.java;fp=src%2Forg%2Fxwt%2FStream.java;h=0000000000000000000000000000000000000000;hb=3591b88b94a6bb378af3d4abe6eb5233ce583104;hp=3d847e80e6f0434f8fed9ee191a27483024ae90d;hpb=de378041d5ca2aca1a2b5a31ef15ae90a86c977f;p=org.ibex.core.git diff --git a/src/org/xwt/Stream.java b/src/org/xwt/Stream.java deleted file mode 100644 index 3d847e8..0000000 --- a/src/org/xwt/Stream.java +++ /dev/null @@ -1,154 +0,0 @@ -// Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL] -package org.xwt; - -import java.io.*; -import java.util.*; -import java.util.zip.*; -import org.xwt.js.*; -import org.xwt.util.*; -import org.xwt.translators.MSPack; -import org.bouncycastle.util.encoders.Base64; - -/** - * Essentiall an InputStream "factory". You can repeatedly ask a - * Stream for an InputStream, and each InputStream you get back will - * be totally independent of the others (ie separate stream position - * and state) although they draw from the same data source. - */ -public abstract class Stream extends JS.Cloneable { - - // Public Interface ////////////////////////////////////////////////////////////////////////////// - - public static InputStream getInputStream(Object js) throws IOException { return ((Stream)((JS)js).unclone()).getInputStream();} - public static class NotCacheableException extends Exception { } - - // streams are "sealed" by default to prevent accidental object leakage - public void put(Object key, Object val) throws JSExn { } - public Object get(Object key) throws JSExn { return null; } - - // Private Interface ////////////////////////////////////////////////////////////////////////////// - - protected abstract InputStream getInputStream() throws IOException; - protected String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); } - - /** HTTP or HTTPS resource */ - public static class HTTP extends Stream { - private String url; - public String toString() { return "Stream.HTTP:" + url; } - HTTP(String url) { while (url.endsWith("/")) url = url.substring(0, url.length() - 1); this.url = url; } - public Object get(Object key) throws JSExn { return new HTTP(url + "/" + (String)key); } - public String getCacheKey(Vec path) throws NotCacheableException { return url; } - public InputStream getInputStream() throws IOException { return new org.xwt.HTTP(url).GET(); } - } - - /** byte arrays */ - public static class ByteArray extends Stream { - private byte[] bytes; - private String cacheKey; - ByteArray(byte[] bytes, String cacheKey) { this.bytes = bytes; this.cacheKey = cacheKey; } - public String getCacheKey() throws NotCacheableException { - if (cacheKey == null) throw new NotCacheableException(); return cacheKey; } - public InputStream getInputStream() throws IOException { return new ByteArrayInputStream(bytes); } - } - - /** a file */ - public static class File extends Stream { - private String path; - File(String path) { this.path = path; } - public String toString() { return "file:" + path; } - public String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); /* already on disk */ } - public InputStream getInputStream() throws IOException { return new FileInputStream(path); } - public Object get(Object key) throws JSExn { return new File(path + java.io.File.separatorChar + (String)key); } - } - - /** "unwrap" a Zip archive */ - public static class Zip extends Stream { - private Stream parent; - private String path; - Zip(Stream parent) { this(parent, null); } - Zip(Stream parent, String path) { - while(path != null && path.startsWith("/")) path = path.substring(1); - this.parent = parent; - this.path = path; - } - public String getCacheKey() throws NotCacheableException { return parent.getCacheKey() + "!zip:"; } - public Object get(Object key) throws JSExn { return new Zip(parent, path==null?(String)key:path+'/'+(String)key); } - public InputStream getInputStream() throws IOException { - InputStream pis = parent.getInputStream(); - ZipInputStream zis = new ZipInputStream(pis); - ZipEntry ze = zis.getNextEntry(); - while(ze != null && !ze.getName().equals(path)) ze = zis.getNextEntry(); - if (ze == null) throw new IOException("requested file (" + path + ") not found in archive"); - return new KnownLength.KnownLengthInputStream(zis, (int)ze.getSize()); - } - } - - /** "unwrap" a Cab archive */ - public static class Cab extends Stream { - private Stream parent; - private String path; - Cab(Stream parent) { this(parent, null); } - Cab(Stream parent, String path) { this.parent = parent; this.path = path; } - public String getCacheKey() throws NotCacheableException { return parent.getCacheKey() + "!cab:"; } - public Object get(Object key) throws JSExn { return new Cab(parent, path==null?(String)key:path+'/'+(String)key); } - public InputStream getInputStream() throws IOException { return new MSPack(parent.getInputStream()).getInputStream(path); } - } - - /** the Builtin resource */ - public static class Builtin extends Stream { - public Builtin() { }; - public String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); } - public InputStream getInputStream() throws IOException { return Platform.getBuiltinInputStream(); } - } - - /** shadow resource which replaces the graft */ - public static class ProgressWatcher extends Stream { - final Stream watchee; - JS callback; - ProgressWatcher(Stream watchee, JS callback) { this.watchee = watchee; this.callback = callback; } - public String getCacheKey() throws NotCacheableException { return watchee.getCacheKey(); } - public InputStream getInputStream() throws IOException { - final InputStream is = watchee.getInputStream(); - return new FilterInputStream(is) { - int bytesDownloaded = 0; - public int read() throws IOException { - int ret = super.read(); - if (ret != -1) bytesDownloaded++; - return ret; - } - public int read(byte[] b, int off, int len) throws IOException { - int ret = super.read(b, off, len); - if (ret != 1) bytesDownloaded += ret; - Scheduler.add(new Scheduler.Task() { public void perform() throws Exception { - callback.call(N(bytesDownloaded), - N(is instanceof KnownLength ? ((KnownLength)is).getLength() : 0), null, null, 2); - } }); - return ret; - } - }; - } - } - - /** subclass from this if you want a CachedInputStream for each path */ - public static class CachedStream extends Stream { - private Stream parent; - private boolean disk = false; - private String key; - public String getCacheKey() throws NotCacheableException { return key; } - CachedInputStream cis = null; - public CachedStream(Stream p, String s, boolean d) throws NotCacheableException { - this.parent = p; this.disk = d; this.key = p.getCacheKey(); - } - public InputStream getInputStream() throws IOException { - if (cis != null) return cis.getInputStream(); - if (!disk) { - cis = new CachedInputStream(parent.getInputStream()); - } else { - java.io.File f = LocalStorage.Cache.getCacheFileForKey(key); - if (f.exists()) return new FileInputStream(f); - cis = new CachedInputStream(parent.getInputStream(), f); - } - return cis.getInputStream(); - } - } -}