rt/vm/src/main/java/org/apidesign/vm4brwsr/ZipHandler.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 07 May 2014 17:24:29 +0200
branchclosure
changeset 1550 cb9e273dfd51
parent 1387 rt/vm/src/main/java/org/apidesign/vm4brwsr/Zips.java@350f8aee0f60
permissions -rw-r--r--
Splitting into two classes: one that deals with classpath, one that deals with ZIP content
     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.IOException;
    21 import java.io.InputStream;
    22 import java.net.URL;
    23 import org.apidesign.bck2brwsr.core.JavaScriptBody;
    24 import org.apidesign.bck2brwsr.emul.zip.FastJar;
    25 
    26 /** Conversion from classpath to load function.
    27  *
    28  * @author Jaroslav Tulach <jtulach@netbeans.org>
    29  */
    30 final class ZipHandler {
    31     private final FastJar fj;
    32 
    33     private ZipHandler(String path, byte[] zipData) throws IOException {
    34         long bef = timeNow();
    35         fj = new FastJar(zipData);
    36         for (FastJar.Entry e : fj.list()) {
    37             putRes(e.name, e);
    38         }
    39         log("Iterating thru " + path + " took " + (timeNow() - bef) + "ms");
    40     }
    41     
    42     public static void init() {
    43     }
    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);
    48     
    49     @JavaScriptBody(args = { "msg" }, body = "if (typeof console !== 'undefined') console.log(msg.toString());")
    50     private static native void log(String msg);
    51 
    52     byte[] findRes(String res) throws IOException {
    53         Object arr = findResImpl(res);
    54         if (arr instanceof FastJar.Entry) {
    55             long bef = timeNow();
    56             InputStream zip = fj.getInputStream((FastJar.Entry)arr);
    57             arr = readFully(new byte[512], zip);
    58             putRes(res, arr);
    59             log("Reading " + res + " took " + (timeNow() - bef) + "ms");
    60         }
    61         return (byte[]) arr;
    62     }
    63 
    64     @JavaScriptBody(args = { "res" }, body = "var r = this[res]; return r ? r : null;")
    65     private native Object findResImpl(String res);
    66 
    67     @JavaScriptBody(args = { "res", "arr" }, body = "this[res] = arr;")
    68     private native void putRes(String res, Object arr);
    69     
    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);
    74     }
    75 
    76     @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(0);")
    77     private static native void enlargeBytes(byte[] arr, int len);
    78 
    79     @JavaScriptBody(args = { "arr", "len" }, body = "arr.splice(len, arr.length - len);")
    80     private static native void sliceArray(byte[] arr, int len);
    81 
    82     private static Object readFully(byte[] arr, InputStream zip) throws IOException {
    83         int offset = 0;
    84         for (;;) {
    85             int len = zip.read(arr, offset, arr.length - offset);
    86             if (len == -1) {
    87                 break;
    88             }
    89             offset += len;
    90             if (offset == arr.length) {
    91                 enlargeBytes(arr, arr.length + 4096);
    92             }
    93         }
    94         sliceArray(arr, offset);
    95         return arr;
    96     }
    97 
    98     private static long timeNow() {
    99         double time = m();
   100         if (time >= 0) {
   101             return (long)time;
   102         }
   103         return org.apidesign.bck2brwsr.emul.lang.System.currentTimeMillis();
   104     }
   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();"
   109     )
   110     private static native double m();
   111     
   112     
   113 }