rt/emul/zip/src/main/java/org/apidesign/bck2brwsr/vmzip/ZipResources.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 09 May 2014 08:37:20 +0200
branchclosure
changeset 1551 0002739b6d1f
parent 1550 rt/vm/src/main/java/org/apidesign/vm4brwsr/ZipHandler.java@cb9e273dfd51
child 1555 71e68f7ed23f
permissions -rw-r--r--
Moving Zip tests into right locations
     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.bck2brwsr.vmzip;
    19 
    20 import java.io.ByteArrayInputStream;
    21 import java.io.IOException;
    22 import java.io.InputStream;
    23 import org.apidesign.bck2brwsr.core.Exported;
    24 import org.apidesign.bck2brwsr.core.JavaScriptBody;
    25 import org.apidesign.bck2brwsr.emul.zip.FastJar;
    26 import org.apidesign.vm4brwsr.Bck2Brwsr;
    27 
    28 /** Conversion from classpath to load function.
    29  *
    30  * @author Jaroslav Tulach <jtulach@netbeans.org>
    31  */
    32 @Exported
    33 public final class ZipResources implements Bck2Brwsr.Resources {
    34     private final FastJar fj;
    35 
    36     @Exported
    37     public ZipResources(byte[] zipData) throws IOException {
    38 //        long bef = timeNow();
    39         fj = new FastJar(zipData);
    40         for (FastJar.Entry e : fj.list()) {
    41             putRes(e.name, e);
    42         }
    43 //        log("Iterating thru " + path + " took " + (timeNow() - bef) + "ms");
    44     }
    45     
    46     @JavaScriptBody(args = { "arr" }, body = "return arr.length;")
    47     private static native int length(Object arr);
    48     @JavaScriptBody(args = { "arr", "index", "value" }, body = "arr[index] = value; return value;")
    49     private static native Object set(Object arr, int index, Object value);
    50     
    51     @JavaScriptBody(args = { "msg" }, body = "if (typeof console !== 'undefined') console.log(msg.toString());")
    52     private static native void log(String msg);
    53 
    54     private byte[] findRes(String res) throws IOException {
    55         Object arr = findResImpl(res);
    56         if (arr instanceof FastJar.Entry) {
    57             long bef = timeNow();
    58             InputStream zip = fj.getInputStream((FastJar.Entry)arr);
    59             arr = readFully(new byte[512], zip);
    60             putRes(res, arr);
    61             log("Reading " + res + " took " + (timeNow() - bef) + "ms");
    62         }
    63         return (byte[]) arr;
    64     }
    65 
    66     @Override
    67     public InputStream get(String resource) throws IOException {
    68         byte[] arr = findRes(resource);
    69         return arr == null ? null : new ByteArrayInputStream(arr);
    70     }
    71 
    72     @JavaScriptBody(args = { "res" }, body = "var r = this[res]; return r ? r : null;")
    73     private native Object findResImpl(String res);
    74 
    75     @JavaScriptBody(args = { "res", "arr" }, body = "this[res] = arr;")
    76     private native void putRes(String res, Object arr);
    77     
    78     @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(0);")
    79     private static native void enlargeBytes(byte[] arr, int len);
    80 
    81     @JavaScriptBody(args = { "arr", "len" }, body = "arr.splice(len, arr.length - len);")
    82     private static native void sliceArray(byte[] arr, int len);
    83 
    84     private static Object readFully(byte[] arr, InputStream zip) throws IOException {
    85         int offset = 0;
    86         for (;;) {
    87             int len = zip.read(arr, offset, arr.length - offset);
    88             if (len == -1) {
    89                 break;
    90             }
    91             offset += len;
    92             if (offset == arr.length) {
    93                 enlargeBytes(arr, arr.length + 4096);
    94             }
    95         }
    96         sliceArray(arr, offset);
    97         return arr;
    98     }
    99 
   100     private static long timeNow() {
   101         double time = m();
   102         if (time >= 0) {
   103             return (long)time;
   104         }
   105         return org.apidesign.bck2brwsr.emul.lang.System.currentTimeMillis();
   106     }
   107     @JavaScriptBody(args = {}, body = 
   108         "if (typeof window.performance === 'undefined') return -1;\n"
   109       + "if (typeof window.performance.now === 'undefined') return -1;\n"
   110       + "return window.performance.now();"
   111     )
   112     private static native double m();
   113 
   114     
   115 }