1.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/Zips.java Thu Feb 07 13:41:56 2013 +0100
1.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/Zips.java Sun Feb 10 12:14:40 2013 +0100
1.3 @@ -19,17 +19,25 @@
1.4
1.5 import java.io.ByteArrayInputStream;
1.6 import java.io.IOException;
1.7 +import java.io.InputStream;
1.8 import java.net.URL;
1.9 -import java.util.zip.ZipEntry;
1.10 -import java.util.zip.ZipInputStream;
1.11 import org.apidesign.bck2brwsr.core.JavaScriptBody;
1.12 +import org.apidesign.bck2brwsr.emul.zip.FastJar;
1.13
1.14 /** Conversion from classpath to load function.
1.15 *
1.16 * @author Jaroslav Tulach <jtulach@netbeans.org>
1.17 */
1.18 final class Zips {
1.19 - private Zips() {
1.20 + private final FastJar fj;
1.21 +
1.22 + private Zips(String path, byte[] zipData) throws IOException {
1.23 + long bef = currentTimeMillis();
1.24 + fj = new FastJar(zipData);
1.25 + for (FastJar.Entry e : fj.list()) {
1.26 + putRes(e.name, e);
1.27 + }
1.28 + log("Iterating thru " + path + " took " + (currentTimeMillis() - bef) + "ms");
1.29 }
1.30
1.31 public static void init() {
1.32 @@ -64,38 +72,32 @@
1.33 }
1.34 return null;
1.35 }
1.36 +
1.37 + @JavaScriptBody(args = { "msg" }, body = "console.log(msg.toString());")
1.38 + private static native void log(String msg);
1.39 +
1.40 + private byte[] findRes(String res) throws IOException {
1.41 + Object arr = findResImpl(res);
1.42 + if (arr instanceof FastJar.Entry) {
1.43 + long bef = currentTimeMillis();
1.44 + InputStream zip = fj.getInputStream((FastJar.Entry)arr);
1.45 + arr = readFully(new byte[512], zip);
1.46 + putRes(res, arr);
1.47 + log("Reading " + res + " took " + (currentTimeMillis() - bef) + "ms");
1.48 + }
1.49 + return (byte[]) arr;
1.50 + }
1.51
1.52 @JavaScriptBody(args = { "res" }, body = "var r = this[res]; return r ? r : null;")
1.53 - private native byte[] findRes(String res);
1.54 + private native Object findResImpl(String res);
1.55
1.56 @JavaScriptBody(args = { "res", "arr" }, body = "this[res] = arr;")
1.57 - private native void putRes(String res, byte[] arr);
1.58 + private native void putRes(String res, Object arr);
1.59
1.60 private static Zips toZip(String path) throws IOException {
1.61 URL u = new URL(path);
1.62 - ZipInputStream zip = new ZipInputStream(u.openStream());
1.63 - Zips z = new Zips();
1.64 - for (;;) {
1.65 - ZipEntry entry = zip.getNextEntry();
1.66 - if (entry == null) {
1.67 - break;
1.68 - }
1.69 - byte[] arr = new byte[4096];
1.70 - int offset = 0;
1.71 - for (;;) {
1.72 - int len = zip.read(arr, offset, arr.length - offset);
1.73 - if (len == -1) {
1.74 - break;
1.75 - }
1.76 - offset += len;
1.77 - if (offset == arr.length) {
1.78 - enlargeArray(arr, arr.length + 4096);
1.79 - }
1.80 - }
1.81 - sliceArray(arr, offset);
1.82 - z.putRes(entry.getName(), arr);
1.83 - }
1.84 - return z;
1.85 + byte[] zipData = (byte[]) u.getContent(new Class[] { byte[].class });
1.86 + return new Zips(path, zipData);
1.87 }
1.88
1.89 private static String processClassPathAttr(final byte[] man, String url, Object[] classpath) throws IOException {
1.90 @@ -132,6 +134,28 @@
1.91
1.92 @JavaScriptBody(args = { "arr", "len" }, body = "arr.splice(len, arr.length - len);")
1.93 private static native void sliceArray(byte[] arr, int len);
1.94 +
1.95 + private static Object readFully(byte[] arr, InputStream zip) throws IOException {
1.96 + int offset = 0;
1.97 + for (;;) {
1.98 + int len = zip.read(arr, offset, arr.length - offset);
1.99 + if (len == -1) {
1.100 + break;
1.101 + }
1.102 + offset += len;
1.103 + if (offset == arr.length) {
1.104 + enlargeArray(arr, arr.length + 4096);
1.105 + }
1.106 + }
1.107 + sliceArray(arr, offset);
1.108 + return arr;
1.109 + }
1.110 +
1.111 + private static long currentTimeMillis() {
1.112 + return (long)m();
1.113 + }
1.114 + @JavaScriptBody(args = { }, body = "return window.performance.now();")
1.115 + private static native double m();
1.116
1.117
1.118 }