vm/src/test/java/org/apidesign/vm4brwsr/BytesLoader.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 16 Dec 2012 20:48:21 +0100
changeset 334 b5dd05670bef
parent 325 acba19bef022
child 336 4c0c01da763d
permissions -rw-r--r--
Sharing the is -> byte[] loading algorithm
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@334
    36
        byte[] arr = readClass(name);
jaroslav@334
    37
        /*
jaroslav@334
    38
        System.err.print("loader['" + name + "'] = [");
jaroslav@334
    39
        for (int i = 0; i < arr.length; i++) {
jaroslav@334
    40
        if (i > 0) {
jaroslav@334
    41
        System.err.print(", ");
jaroslav@334
    42
        }
jaroslav@334
    43
        System.err.print(arr[i]);
jaroslav@334
    44
        }
jaroslav@334
    45
        System.err.println("]");
jaroslav@334
    46
         */
jaroslav@334
    47
        return arr;
jaroslav@334
    48
    }
jaroslav@334
    49
jaroslav@334
    50
    static byte[] readClass(String name) throws IOException {
jaroslav@298
    51
        InputStream is = BytesLoader.class.getClassLoader().getResourceAsStream(name);
jaroslav@298
    52
        if (is == null) {
jaroslav@298
    53
            throw new IOException("Can't find " + name);
jaroslav@298
    54
        }
jaroslav@298
    55
        byte[] arr = new byte[is.available()];
jaroslav@325
    56
        int offset = 0;
jaroslav@325
    57
        while (offset < arr.length) {
jaroslav@325
    58
            int len = is.read(arr, offset, arr.length - offset);
jaroslav@325
    59
            if (len == -1) {
jaroslav@325
    60
                throw new IOException("Can't read " + name);
jaroslav@325
    61
            }
jaroslav@325
    62
            offset += len;
jaroslav@298
    63
        }
jaroslav@298
    64
        return arr;
jaroslav@298
    65
    }
jaroslav@298
    66
    
jaroslav@298
    67
}