vm/src/main/java/org/apidesign/vm4brwsr/Zips.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 05 Feb 2013 13:19:06 +0100
branchemul
changeset 671 99fa4fe6b980
child 672 add357fd6c5c
permissions -rw-r--r--
Don't really initialize the classes. Just pretend they will be initialized so bck2brwsr includes them in transitive closure
     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.net.URL;
    22 import java.util.zip.ZipEntry;
    23 import java.util.zip.ZipInputStream;
    24 import org.apidesign.bck2brwsr.core.JavaScriptBody;
    25 
    26 /** Conversion from classpath to load function.
    27  *
    28  * @author Jaroslav Tulach <jtulach@netbeans.org>
    29  */
    30 final class Zips {
    31     private Zips() {
    32     }
    33     
    34     public static void init() {
    35     }
    36     
    37     public static byte[] loadFromCp(Object[] classpath, String res) {
    38         for (int i = 0; i < classpath.length; i++) {
    39             Object c = classpath[i];
    40             if (c instanceof String) {
    41                 try {
    42                     c = classpath[i] = toZip((String)c);
    43                 } catch (IOException ex) {
    44                     classpath[i] = ex;
    45                 }
    46             }
    47             if (c instanceof Zips) {
    48                 Object checkRes = ((Zips)c).findRes(res);
    49                 if (checkRes instanceof byte[]) {
    50                     return (byte[])checkRes;
    51                 }
    52             }
    53         }
    54         return null;
    55     }
    56 
    57     @JavaScriptBody(args = { "res" }, body = "var r = this[res]; return r ? r : null;")
    58     private native byte[] findRes(String res);
    59 
    60     @JavaScriptBody(args = { "res", "arr" }, body = "this[res] = arr;")
    61     private native void putRes(String res, byte[] arr);
    62     
    63     private static Zips toZip(String path) throws IOException {
    64         URL u = new URL(path);
    65         ZipInputStream zip = new ZipInputStream(u.openStream());
    66         Zips z = new Zips();
    67         for (;;) {
    68             ZipEntry entry = zip.getNextEntry();
    69             if (entry == null) {
    70                 break;
    71             }
    72             byte[] arr = new byte[4096];
    73             int offset = 0;
    74             for (;;) {
    75                 int len = zip.read(arr, offset, arr.length - offset);
    76                 if (len == -1) {
    77                     break;
    78                 }
    79                 offset += len;
    80                 if (offset == arr.length) {
    81                     enlargeArray(arr, arr.length + 4096);
    82                 }
    83             }
    84             sliceArray(arr, offset);
    85             z.putRes(entry.getName(), arr);
    86         }
    87         return z;
    88     }
    89 
    90     @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(0);")
    91     private static native void enlargeArray(byte[] arr, int len);
    92 
    93     @JavaScriptBody(args = { "arr", "len" }, body = "arr.splice(len, arr.length - len);")
    94     private static native void sliceArray(byte[] arr, int len);
    95     
    96     
    97 }