1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/Zips.java Fri Feb 01 18:42:07 2013 +0100
1.3 @@ -0,0 +1,97 @@
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.vm4brwsr;
1.22 +
1.23 +import java.io.IOException;
1.24 +import java.net.URL;
1.25 +import java.util.zip.ZipEntry;
1.26 +import java.util.zip.ZipInputStream;
1.27 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
1.28 +
1.29 +/** Conversion from classpath to load function.
1.30 + *
1.31 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.32 + */
1.33 +final class Zips {
1.34 + private Zips() {
1.35 + }
1.36 +
1.37 + public static void init() {
1.38 + }
1.39 +
1.40 + public static byte[] loadFromCp(Object[] classpath, String res) {
1.41 + for (int i = 0; i < classpath.length; i++) {
1.42 + Object c = classpath[i];
1.43 + if (c instanceof String) {
1.44 + try {
1.45 + c = classpath[i] = toZip((String)c);
1.46 + } catch (IOException ex) {
1.47 + classpath[i] = ex;
1.48 + }
1.49 + }
1.50 + if (c instanceof Zips) {
1.51 + Object checkRes = ((Zips)c).findRes(res);
1.52 + if (checkRes instanceof byte[]) {
1.53 + return (byte[])checkRes;
1.54 + }
1.55 + }
1.56 + }
1.57 + return null;
1.58 + }
1.59 +
1.60 + @JavaScriptBody(args = { "res" }, body = "var r = this[res]; return r ? r : null;")
1.61 + private native byte[] findRes(String res);
1.62 +
1.63 + @JavaScriptBody(args = { "res", "arr" }, body = "this[res] = arr;")
1.64 + private native void putRes(String res, byte[] arr);
1.65 +
1.66 + private static Zips toZip(String path) throws IOException {
1.67 + URL u = new URL(path);
1.68 + ZipInputStream zip = new ZipInputStream(u.openStream());
1.69 + Zips z = new Zips();
1.70 + for (;;) {
1.71 + ZipEntry entry = zip.getNextEntry();
1.72 + if (entry == null) {
1.73 + break;
1.74 + }
1.75 + byte[] arr = new byte[4096];
1.76 + int offset = 0;
1.77 + for (;;) {
1.78 + int len = zip.read(arr, offset, arr.length - offset);
1.79 + if (len == -1) {
1.80 + break;
1.81 + }
1.82 + offset += len;
1.83 + if (offset == arr.length) {
1.84 + enlargeArray(arr, arr.length + 4096);
1.85 + }
1.86 + }
1.87 + sliceArray(arr, offset);
1.88 + z.putRes(entry.getName(), arr);
1.89 + }
1.90 + return z;
1.91 + }
1.92 +
1.93 + @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(0);")
1.94 + private static native void enlargeArray(byte[] arr, int len);
1.95 +
1.96 + @JavaScriptBody(args = { "arr", "len" }, body = "arr.splice(len, arr.length - len);")
1.97 + private static native void sliceArray(byte[] arr, int len);
1.98 +
1.99 +
1.100 +}