vm/src/test/java/org/apidesign/vm4brwsr/BytesLoader.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 11 Dec 2012 09:36:44 +0100
branchlazyvm
changeset 298 885acca2fa0b
child 325 acba19bef022
permissions -rw-r--r--
Creating Bck2Brwsr entrypoint to for those who wish to generate their JavaScript based Java VM
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@298
    22
import java.util.Set;
jaroslav@298
    23
import java.util.TreeSet;
jaroslav@298
    24
jaroslav@298
    25
/**
jaroslav@298
    26
 *
jaroslav@298
    27
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@298
    28
 */
jaroslav@298
    29
public final class BytesLoader {
jaroslav@298
    30
    private static Set<String> requested = new TreeSet<String>();
jaroslav@298
    31
jaroslav@298
    32
    public byte[] get(String name) throws IOException {
jaroslav@298
    33
        if (!requested.add(name)) {
jaroslav@298
    34
            throw new IllegalStateException("Requested for second time: " + name);
jaroslav@298
    35
        }
jaroslav@298
    36
        InputStream is = BytesLoader.class.getClassLoader().getResourceAsStream(name);
jaroslav@298
    37
        if (is == null) {
jaroslav@298
    38
            throw new IOException("Can't find " + name);
jaroslav@298
    39
        }
jaroslav@298
    40
        byte[] arr = new byte[is.available()];
jaroslav@298
    41
        int len = is.read(arr);
jaroslav@298
    42
        if (len != arr.length) {
jaroslav@298
    43
            throw new IOException("Read only " + len + " wanting " + arr.length);
jaroslav@298
    44
        }
jaroslav@298
    45
        /*
jaroslav@298
    46
        System.err.print("loader['" + name + "'] = [");
jaroslav@298
    47
        for (int i = 0; i < arr.length; i++) {
jaroslav@298
    48
        if (i > 0) {
jaroslav@298
    49
        System.err.print(", ");
jaroslav@298
    50
        }
jaroslav@298
    51
        System.err.print(arr[i]);
jaroslav@298
    52
        }
jaroslav@298
    53
        System.err.println("]");
jaroslav@298
    54
         */
jaroslav@298
    55
        return arr;
jaroslav@298
    56
    }
jaroslav@298
    57
    
jaroslav@298
    58
}