vm/src/test/java/org/apidesign/vm4brwsr/BytesLoader.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 16 Dec 2012 18:48:57 +0100
changeset 325 acba19bef022
parent 298 885acca2fa0b
child 334 b5dd05670bef
permissions -rw-r--r--
Real algorithm for reading (even large) arrays
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@325
    41
        int offset = 0;
jaroslav@325
    42
        while (offset < arr.length) {
jaroslav@325
    43
            int len = is.read(arr, offset, arr.length - offset);
jaroslav@325
    44
            if (len == -1) {
jaroslav@325
    45
                throw new IOException("Can't read " + name);
jaroslav@325
    46
            }
jaroslav@325
    47
            offset += len;
jaroslav@298
    48
        }
jaroslav@298
    49
        /*
jaroslav@298
    50
        System.err.print("loader['" + name + "'] = [");
jaroslav@298
    51
        for (int i = 0; i < arr.length; i++) {
jaroslav@298
    52
        if (i > 0) {
jaroslav@298
    53
        System.err.print(", ");
jaroslav@298
    54
        }
jaroslav@298
    55
        System.err.print(arr[i]);
jaroslav@298
    56
        }
jaroslav@298
    57
        System.err.println("]");
jaroslav@298
    58
         */
jaroslav@298
    59
        return arr;
jaroslav@298
    60
    }
jaroslav@298
    61
    
jaroslav@298
    62
}