diff -r fa42b3d8cbbc -r a48961ff3e6b vm/src/main/java/org/apidesign/vm4brwsr/Zips.java --- a/vm/src/main/java/org/apidesign/vm4brwsr/Zips.java Thu Feb 07 13:41:56 2013 +0100 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/Zips.java Sun Feb 10 12:14:40 2013 +0100 @@ -19,17 +19,25 @@ import java.io.ByteArrayInputStream; import java.io.IOException; +import java.io.InputStream; import java.net.URL; -import java.util.zip.ZipEntry; -import java.util.zip.ZipInputStream; import org.apidesign.bck2brwsr.core.JavaScriptBody; +import org.apidesign.bck2brwsr.emul.zip.FastJar; /** Conversion from classpath to load function. * * @author Jaroslav Tulach */ final class Zips { - private Zips() { + private final FastJar fj; + + private Zips(String path, byte[] zipData) throws IOException { + long bef = currentTimeMillis(); + fj = new FastJar(zipData); + for (FastJar.Entry e : fj.list()) { + putRes(e.name, e); + } + log("Iterating thru " + path + " took " + (currentTimeMillis() - bef) + "ms"); } public static void init() { @@ -64,38 +72,32 @@ } return null; } + + @JavaScriptBody(args = { "msg" }, body = "console.log(msg.toString());") + private static native void log(String msg); + + private byte[] findRes(String res) throws IOException { + Object arr = findResImpl(res); + if (arr instanceof FastJar.Entry) { + long bef = currentTimeMillis(); + InputStream zip = fj.getInputStream((FastJar.Entry)arr); + arr = readFully(new byte[512], zip); + putRes(res, arr); + log("Reading " + res + " took " + (currentTimeMillis() - bef) + "ms"); + } + return (byte[]) arr; + } @JavaScriptBody(args = { "res" }, body = "var r = this[res]; return r ? r : null;") - private native byte[] findRes(String res); + private native Object findResImpl(String res); @JavaScriptBody(args = { "res", "arr" }, body = "this[res] = arr;") - private native void putRes(String res, byte[] arr); + private native void putRes(String res, Object arr); private static Zips toZip(String path) throws IOException { URL u = new URL(path); - ZipInputStream zip = new ZipInputStream(u.openStream()); - Zips z = new Zips(); - for (;;) { - ZipEntry entry = zip.getNextEntry(); - if (entry == null) { - break; - } - byte[] arr = new byte[4096]; - int offset = 0; - for (;;) { - int len = zip.read(arr, offset, arr.length - offset); - if (len == -1) { - break; - } - offset += len; - if (offset == arr.length) { - enlargeArray(arr, arr.length + 4096); - } - } - sliceArray(arr, offset); - z.putRes(entry.getName(), arr); - } - return z; + byte[] zipData = (byte[]) u.getContent(new Class[] { byte[].class }); + return new Zips(path, zipData); } private static String processClassPathAttr(final byte[] man, String url, Object[] classpath) throws IOException { @@ -132,6 +134,28 @@ @JavaScriptBody(args = { "arr", "len" }, body = "arr.splice(len, arr.length - len);") private static native void sliceArray(byte[] arr, int len); + + private static Object readFully(byte[] arr, InputStream zip) throws IOException { + int offset = 0; + for (;;) { + int len = zip.read(arr, offset, arr.length - offset); + if (len == -1) { + break; + } + offset += len; + if (offset == arr.length) { + enlargeArray(arr, arr.length + 4096); + } + } + sliceArray(arr, offset); + return arr; + } + + private static long currentTimeMillis() { + return (long)m(); + } + @JavaScriptBody(args = { }, body = "return window.performance.now();") + private static native double m(); }