jaroslav@644: /** jaroslav@644: * Back 2 Browser Bytecode Translator jaroslav@644: * Copyright (C) 2012 Jaroslav Tulach jaroslav@644: * jaroslav@644: * This program is free software: you can redistribute it and/or modify jaroslav@644: * it under the terms of the GNU General Public License as published by jaroslav@644: * the Free Software Foundation, version 2 of the License. jaroslav@644: * jaroslav@644: * This program is distributed in the hope that it will be useful, jaroslav@644: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@644: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@644: * GNU General Public License for more details. jaroslav@644: * jaroslav@644: * You should have received a copy of the GNU General Public License jaroslav@644: * along with this program. Look for COPYING file in the top folder. jaroslav@644: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@644: */ jaroslav@644: package org.apidesign.vm4brwsr; jaroslav@644: jaroslav@644: import java.io.IOException; jaroslav@644: import java.net.URL; jaroslav@644: import java.util.zip.ZipEntry; jaroslav@644: import java.util.zip.ZipInputStream; jaroslav@644: import org.apidesign.bck2brwsr.core.JavaScriptBody; jaroslav@644: jaroslav@644: /** Conversion from classpath to load function. jaroslav@644: * jaroslav@644: * @author Jaroslav Tulach jaroslav@644: */ jaroslav@644: final class Zips { jaroslav@644: private Zips() { jaroslav@644: } jaroslav@644: jaroslav@644: public static void init() { jaroslav@644: } jaroslav@644: jaroslav@644: public static byte[] loadFromCp(Object[] classpath, String res) { jaroslav@644: for (int i = 0; i < classpath.length; i++) { jaroslav@644: Object c = classpath[i]; jaroslav@644: if (c instanceof String) { jaroslav@644: try { jaroslav@644: c = classpath[i] = toZip((String)c); jaroslav@644: } catch (IOException ex) { jaroslav@644: classpath[i] = ex; jaroslav@644: } jaroslav@644: } jaroslav@644: if (c instanceof Zips) { jaroslav@644: Object checkRes = ((Zips)c).findRes(res); jaroslav@644: if (checkRes instanceof byte[]) { jaroslav@644: return (byte[])checkRes; jaroslav@644: } jaroslav@644: } jaroslav@644: } jaroslav@644: return null; jaroslav@644: } jaroslav@644: jaroslav@644: @JavaScriptBody(args = { "res" }, body = "var r = this[res]; return r ? r : null;") jaroslav@644: private native byte[] findRes(String res); jaroslav@644: jaroslav@644: @JavaScriptBody(args = { "res", "arr" }, body = "this[res] = arr;") jaroslav@644: private native void putRes(String res, byte[] arr); jaroslav@644: jaroslav@644: private static Zips toZip(String path) throws IOException { jaroslav@644: URL u = new URL(path); jaroslav@644: ZipInputStream zip = new ZipInputStream(u.openStream()); jaroslav@644: Zips z = new Zips(); jaroslav@644: for (;;) { jaroslav@644: ZipEntry entry = zip.getNextEntry(); jaroslav@644: if (entry == null) { jaroslav@644: break; jaroslav@644: } jaroslav@644: byte[] arr = new byte[4096]; jaroslav@644: int offset = 0; jaroslav@644: for (;;) { jaroslav@644: int len = zip.read(arr, offset, arr.length - offset); jaroslav@644: if (len == -1) { jaroslav@644: break; jaroslav@644: } jaroslav@644: offset += len; jaroslav@644: if (offset == arr.length) { jaroslav@644: enlargeArray(arr, arr.length + 4096); jaroslav@644: } jaroslav@644: } jaroslav@644: sliceArray(arr, offset); jaroslav@644: z.putRes(entry.getName(), arr); jaroslav@644: } jaroslav@644: return z; jaroslav@644: } jaroslav@644: jaroslav@644: @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(0);") jaroslav@644: private static native void enlargeArray(byte[] arr, int len); jaroslav@644: jaroslav@644: @JavaScriptBody(args = { "arr", "len" }, body = "arr.splice(len, arr.length - len);") jaroslav@644: private static native void sliceArray(byte[] arr, int len); jaroslav@644: jaroslav@644: jaroslav@644: }