jaroslav@1639: /** jaroslav@1639: * Back 2 Browser Bytecode Translator jaroslav@1639: * Copyright (C) 2012 Jaroslav Tulach jaroslav@1639: * jaroslav@1639: * This program is free software: you can redistribute it and/or modify jaroslav@1639: * it under the terms of the GNU General Public License as published by jaroslav@1639: * the Free Software Foundation, version 2 of the License. jaroslav@1639: * jaroslav@1639: * This program is distributed in the hope that it will be useful, jaroslav@1639: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@1639: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@1639: * GNU General Public License for more details. jaroslav@1639: * jaroslav@1639: * You should have received a copy of the GNU General Public License jaroslav@1639: * along with this program. Look for COPYING file in the top folder. jaroslav@1639: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@1639: */ jaroslav@1639: package org.apidesign.bck2brwsr.vm8; jaroslav@1639: jaroslav@1639: import java.io.IOException; jaroslav@1639: import java.io.InputStream; jaroslav@1639: import java.net.URL; jaroslav@1639: import java.util.Enumeration; jaroslav@1639: jaroslav@1639: /** jaroslav@1639: * jaroslav@1639: * @author Jaroslav Tulach jaroslav@1639: */ jaroslav@1639: public final class BytesLoader { jaroslav@1639: public byte[] get(String name) throws IOException { jaroslav@1639: byte[] arr = readClass(name); jaroslav@1639: /* jaroslav@1639: System.err.print("loader['" + name + "'] = ["); jaroslav@1639: for (int i = 0; i < arr.length; i++) { jaroslav@1639: if (i > 0) { jaroslav@1639: System.err.print(", "); jaroslav@1639: } jaroslav@1639: System.err.print(arr[i]); jaroslav@1639: } jaroslav@1639: System.err.println("]"); jaroslav@1639: */ jaroslav@1639: return arr; jaroslav@1639: } jaroslav@1639: jaroslav@1639: static byte[] readClass(String name) throws IOException { jaroslav@1639: URL u = null; jaroslav@1639: Enumeration en = BytesLoader.class.getClassLoader().getResources(name); jaroslav@1639: while (en.hasMoreElements()) { jaroslav@1639: u = en.nextElement(); jaroslav@1639: } jaroslav@1639: if (u == null) { jaroslav@1639: throw new IOException("Can't find " + name); jaroslav@1639: } jaroslav@1639: try (InputStream is = u.openStream()) { jaroslav@1639: byte[] arr; jaroslav@1639: arr = new byte[is.available()]; jaroslav@1639: int offset = 0; jaroslav@1639: while (offset < arr.length) { jaroslav@1639: int len = is.read(arr, offset, arr.length - offset); jaroslav@1639: if (len == -1) { jaroslav@1639: throw new IOException("Can't read " + name); jaroslav@1639: } jaroslav@1639: offset += len; jaroslav@1639: } jaroslav@1639: return arr; jaroslav@1639: } jaroslav@1639: } jaroslav@1639: jaroslav@1639: }