Splitting into two classes: one that deals with classpath, one that deals with ZIP content
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.vm4brwsr;
20 import java.io.IOException;
21 import java.io.InputStream;
23 import org.apidesign.bck2brwsr.core.JavaScriptBody;
24 import org.apidesign.bck2brwsr.emul.zip.FastJar;
26 /** Conversion from classpath to load function.
28 * @author Jaroslav Tulach <jtulach@netbeans.org>
30 final class ZipHandler {
31 private final FastJar fj;
33 private ZipHandler(String path, byte[] zipData) throws IOException {
35 fj = new FastJar(zipData);
36 for (FastJar.Entry e : fj.list()) {
39 log("Iterating thru " + path + " took " + (timeNow() - bef) + "ms");
42 public static void init() {
44 @JavaScriptBody(args = { "arr" }, body = "return arr.length;")
45 private static native int length(Object arr);
46 @JavaScriptBody(args = { "arr", "index", "value" }, body = "arr[index] = value; return value;")
47 private static native Object set(Object arr, int index, Object value);
49 @JavaScriptBody(args = { "msg" }, body = "if (typeof console !== 'undefined') console.log(msg.toString());")
50 private static native void log(String msg);
52 byte[] findRes(String res) throws IOException {
53 Object arr = findResImpl(res);
54 if (arr instanceof FastJar.Entry) {
56 InputStream zip = fj.getInputStream((FastJar.Entry)arr);
57 arr = readFully(new byte[512], zip);
59 log("Reading " + res + " took " + (timeNow() - bef) + "ms");
64 @JavaScriptBody(args = { "res" }, body = "var r = this[res]; return r ? r : null;")
65 private native Object findResImpl(String res);
67 @JavaScriptBody(args = { "res", "arr" }, body = "this[res] = arr;")
68 private native void putRes(String res, Object arr);
70 static ZipHandler toZip(String path) throws IOException {
71 URL u = new URL(path);
72 byte[] zipData = (byte[]) u.getContent(new Class[] { byte[].class });
73 return new ZipHandler(path, zipData);
76 @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(0);")
77 private static native void enlargeBytes(byte[] arr, int len);
79 @JavaScriptBody(args = { "arr", "len" }, body = "arr.splice(len, arr.length - len);")
80 private static native void sliceArray(byte[] arr, int len);
82 private static Object readFully(byte[] arr, InputStream zip) throws IOException {
85 int len = zip.read(arr, offset, arr.length - offset);
90 if (offset == arr.length) {
91 enlargeBytes(arr, arr.length + 4096);
94 sliceArray(arr, offset);
98 private static long timeNow() {
103 return org.apidesign.bck2brwsr.emul.lang.System.currentTimeMillis();
105 @JavaScriptBody(args = {}, body =
106 "if (typeof window.performance === 'undefined') return -1;\n"
107 + "if (typeof window.performance.now === 'undefined') return -1;\n"
108 + "return window.performance.now();"
110 private static native double m();