rt/vm/src/test/java/org/apidesign/vm4brwsr/BytesLoader.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 14 Oct 2013 16:44:55 +0200
changeset 1373 c4e57ec5f0df
parent 772 d382dacfd73f
child 1787 ea12a3bb4b33
permissions -rw-r--r--
VM.reload can now reload a class in existing virtual machine
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
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@1373
    30
    private static final StringArray requested = new StringArray();
jaroslav@298
    31
jaroslav@298
    32
    public byte[] get(String name) throws IOException {
jaroslav@1373
    33
        if (requested.contains(name)) {
jaroslav@298
    34
            throw new IllegalStateException("Requested for second time: " + name);
jaroslav@298
    35
        }
jaroslav@1373
    36
        requested.add(name);
jaroslav@334
    37
        byte[] arr = readClass(name);
jaroslav@334
    38
        /*
jaroslav@334
    39
        System.err.print("loader['" + name + "'] = [");
jaroslav@334
    40
        for (int i = 0; i < arr.length; i++) {
jaroslav@334
    41
        if (i > 0) {
jaroslav@334
    42
        System.err.print(", ");
jaroslav@334
    43
        }
jaroslav@334
    44
        System.err.print(arr[i]);
jaroslav@334
    45
        }
jaroslav@334
    46
        System.err.println("]");
jaroslav@334
    47
         */
jaroslav@334
    48
        return arr;
jaroslav@334
    49
    }
jaroslav@334
    50
jaroslav@334
    51
    static byte[] readClass(String name) throws IOException {
jaroslav@336
    52
        URL u = null;
jaroslav@336
    53
        Enumeration<URL> en = BytesLoader.class.getClassLoader().getResources(name);
jaroslav@336
    54
        while (en.hasMoreElements()) {
jaroslav@336
    55
            u = en.nextElement();
jaroslav@336
    56
        }
jaroslav@336
    57
        if (u == null) {
jaroslav@298
    58
            throw new IOException("Can't find " + name);
jaroslav@298
    59
        }
jaroslav@336
    60
        try (InputStream is = u.openStream()) {
jaroslav@336
    61
            byte[] arr;
jaroslav@336
    62
            arr = new byte[is.available()];
jaroslav@336
    63
            int offset = 0;
jaroslav@336
    64
            while (offset < arr.length) {
jaroslav@336
    65
                int len = is.read(arr, offset, arr.length - offset);
jaroslav@336
    66
                if (len == -1) {
jaroslav@336
    67
                    throw new IOException("Can't read " + name);
jaroslav@336
    68
                }
jaroslav@336
    69
                offset += len;
jaroslav@325
    70
            }
jaroslav@336
    71
            return arr;
jaroslav@298
    72
        }
jaroslav@298
    73
    }
jaroslav@298
    74
    
jaroslav@298
    75
}