rt/vm/src/main/java/org/apidesign/vm4brwsr/ClassPath.java
branchclosure
changeset 1551 0002739b6d1f
parent 1550 cb9e273dfd51
child 1554 7ba27baf5f3f
     1.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/ClassPath.java	Wed May 07 17:24:29 2014 +0200
     1.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/ClassPath.java	Fri May 09 08:37:20 2014 +0200
     1.3 @@ -19,6 +19,7 @@
     1.4  
     1.5  import java.io.ByteArrayInputStream;
     1.6  import java.io.IOException;
     1.7 +import java.io.InputStream;
     1.8  import java.net.URL;
     1.9  import org.apidesign.bck2brwsr.core.JavaScriptBody;
    1.10  
    1.11 @@ -46,9 +47,9 @@
    1.12              if (c instanceof String) {
    1.13                  try {
    1.14                      String url = (String)c;
    1.15 -                    final ZipHandler z = ZipHandler.toZip(url);
    1.16 +                    final Bck2Brwsr.Resources z = toZip(url);
    1.17                      c = set(classpath, i, z);
    1.18 -                    final byte[] man = z.findRes("META-INF/MANIFEST.MF");
    1.19 +                    final byte[] man = readBytes(z, "META-INF/MANIFEST.MF");
    1.20                      if (man != null) {
    1.21                          String mainClass = processClassPathAttr(man, url, classpath);
    1.22  //                        if (mainClass != null) {
    1.23 @@ -62,8 +63,8 @@
    1.24              }
    1.25              if (res != null) {
    1.26                  byte[] checkRes;
    1.27 -                if (c instanceof ZipHandler) {
    1.28 -                    checkRes = ((ZipHandler)c).findRes(res);
    1.29 +                if (c instanceof Bck2Brwsr.Resources) {
    1.30 +                    checkRes = readBytes((Bck2Brwsr.Resources)c, res);
    1.31                      if (checkRes != null && --skip < 0) {
    1.32                          return checkRes;
    1.33                      }
    1.34 @@ -116,4 +117,36 @@
    1.35  
    1.36      @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(null); return arr;")
    1.37      private static native Object enlargeArray(Object arr, int len);
    1.38 +    
    1.39 +    private static Bck2Brwsr.Resources toZip(String path) throws IOException {
    1.40 +        URL u = new URL(path);
    1.41 +        byte[] zipData = (byte[]) u.getContent(new Class[]{byte[].class});
    1.42 +        Bck2Brwsr.Resources r;
    1.43 +        try {
    1.44 +            Class<?> fastJar = Class.forName("org.apidesign.bck2brwsr.vmzip.ZipResources");
    1.45 +            return (Bck2Brwsr.Resources) fastJar.getConstructor(byte[].class).newInstance(zipData);
    1.46 +        } catch (Exception ex) {
    1.47 +            log("Reading JARs is only possible with enum.zip module included: " + ex.getMessage());
    1.48 +            ex.printStackTrace();
    1.49 +            throw new IOException(ex);
    1.50 +        }
    1.51 +    }
    1.52 +    
    1.53 +    private static byte[] readBytes(Bck2Brwsr.Resources r, String res) throws IOException {
    1.54 +        InputStream is = r.get(res);
    1.55 +        if (is == null) {
    1.56 +            return null;
    1.57 +        }
    1.58 +        byte[] arr = new byte[is.available()];
    1.59 +        int off = 0;
    1.60 +        for (;;) {
    1.61 +            int len = is.read(arr, off, arr.length - off);
    1.62 +            if (len == -1) {
    1.63 +                break;
    1.64 +            }
    1.65 +            off += len;
    1.66 +        }
    1.67 +        is.close();
    1.68 +        return arr;
    1.69 +    }
    1.70  }