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@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@298: InputStream is = BytesLoader.class.getClassLoader().getResourceAsStream(name); jaroslav@298: if (is == null) { jaroslav@298: throw new IOException("Can't find " + name); jaroslav@298: } jaroslav@298: byte[] arr = new byte[is.available()]; jaroslav@298: int len = is.read(arr); jaroslav@298: if (len != arr.length) { jaroslav@298: throw new IOException("Read only " + len + " wanting " + arr.length); jaroslav@298: } jaroslav@298: /* jaroslav@298: System.err.print("loader['" + name + "'] = ["); jaroslav@298: for (int i = 0; i < arr.length; i++) { jaroslav@298: if (i > 0) { jaroslav@298: System.err.print(", "); jaroslav@298: } jaroslav@298: System.err.print(arr[i]); jaroslav@298: } jaroslav@298: System.err.println("]"); jaroslav@298: */ jaroslav@298: return arr; jaroslav@298: } jaroslav@298: jaroslav@298: }