launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Console.java
branchlauncher
changeset 360 86f3ea771e24
parent 356 e078953818d2
child 366 ca2be963f3b9
     1.1 --- a/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Console.java	Thu Dec 20 11:03:34 2012 +0100
     1.2 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Console.java	Thu Dec 20 21:54:33 2012 +0100
     1.3 @@ -17,9 +17,12 @@
     1.4   */
     1.5  package org.apidesign.bck2brwsr.launcher;
     1.6  
     1.7 +import java.io.IOException;
     1.8 +import java.io.InputStream;
     1.9  import java.lang.reflect.InvocationTargetException;
    1.10  import java.lang.reflect.Method;
    1.11  import java.net.URL;
    1.12 +import java.util.Enumeration;
    1.13  import org.apidesign.bck2brwsr.core.JavaScriptBody;
    1.14  
    1.15  /**
    1.16 @@ -89,6 +92,37 @@
    1.17          return r == null ? "null" : r.toString().toString();
    1.18      }
    1.19  
    1.20 +    /** Helper method that inspects the classpath and loads given resource
    1.21 +     * (usually a class file). Used while running tests in Rhino.
    1.22 +     * 
    1.23 +     * @param name resource name to find
    1.24 +     * @return the array of bytes in the given resource
    1.25 +     * @throws IOException I/O in case something goes wrong
    1.26 +     */
    1.27 +    public static byte[] read(String name) throws IOException {
    1.28 +        URL u = null;
    1.29 +        Enumeration<URL> en = Console.class.getClassLoader().getResources(name);
    1.30 +        while (en.hasMoreElements()) {
    1.31 +            u = en.nextElement();
    1.32 +        }
    1.33 +        if (u == null) {
    1.34 +            throw new IOException("Can't find " + name);
    1.35 +        }
    1.36 +        try (InputStream is = u.openStream()) {
    1.37 +            byte[] arr;
    1.38 +            arr = new byte[is.available()];
    1.39 +            int offset = 0;
    1.40 +            while (offset < arr.length) {
    1.41 +                int len = is.read(arr, offset, arr.length - offset);
    1.42 +                if (len == -1) {
    1.43 +                    throw new IOException("Can't read " + name);
    1.44 +                }
    1.45 +                offset += len;
    1.46 +            }
    1.47 +            return arr;
    1.48 +        }
    1.49 +    }
    1.50 +   
    1.51      private static Object invokeMethod(String clazz, String method) 
    1.52      throws ClassNotFoundException, InvocationTargetException, 
    1.53      SecurityException, IllegalAccessException, IllegalArgumentException {