rt/vm/src/test/java/org/apidesign/vm4brwsr/Resources.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 17 Jan 2017 07:04:06 +0100
changeset 1985 cd1cc103a03c
parent 1722 fd3a354d6e8f
permissions -rw-r--r--
Implementation of ClassValue for bck2brwsr
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012-2015 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 java.util.Enumeration;
    24 import net.java.html.js.JavaScriptBody;
    25 import net.java.html.js.JavaScriptResource;
    26 import org.apidesign.bck2brwsr.core.ExtraJavaScript;
    27 
    28 /**
    29  *
    30  * @author Jaroslav Tulach <jtulach@netbeans.org>
    31  */
    32 @ExtraJavaScript(resource = "org/apidesign/vm4brwsr/var.js", processByteCode = true)
    33 @JavaScriptResource("obj.js")
    34 public class Resources {
    35     @JavaScriptBody(args = {}, body = "return obj;")
    36     static Object retObj() {
    37         return null;
    38     }
    39     
    40     public static boolean isObj() {
    41         return retObj() != null;
    42     }
    43     public static boolean isResource(String name) {
    44         return Resources.class.getResource(name) != null;
    45     }
    46     
    47     public static String loadKO() throws IOException {
    48         InputStream is = Resources.class.getResourceAsStream("ko.js");
    49         return readIS(is, false);
    50     }
    51     
    52     static String loadClazz() throws IOException {
    53         Object o = new Resources();
    54         InputStream is = o.getClass().getResourceAsStream("Bck2BrwsrToolkit.class");
    55         return readIS(is, false);
    56     }
    57 
    58     private static String readIS(InputStream is, boolean asString) throws IOException {
    59         if (is == null) {
    60             return "No resource found!";
    61         }
    62         byte[] arr = new byte[4092];
    63         int len = is.read(arr);
    64         if (len < 5) {
    65             return "No data read! Len: " + len;
    66         }
    67         
    68         if (asString) {
    69             return new String(arr, 0, len, "UTF-8").toString().toString();
    70         }
    71         
    72         StringBuilder sb = new StringBuilder();
    73         sb.append("[");
    74         for (int i = 0; i < len; i++) {
    75             sb.append(arr[i]).append(", ");
    76         }
    77         
    78         return sb.toString().toString();
    79     }
    80     static long bytesToLong(byte b1, byte b2, int shift) {
    81         return (((long)b1 << 56) +
    82                 ((long)b2 & 255) << 48) >> shift;
    83     }
    84 
    85     public static String loadHello() throws IOException {
    86         Enumeration<URL> en;
    87         try {
    88             en = Resources.class.getClassLoader().getResources("META-INF/ahoj");
    89         } catch (SecurityException ex) {
    90             return "SecurityException";
    91         }
    92         StringBuilder sb = new StringBuilder();
    93         while (en.hasMoreElements()) {
    94             URL url = en.nextElement();
    95             sb.append(readIS(url.openStream(), true));
    96         }
    97         String s = sb.toString();
    98         s = s + s.hashCode();
    99         return s.toString();
   100     }
   101     public static String loadJustHello() throws IOException {
   102         URL url = Resources.class.getResource("/META-INF/ahoj");
   103         StringBuilder sb = new StringBuilder();
   104         sb.append(readIS(url.openStream(), true));
   105         return sb.toString().toString();
   106     }
   107 }