rt/vm/src/main/java/org/apidesign/vm4brwsr/ZipHandler.java
branchclosure
changeset 1551 0002739b6d1f
parent 1550 cb9e273dfd51
child 1552 5a840853d1a8
     1.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/ZipHandler.java	Wed May 07 17:24:29 2014 +0200
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,113 +0,0 @@
     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.io.InputStream;
    1.25 -import java.net.URL;
    1.26 -import org.apidesign.bck2brwsr.core.JavaScriptBody;
    1.27 -import org.apidesign.bck2brwsr.emul.zip.FastJar;
    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 ZipHandler {
    1.34 -    private final FastJar fj;
    1.35 -
    1.36 -    private ZipHandler(String path, byte[] zipData) throws IOException {
    1.37 -        long bef = timeNow();
    1.38 -        fj = new FastJar(zipData);
    1.39 -        for (FastJar.Entry e : fj.list()) {
    1.40 -            putRes(e.name, e);
    1.41 -        }
    1.42 -        log("Iterating thru " + path + " took " + (timeNow() - bef) + "ms");
    1.43 -    }
    1.44 -    
    1.45 -    public static void init() {
    1.46 -    }
    1.47 -    @JavaScriptBody(args = { "arr" }, body = "return arr.length;")
    1.48 -    private static native int length(Object arr);
    1.49 -    @JavaScriptBody(args = { "arr", "index", "value" }, body = "arr[index] = value; return value;")
    1.50 -    private static native Object set(Object arr, int index, Object value);
    1.51 -    
    1.52 -    @JavaScriptBody(args = { "msg" }, body = "if (typeof console !== 'undefined') console.log(msg.toString());")
    1.53 -    private static native void log(String msg);
    1.54 -
    1.55 -    byte[] findRes(String res) throws IOException {
    1.56 -        Object arr = findResImpl(res);
    1.57 -        if (arr instanceof FastJar.Entry) {
    1.58 -            long bef = timeNow();
    1.59 -            InputStream zip = fj.getInputStream((FastJar.Entry)arr);
    1.60 -            arr = readFully(new byte[512], zip);
    1.61 -            putRes(res, arr);
    1.62 -            log("Reading " + res + " took " + (timeNow() - bef) + "ms");
    1.63 -        }
    1.64 -        return (byte[]) arr;
    1.65 -    }
    1.66 -
    1.67 -    @JavaScriptBody(args = { "res" }, body = "var r = this[res]; return r ? r : null;")
    1.68 -    private native Object findResImpl(String res);
    1.69 -
    1.70 -    @JavaScriptBody(args = { "res", "arr" }, body = "this[res] = arr;")
    1.71 -    private native void putRes(String res, Object arr);
    1.72 -    
    1.73 -    static ZipHandler toZip(String path) throws IOException {
    1.74 -        URL u = new URL(path);
    1.75 -        byte[] zipData = (byte[]) u.getContent(new Class[] { byte[].class });
    1.76 -        return new ZipHandler(path, zipData);
    1.77 -    }
    1.78 -
    1.79 -    @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(0);")
    1.80 -    private static native void enlargeBytes(byte[] arr, int len);
    1.81 -
    1.82 -    @JavaScriptBody(args = { "arr", "len" }, body = "arr.splice(len, arr.length - len);")
    1.83 -    private static native void sliceArray(byte[] arr, int len);
    1.84 -
    1.85 -    private static Object readFully(byte[] arr, InputStream zip) throws IOException {
    1.86 -        int offset = 0;
    1.87 -        for (;;) {
    1.88 -            int len = zip.read(arr, offset, arr.length - offset);
    1.89 -            if (len == -1) {
    1.90 -                break;
    1.91 -            }
    1.92 -            offset += len;
    1.93 -            if (offset == arr.length) {
    1.94 -                enlargeBytes(arr, arr.length + 4096);
    1.95 -            }
    1.96 -        }
    1.97 -        sliceArray(arr, offset);
    1.98 -        return arr;
    1.99 -    }
   1.100 -
   1.101 -    private static long timeNow() {
   1.102 -        double time = m();
   1.103 -        if (time >= 0) {
   1.104 -            return (long)time;
   1.105 -        }
   1.106 -        return org.apidesign.bck2brwsr.emul.lang.System.currentTimeMillis();
   1.107 -    }
   1.108 -    @JavaScriptBody(args = {}, body = 
   1.109 -        "if (typeof window.performance === 'undefined') return -1;\n"
   1.110 -      + "if (typeof window.performance.now === 'undefined') return -1;\n"
   1.111 -      + "return window.performance.now();"
   1.112 -    )
   1.113 -    private static native double m();
   1.114 -    
   1.115 -    
   1.116 -}