rt/vm/src/test/java/org/apidesign/vm4brwsr/BytesLoader.java
changeset 772 d382dacfd73f
parent 336 4c0c01da763d
child 1373 c4e57ec5f0df
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/BytesLoader.java	Tue Feb 26 16:54:16 2013 +0100
     1.3 @@ -0,0 +1,76 @@
     1.4 +/**
     1.5 + * Back 2 Browser Bytecode Translator
     1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, version 2 of the License.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License
    1.18 + * along with this program. Look for COPYING file in the top folder.
    1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    1.20 + */
    1.21 +package org.apidesign.vm4brwsr;
    1.22 +
    1.23 +import java.io.IOException;
    1.24 +import java.io.InputStream;
    1.25 +import java.net.URL;
    1.26 +import java.util.Enumeration;
    1.27 +import java.util.Set;
    1.28 +import java.util.TreeSet;
    1.29 +
    1.30 +/**
    1.31 + *
    1.32 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.33 + */
    1.34 +public final class BytesLoader {
    1.35 +    private static Set<String> requested = new TreeSet<String>();
    1.36 +
    1.37 +    public byte[] get(String name) throws IOException {
    1.38 +        if (!requested.add(name)) {
    1.39 +            throw new IllegalStateException("Requested for second time: " + name);
    1.40 +        }
    1.41 +        byte[] arr = readClass(name);
    1.42 +        /*
    1.43 +        System.err.print("loader['" + name + "'] = [");
    1.44 +        for (int i = 0; i < arr.length; i++) {
    1.45 +        if (i > 0) {
    1.46 +        System.err.print(", ");
    1.47 +        }
    1.48 +        System.err.print(arr[i]);
    1.49 +        }
    1.50 +        System.err.println("]");
    1.51 +         */
    1.52 +        return arr;
    1.53 +    }
    1.54 +
    1.55 +    static byte[] readClass(String name) throws IOException {
    1.56 +        URL u = null;
    1.57 +        Enumeration<URL> en = BytesLoader.class.getClassLoader().getResources(name);
    1.58 +        while (en.hasMoreElements()) {
    1.59 +            u = en.nextElement();
    1.60 +        }
    1.61 +        if (u == null) {
    1.62 +            throw new IOException("Can't find " + name);
    1.63 +        }
    1.64 +        try (InputStream is = u.openStream()) {
    1.65 +            byte[] arr;
    1.66 +            arr = new byte[is.available()];
    1.67 +            int offset = 0;
    1.68 +            while (offset < arr.length) {
    1.69 +                int len = is.read(arr, offset, arr.length - offset);
    1.70 +                if (len == -1) {
    1.71 +                    throw new IOException("Can't read " + name);
    1.72 +                }
    1.73 +                offset += len;
    1.74 +            }
    1.75 +            return arr;
    1.76 +        }
    1.77 +    }
    1.78 +    
    1.79 +}