vm/src/main/java/org/apidesign/vm4brwsr/Zips.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 10 Feb 2013 12:14:40 +0100
branchemul
changeset 706 a48961ff3e6b
parent 702 fa42b3d8cbbc
child 715 7572022945a0
permissions -rw-r--r--
Using FastJar to read the content table of JAR files
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     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.
     8  *
     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.
    13  *
    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.
    17  */
    18 package org.apidesign.vm4brwsr;
    19 
    20 import java.io.ByteArrayInputStream;
    21 import java.io.IOException;
    22 import java.io.InputStream;
    23 import java.net.URL;
    24 import org.apidesign.bck2brwsr.core.JavaScriptBody;
    25 import org.apidesign.bck2brwsr.emul.zip.FastJar;
    26 
    27 /** Conversion from classpath to load function.
    28  *
    29  * @author Jaroslav Tulach <jtulach@netbeans.org>
    30  */
    31 final class Zips {
    32     private final FastJar fj;
    33 
    34     private Zips(String path, byte[] zipData) throws IOException {
    35         long bef = currentTimeMillis();
    36         fj = new FastJar(zipData);
    37         for (FastJar.Entry e : fj.list()) {
    38             putRes(e.name, e);
    39         }
    40         log("Iterating thru " + path + " took " + (currentTimeMillis() - bef) + "ms");
    41     }
    42     
    43     public static void init() {
    44     }
    45     
    46     public static byte[] loadFromCp(Object[] classpath, String res) throws Exception {
    47         for (int i = 0; i < classpath.length; i++) {
    48             Object c = classpath[i];
    49             if (c instanceof String) {
    50                 try {
    51                     String url = (String)c;
    52                     final Zips z = toZip(url);
    53                     c = classpath[i] = z;
    54                     final byte[] man = z.findRes("META-INF/MANIFEST.MF");
    55                     if (man != null) {
    56                         String mainClass = processClassPathAttr(man, url, classpath);
    57                         if (mainClass != null) {
    58                             Class.forName(mainClass);
    59                         }
    60                     }
    61                 } catch (Exception ex) {
    62                     classpath[i] = ex;
    63                     throw ex;
    64                 }
    65             }
    66             if (res != null && c instanceof Zips) {
    67                 Object checkRes = ((Zips)c).findRes(res);
    68                 if (checkRes instanceof byte[]) {
    69                     return (byte[])checkRes;
    70                 }
    71             }
    72         }
    73         return null;
    74     }
    75     
    76     @JavaScriptBody(args = { "msg" }, body = "console.log(msg.toString());")
    77     private static native void log(String msg);
    78 
    79     private byte[] findRes(String res) throws IOException {
    80         Object arr = findResImpl(res);
    81         if (arr instanceof FastJar.Entry) {
    82             long bef = currentTimeMillis();
    83             InputStream zip = fj.getInputStream((FastJar.Entry)arr);
    84             arr = readFully(new byte[512], zip);
    85             putRes(res, arr);
    86             log("Reading " + res + " took " + (currentTimeMillis() - bef) + "ms");
    87         }
    88         return (byte[]) arr;
    89     }
    90 
    91     @JavaScriptBody(args = { "res" }, body = "var r = this[res]; return r ? r : null;")
    92     private native Object findResImpl(String res);
    93 
    94     @JavaScriptBody(args = { "res", "arr" }, body = "this[res] = arr;")
    95     private native void putRes(String res, Object arr);
    96     
    97     private static Zips toZip(String path) throws IOException {
    98         URL u = new URL(path);
    99         byte[] zipData = (byte[]) u.getContent(new Class[] { byte[].class });
   100         return new Zips(path, zipData);
   101     }
   102 
   103     private static String processClassPathAttr(final byte[] man, String url, Object[] classpath) throws IOException {
   104         try (ParseMan is = new ParseMan(new ByteArrayInputStream(man))) {
   105             String cp = is.toString();
   106             if (cp != null) {
   107                 cp = cp.trim();
   108                 for (int p = 0; p < cp.length();) {
   109                     int n = cp.indexOf(' ', p);
   110                     if (n == -1) {
   111                         n = cp.length();
   112                     }
   113                     String el = cp.substring(p, n);
   114                     URL u = new URL(new URL(url), el);
   115                     classpath = addToArray(classpath, u.toString());
   116                     p = n + 1;
   117                 }
   118             }
   119             return is.getMainClass();
   120         }
   121     }
   122 
   123     private static Object[] addToArray(Object[] arr, String value) {
   124         final int last = arr.length;
   125         Object[] ret = enlargeArray(arr, last + 1);
   126         ret[last] = value;
   127         return ret;
   128     }
   129 
   130     @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(null); return arr;throw('Arr: ' + arr);")
   131     private static native Object[] enlargeArray(Object[] arr, int len);
   132     @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(0);")
   133     private static native void enlargeArray(byte[] arr, int len);
   134 
   135     @JavaScriptBody(args = { "arr", "len" }, body = "arr.splice(len, arr.length - len);")
   136     private static native void sliceArray(byte[] arr, int len);
   137 
   138     private static Object readFully(byte[] arr, InputStream zip) throws IOException {
   139         int offset = 0;
   140         for (;;) {
   141             int len = zip.read(arr, offset, arr.length - offset);
   142             if (len == -1) {
   143                 break;
   144             }
   145             offset += len;
   146             if (offset == arr.length) {
   147                 enlargeArray(arr, arr.length + 4096);
   148             }
   149         }
   150         sliceArray(arr, offset);
   151         return arr;
   152     }
   153 
   154     private static long currentTimeMillis() {
   155         return (long)m();
   156     }
   157     @JavaScriptBody(args = {  }, body = "return window.performance.now();")
   158     private static native double m();
   159     
   160     
   161 }