jaroslav@114: /** jaroslav@114: * Back 2 Browser Bytecode Translator jaroslav@114: * Copyright (C) 2012 Jaroslav Tulach jaroslav@114: * jaroslav@114: * This program is free software: you can redistribute it and/or modify jaroslav@114: * it under the terms of the GNU General Public License as published by jaroslav@114: * the Free Software Foundation, version 2 of the License. jaroslav@114: * jaroslav@114: * This program is distributed in the hope that it will be useful, jaroslav@114: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@114: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@114: * GNU General Public License for more details. jaroslav@114: * jaroslav@114: * You should have received a copy of the GNU General Public License jaroslav@114: * along with this program. Look for COPYING file in the top folder. jaroslav@114: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@114: */ jaroslav@114: package org.apidesign.vm4brwsr; jaroslav@114: jaroslav@181: import java.io.IOException; jaroslav@1493: import java.io.InputStream; jaroslav@1497: import java.net.URL; jaroslav@1497: import java.util.Enumeration; jaroslav@1587: import net.java.html.js.JavaScriptBody; jaroslav@1587: import net.java.html.js.JavaScriptResource; jaroslav@181: jaroslav@114: /** jaroslav@114: * jaroslav@114: * @author Jaroslav Tulach jaroslav@114: */ jaroslav@1587: @JavaScriptResource("obj.js") jaroslav@1493: public class Resources { jaroslav@1587: @JavaScriptBody(args = {}, body = "return obj;") jaroslav@1587: static Object retObj() { jaroslav@1587: return null; jaroslav@1587: } jaroslav@1587: jaroslav@1587: public static boolean isObj() { jaroslav@1587: return retObj() != null; jaroslav@1587: } jaroslav@1587: public static boolean isResource() { jaroslav@1587: return Resources.class.getResource("obj.js") != null; jaroslav@1587: } jaroslav@1587: jaroslav@1493: public static String loadKO() throws IOException { jaroslav@1493: InputStream is = Resources.class.getResourceAsStream("ko.js"); jaroslav@1497: return readIS(is, false); jaroslav@1497: } jaroslav@1497: jaroslav@1497: static String loadClazz() throws IOException { jaroslav@1707: Object o = new Resources(); jaroslav@1707: InputStream is = o.getClass().getResourceAsStream("Bck2BrwsrToolkit.class"); jaroslav@1497: return readIS(is, false); jaroslav@1497: } jaroslav@1497: jaroslav@1497: private static String readIS(InputStream is, boolean asString) throws IOException { jaroslav@1493: if (is == null) { jaroslav@1493: return "No resource found!"; jaroslav@1493: } jaroslav@1493: byte[] arr = new byte[4092]; jaroslav@1493: int len = is.read(arr); jaroslav@1497: if (len < 5) { jaroslav@1497: return "No data read! Len: " + len; jaroslav@1493: } jaroslav@1497: jaroslav@1497: if (asString) { jaroslav@1497: return new String(arr, 0, len, "UTF-8").toString().toString(); jaroslav@1496: } jaroslav@1496: jaroslav@1496: StringBuilder sb = new StringBuilder(); jaroslav@1496: sb.append("["); jaroslav@1496: for (int i = 0; i < len; i++) { jaroslav@1496: sb.append(arr[i]).append(", "); jaroslav@1496: } jaroslav@1496: jaroslav@1496: return sb.toString().toString(); jaroslav@1496: } jaroslav@1462: static long bytesToLong(byte b1, byte b2, int shift) { jaroslav@1462: return (((long)b1 << 56) + jaroslav@1462: ((long)b2 & 255) << 48) >> shift; jaroslav@1462: } jaroslav@1497: jaroslav@1558: public static String loadHello() throws IOException { jaroslav@1497: Enumeration en; jaroslav@1497: try { jaroslav@1497: en = Resources.class.getClassLoader().getResources("META-INF/ahoj"); jaroslav@1497: } catch (SecurityException ex) { jaroslav@1497: return "SecurityException"; jaroslav@1497: } jaroslav@1497: StringBuilder sb = new StringBuilder(); jaroslav@1497: while (en.hasMoreElements()) { jaroslav@1497: URL url = en.nextElement(); jaroslav@1497: sb.append(readIS(url.openStream(), true)); jaroslav@1497: } jaroslav@1497: return sb.toString().toString(); jaroslav@1497: } jaroslav@1558: public static String loadJustHello() throws IOException { jaroslav@1497: URL url = Resources.class.getResource("/META-INF/ahoj"); jaroslav@1497: StringBuilder sb = new StringBuilder(); jaroslav@1497: sb.append(readIS(url.openStream(), true)); jaroslav@1497: return sb.toString().toString(); jaroslav@1497: } jaroslav@114: }