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