rt/vm/src/test/java/org/apidesign/vm4brwsr/Resources.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 11 May 2014 23:06:45 +0200
branchclosure
changeset 1558 0c5a8b83182a
parent 1513 ba912ef24b27
child 1587 bf08bd96d408
permissions -rw-r--r--
Full obfuscation of libraries works OK
     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.io.InputStream;
    22 import java.net.URL;
    23 import java.util.Enumeration;
    24 
    25 /**
    26  *
    27  * @author Jaroslav Tulach <jtulach@netbeans.org>
    28  */
    29 public class Resources {
    30     public static String loadKO() throws IOException {
    31         InputStream is = Resources.class.getResourceAsStream("ko.js");
    32         return readIS(is, false);
    33     }
    34     
    35     static String loadClazz() throws IOException {
    36         InputStream is = Resources.class.getResourceAsStream("Bck2BrwsrToolkit.class");
    37         return readIS(is, false);
    38     }
    39 
    40     private static String readIS(InputStream is, boolean asString) throws IOException {
    41         if (is == null) {
    42             return "No resource found!";
    43         }
    44         byte[] arr = new byte[4092];
    45         int len = is.read(arr);
    46         if (len < 5) {
    47             return "No data read! Len: " + len;
    48         }
    49         
    50         if (asString) {
    51             return new String(arr, 0, len, "UTF-8").toString().toString();
    52         }
    53         
    54         StringBuilder sb = new StringBuilder();
    55         sb.append("[");
    56         for (int i = 0; i < len; i++) {
    57             sb.append(arr[i]).append(", ");
    58         }
    59         
    60         return sb.toString().toString();
    61     }
    62     static long bytesToLong(byte b1, byte b2, int shift) {
    63         return (((long)b1 << 56) +
    64                 ((long)b2 & 255) << 48) >> shift;
    65     }
    66 
    67     public static String loadHello() throws IOException {
    68         Enumeration<URL> en;
    69         try {
    70             en = Resources.class.getClassLoader().getResources("META-INF/ahoj");
    71         } catch (SecurityException ex) {
    72             return "SecurityException";
    73         }
    74         StringBuilder sb = new StringBuilder();
    75         while (en.hasMoreElements()) {
    76             URL url = en.nextElement();
    77             sb.append(readIS(url.openStream(), true));
    78         }
    79         return sb.toString().toString();
    80     }
    81     public static String loadJustHello() throws IOException {
    82         URL url = Resources.class.getResource("/META-INF/ahoj");
    83         StringBuilder sb = new StringBuilder();
    84         sb.append(readIS(url.openStream(), true));
    85         return sb.toString().toString();
    86     }
    87 }