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
     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.util.Set;
    23 import java.util.TreeSet;
    24 
    25 /**
    26  *
    27  * @author Jaroslav Tulach <jtulach@netbeans.org>
    28  */
    29 public final class BytesLoader {
    30     private static Set<String> requested = new TreeSet<String>();
    31 
    32     public byte[] get(String name) throws IOException {
    33         if (!requested.add(name)) {
    34             throw new IllegalStateException("Requested for second time: " + name);
    35         }
    36         InputStream is = BytesLoader.class.getClassLoader().getResourceAsStream(name);
    37         if (is == null) {
    38             throw new IOException("Can't find " + name);
    39         }
    40         byte[] arr = new byte[is.available()];
    41         int len = is.read(arr);
    42         if (len != arr.length) {
    43             throw new IOException("Read only " + len + " wanting " + arr.length);
    44         }
    45         /*
    46         System.err.print("loader['" + name + "'] = [");
    47         for (int i = 0; i < arr.length; i++) {
    48         if (i > 0) {
    49         System.err.print(", ");
    50         }
    51         System.err.print(arr[i]);
    52         }
    53         System.err.println("]");
    54          */
    55         return arr;
    56     }
    57     
    58 }