vm/src/test/java/org/apidesign/vm4brwsr/BytesLoader.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 17 Dec 2012 09:46:09 +0100
changeset 336 4c0c01da763d
parent 334 b5dd05670bef
permissions -rw-r--r--
If available, prefer emulation
jaroslav@298
     1
/**
jaroslav@298
     2
 * Back 2 Browser Bytecode Translator
jaroslav@298
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@298
     4
 *
jaroslav@298
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@298
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@298
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@298
     8
 *
jaroslav@298
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@298
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@298
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@298
    12
 * GNU General Public License for more details.
jaroslav@298
    13
 *
jaroslav@298
    14
 * You should have received a copy of the GNU General Public License
jaroslav@298
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@298
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@298
    17
 */
jaroslav@298
    18
package org.apidesign.vm4brwsr;
jaroslav@298
    19
jaroslav@298
    20
import java.io.IOException;
jaroslav@298
    21
import java.io.InputStream;
jaroslav@336
    22
import java.net.URL;
jaroslav@336
    23
import java.util.Enumeration;
jaroslav@298
    24
import java.util.Set;
jaroslav@298
    25
import java.util.TreeSet;
jaroslav@298
    26
jaroslav@298
    27
/**
jaroslav@298
    28
 *
jaroslav@298
    29
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@298
    30
 */
jaroslav@298
    31
public final class BytesLoader {
jaroslav@298
    32
    private static Set<String> requested = new TreeSet<String>();
jaroslav@298
    33
jaroslav@298
    34
    public byte[] get(String name) throws IOException {
jaroslav@298
    35
        if (!requested.add(name)) {
jaroslav@298
    36
            throw new IllegalStateException("Requested for second time: " + name);
jaroslav@298
    37
        }
jaroslav@334
    38
        byte[] arr = readClass(name);
jaroslav@334
    39
        /*
jaroslav@334
    40
        System.err.print("loader['" + name + "'] = [");
jaroslav@334
    41
        for (int i = 0; i < arr.length; i++) {
jaroslav@334
    42
        if (i > 0) {
jaroslav@334
    43
        System.err.print(", ");
jaroslav@334
    44
        }
jaroslav@334
    45
        System.err.print(arr[i]);
jaroslav@334
    46
        }
jaroslav@334
    47
        System.err.println("]");
jaroslav@334
    48
         */
jaroslav@334
    49
        return arr;
jaroslav@334
    50
    }
jaroslav@334
    51
jaroslav@334
    52
    static byte[] readClass(String name) throws IOException {
jaroslav@336
    53
        URL u = null;
jaroslav@336
    54
        Enumeration<URL> en = BytesLoader.class.getClassLoader().getResources(name);
jaroslav@336
    55
        while (en.hasMoreElements()) {
jaroslav@336
    56
            u = en.nextElement();
jaroslav@336
    57
        }
jaroslav@336
    58
        if (u == null) {
jaroslav@298
    59
            throw new IOException("Can't find " + name);
jaroslav@298
    60
        }
jaroslav@336
    61
        try (InputStream is = u.openStream()) {
jaroslav@336
    62
            byte[] arr;
jaroslav@336
    63
            arr = new byte[is.available()];
jaroslav@336
    64
            int offset = 0;
jaroslav@336
    65
            while (offset < arr.length) {
jaroslav@336
    66
                int len = is.read(arr, offset, arr.length - offset);
jaroslav@336
    67
                if (len == -1) {
jaroslav@336
    68
                    throw new IOException("Can't read " + name);
jaroslav@336
    69
                }
jaroslav@336
    70
                offset += len;
jaroslav@325
    71
            }
jaroslav@336
    72
            return arr;
jaroslav@298
    73
        }
jaroslav@298
    74
    }
jaroslav@298
    75
    
jaroslav@298
    76
}