// Copyright 2000-2005 the Contributors, as shown in the revision logs. // Licensed under the Apache Public Source License 2.0 ("the License"). // You may not use this file except in compliance with the License. package org.ibex.js; import java.io.*; import java.util.zip.*; import org.ibex.js.*; import org.ibex.util.*; import org.ibex.net.*; /** * Essentiall an InputStream "factory". You can repeatedly ask a * Fountain 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 Fountain extends JS.Obj implements JS.Cloneable { // Public Interface ////////////////////////////////////////////////////////////////////////////// public static InputStream getInputStream(Object js) throws IOException { return ((Fountain)((JS)js).unclone()).getInputStream();} public static class NotCacheableException extends Exception { } // streams are "sealed" by default to prevent accidental object leakage private Cache getCache = new Cache(100, true); protected JS _get(JS key) throws JSExn { return null; } public final JS get(JS key) throws JSExn { JS ret = (JS)getCache.get(key); if (ret == null) getCache.put(key, ret = _get(key)); return ret; } // Private Interface ////////////////////////////////////////////////////////////////////////////// public abstract InputStream getInputStream() throws IOException; protected String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); } /** HTTP or HTTPS resource */ public static class HTTP extends Fountain { private String url; //public String toString() { return "Fountain.HTTP:" + url; } public HTTP(String url) { while (url.endsWith("/")) url = url.substring(0, url.length() - 1); this.url = url; } public JS _get(JS key) throws JSExn { return new HTTP(url + "/" + JSU.toString(key)); } public String getCacheKey(Vec path) throws NotCacheableException { return url; } public InputStream getInputStream() throws IOException { return new org.ibex.net.HTTP(url).GET(null, null); } } /** byte arrays */ public static class ByteArray extends Fountain { private byte[] bytes; private String cacheKey; public 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 Fountain { private String path; public 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 JS _get(JS key) throws JSExn { return new File(path + java.io.File.separatorChar + JSU.toString(key)); } } /** "unwrap" a Zip archive */ public static class Zip extends Fountain { private Fountain parent; private String path; public Zip(Fountain parent) { this(parent, null); } public Zip(Fountain 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 JS _get(JS key) throws JSExn { return new Zip(parent, path==null?JSU.toString(key):path+'/'+JSU.toString(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 Fountain { private Fountain parent; private String path; public Cab(Fountain parent) { this(parent, null); } public Cab(Fountain parent, String path) { this.parent = parent; this.path = path; } public String getCacheKey() throws NotCacheableException { return parent.getCacheKey() + "!cab:"; } public JS _get(JS 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); } } */ public static class FromInputStream extends Fountain { private final InputStream is; public FromInputStream(InputStream is) { this.is = is; } public String getCacheKey() throws NotCacheableException { throw new NotCacheableException(); } public InputStream getInputStream() throws IOException { return is; } } /** shadow resource which replaces the graft */ public static class ProgressWatcher extends Fountain { private final JS[] callargs = new JS[2]; final Fountain watchee; Callable callback; public ProgressWatcher(Fountain watchee, Callable 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; callargs[0] = JSU.N(bytesDownloaded); callargs[1] = JSU.N(is instanceof KnownLength.KnownLengthInputStream ? ((KnownLength.KnownLengthInputStream)is).getLength() : 0); try { callback.run(callargs); } catch (Exception e) { Log.warn(ProgressWatcher.class, e); } return ret; } }; } } /** subclass from this if you want a CachedInputStream for each path */ /* public static class CachedFountain extends Fountain { private Fountain parent; private boolean disk = false; private String key; public String getCacheKey() throws NotCacheableException { return key; } CachedInputStream cis = null; public CachedFountain(Fountain 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 = org.ibex.plat.Platform.LocalStorage.Cache.getCacheFileForKey(key); if (f.exists()) return new FileInputStream(f); cis = new CachedInputStream(parent.getInputStream(), f); } return cis.getInputStream(); } } */ }