2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.bck2brwsr.vmzip;
20 import java.io.ByteArrayInputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import org.apidesign.bck2brwsr.core.Exported;
24 import org.apidesign.bck2brwsr.core.JavaScriptBody;
25 import org.apidesign.bck2brwsr.emul.zip.FastJar;
26 import org.apidesign.vm4brwsr.Bck2Brwsr;
28 /** Conversion from classpath to load function.
30 * @author Jaroslav Tulach <jtulach@netbeans.org>
33 public final class ZipResources implements Bck2Brwsr.Resources {
34 private final FastJar fj;
37 public ZipResources(byte[] zipData) throws IOException {
38 // long bef = timeNow();
39 fj = new FastJar(zipData);
40 for (FastJar.Entry e : fj.list()) {
43 // log("Iterating thru " + path + " took " + (timeNow() - bef) + "ms");
46 @JavaScriptBody(args = { "arr" }, body = "return arr.length;")
47 private static native int length(Object arr);
48 @JavaScriptBody(args = { "arr", "index", "value" }, body = "arr[index] = value; return value;")
49 private static native Object set(Object arr, int index, Object value);
51 @JavaScriptBody(args = { "msg" }, body = "if (typeof console !== 'undefined') console.log(msg.toString());")
52 private static native void log(String msg);
54 private byte[] findRes(String res) throws IOException {
55 Object arr = findResImpl(res);
56 if (arr instanceof FastJar.Entry) {
58 InputStream zip = fj.getInputStream((FastJar.Entry)arr);
59 arr = readFully(new byte[512], zip);
61 log("Reading " + res + " took " + (timeNow() - bef) + "ms");
67 public InputStream get(String resource) throws IOException {
68 byte[] arr = findRes(resource);
69 return arr == null ? null : new ByteArrayInputStream(arr);
72 @JavaScriptBody(args = { "res" }, body = "var r = this[res]; return r ? r : null;")
73 private native Object findResImpl(String res);
75 @JavaScriptBody(args = { "res", "arr" }, body = "this[res] = arr;")
76 private native void putRes(String res, Object arr);
78 @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(0);")
79 private static native void enlargeBytes(byte[] arr, int len);
81 @JavaScriptBody(args = { "arr", "len" }, body = "arr.splice(len, arr.length - len);")
82 private static native void sliceArray(byte[] arr, int len);
84 private static Object readFully(byte[] arr, InputStream zip) throws IOException {
87 int len = zip.read(arr, offset, arr.length - offset);
92 if (offset == arr.length) {
93 enlargeBytes(arr, arr.length + 4096);
96 sliceArray(arr, offset);
100 private static long timeNow() {
105 return org.apidesign.bck2brwsr.emul.lang.System.currentTimeMillis();
107 @JavaScriptBody(args = {}, body =
108 "if (typeof window.performance === 'undefined') return -1;\n"
109 + "if (typeof window.performance.now === 'undefined') return -1;\n"
110 + "return window.performance.now();"
112 private static native double m();