rt/vm/src/test/java/org/apidesign/vm4brwsr/Resources.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 28 Apr 2014 20:23:44 +0200
branchclosure
changeset 1497 daa5f903b529
parent 1496 d3df935aff70
child 1513 ba912ef24b27
permissions -rw-r--r--
Testing loading of resource from an extension and preparing for loading them from multiple extensions
     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 
    63     static String loadHello() throws IOException {
    64         Enumeration<URL> en;
    65         try {
    66             en = Resources.class.getClassLoader().getResources("META-INF/ahoj");
    67         } catch (SecurityException ex) {
    68             return "SecurityException";
    69         }
    70         StringBuilder sb = new StringBuilder();
    71         while (en.hasMoreElements()) {
    72             URL url = en.nextElement();
    73             sb.append(readIS(url.openStream(), true));
    74         }
    75         return sb.toString().toString();
    76     }
    77     static String loadJustHello() throws IOException {
    78         URL url = Resources.class.getResource("/META-INF/ahoj");
    79         StringBuilder sb = new StringBuilder();
    80         sb.append(readIS(url.openStream(), true));
    81         return sb.toString().toString();
    82     }
    83 }