1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/emul/zip/src/main/java/org/apidesign/bck2brwsr/vmzip/ZipResources.java Fri May 09 08:37:20 2014 +0200
1.3 @@ -0,0 +1,115 @@
1.4 +/**
1.5 + * Back 2 Browser Bytecode Translator
1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, version 2 of the License.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License
1.18 + * along with this program. Look for COPYING file in the top folder.
1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
1.20 + */
1.21 +package org.apidesign.bck2brwsr.vmzip;
1.22 +
1.23 +import java.io.ByteArrayInputStream;
1.24 +import java.io.IOException;
1.25 +import java.io.InputStream;
1.26 +import org.apidesign.bck2brwsr.core.Exported;
1.27 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
1.28 +import org.apidesign.bck2brwsr.emul.zip.FastJar;
1.29 +import org.apidesign.vm4brwsr.Bck2Brwsr;
1.30 +
1.31 +/** Conversion from classpath to load function.
1.32 + *
1.33 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.34 + */
1.35 +@Exported
1.36 +public final class ZipResources implements Bck2Brwsr.Resources {
1.37 + private final FastJar fj;
1.38 +
1.39 + @Exported
1.40 + public ZipResources(byte[] zipData) throws IOException {
1.41 +// long bef = timeNow();
1.42 + fj = new FastJar(zipData);
1.43 + for (FastJar.Entry e : fj.list()) {
1.44 + putRes(e.name, e);
1.45 + }
1.46 +// log("Iterating thru " + path + " took " + (timeNow() - bef) + "ms");
1.47 + }
1.48 +
1.49 + @JavaScriptBody(args = { "arr" }, body = "return arr.length;")
1.50 + private static native int length(Object arr);
1.51 + @JavaScriptBody(args = { "arr", "index", "value" }, body = "arr[index] = value; return value;")
1.52 + private static native Object set(Object arr, int index, Object value);
1.53 +
1.54 + @JavaScriptBody(args = { "msg" }, body = "if (typeof console !== 'undefined') console.log(msg.toString());")
1.55 + private static native void log(String msg);
1.56 +
1.57 + private byte[] findRes(String res) throws IOException {
1.58 + Object arr = findResImpl(res);
1.59 + if (arr instanceof FastJar.Entry) {
1.60 + long bef = timeNow();
1.61 + InputStream zip = fj.getInputStream((FastJar.Entry)arr);
1.62 + arr = readFully(new byte[512], zip);
1.63 + putRes(res, arr);
1.64 + log("Reading " + res + " took " + (timeNow() - bef) + "ms");
1.65 + }
1.66 + return (byte[]) arr;
1.67 + }
1.68 +
1.69 + @Override
1.70 + public InputStream get(String resource) throws IOException {
1.71 + byte[] arr = findRes(resource);
1.72 + return arr == null ? null : new ByteArrayInputStream(arr);
1.73 + }
1.74 +
1.75 + @JavaScriptBody(args = { "res" }, body = "var r = this[res]; return r ? r : null;")
1.76 + private native Object findResImpl(String res);
1.77 +
1.78 + @JavaScriptBody(args = { "res", "arr" }, body = "this[res] = arr;")
1.79 + private native void putRes(String res, Object arr);
1.80 +
1.81 + @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(0);")
1.82 + private static native void enlargeBytes(byte[] arr, int len);
1.83 +
1.84 + @JavaScriptBody(args = { "arr", "len" }, body = "arr.splice(len, arr.length - len);")
1.85 + private static native void sliceArray(byte[] arr, int len);
1.86 +
1.87 + private static Object readFully(byte[] arr, InputStream zip) throws IOException {
1.88 + int offset = 0;
1.89 + for (;;) {
1.90 + int len = zip.read(arr, offset, arr.length - offset);
1.91 + if (len == -1) {
1.92 + break;
1.93 + }
1.94 + offset += len;
1.95 + if (offset == arr.length) {
1.96 + enlargeBytes(arr, arr.length + 4096);
1.97 + }
1.98 + }
1.99 + sliceArray(arr, offset);
1.100 + return arr;
1.101 + }
1.102 +
1.103 + private static long timeNow() {
1.104 + double time = m();
1.105 + if (time >= 0) {
1.106 + return (long)time;
1.107 + }
1.108 + return org.apidesign.bck2brwsr.emul.lang.System.currentTimeMillis();
1.109 + }
1.110 + @JavaScriptBody(args = {}, body =
1.111 + "if (typeof window.performance === 'undefined') return -1;\n"
1.112 + + "if (typeof window.performance.now === 'undefined') return -1;\n"
1.113 + + "return window.performance.now();"
1.114 + )
1.115 + private static native double m();
1.116 +
1.117 +
1.118 +}