2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.vm4brwsr;
20 import java.io.IOException;
21 import java.io.InputStream;
23 import java.util.Enumeration;
24 import net.java.html.js.JavaScriptBody;
25 import net.java.html.js.JavaScriptResource;
29 * @author Jaroslav Tulach <jtulach@netbeans.org>
31 @JavaScriptResource("obj.js")
32 public class Resources {
33 @JavaScriptBody(args = {}, body = "return obj;")
34 static Object retObj() {
38 public static boolean isObj() {
39 return retObj() != null;
41 public static boolean isResource() {
42 return Resources.class.getResource("obj.js") != null;
45 public static String loadKO() throws IOException {
46 InputStream is = Resources.class.getResourceAsStream("ko.js");
47 return readIS(is, false);
50 static String loadClazz() throws IOException {
51 Object o = new Resources();
52 InputStream is = o.getClass().getResourceAsStream("Bck2BrwsrToolkit.class");
53 return readIS(is, false);
56 private static String readIS(InputStream is, boolean asString) throws IOException {
58 return "No resource found!";
60 byte[] arr = new byte[4092];
61 int len = is.read(arr);
63 return "No data read! Len: " + len;
67 return new String(arr, 0, len, "UTF-8").toString().toString();
70 StringBuilder sb = new StringBuilder();
72 for (int i = 0; i < len; i++) {
73 sb.append(arr[i]).append(", ");
76 return sb.toString().toString();
78 static long bytesToLong(byte b1, byte b2, int shift) {
79 return (((long)b1 << 56) +
80 ((long)b2 & 255) << 48) >> shift;
83 public static String loadHello() throws IOException {
86 en = Resources.class.getClassLoader().getResources("META-INF/ahoj");
87 } catch (SecurityException ex) {
88 return "SecurityException";
90 StringBuilder sb = new StringBuilder();
91 while (en.hasMoreElements()) {
92 URL url = en.nextElement();
93 sb.append(readIS(url.openStream(), true));
95 String s = sb.toString();
99 public static String loadJustHello() throws IOException {
100 URL url = Resources.class.getResource("/META-INF/ahoj");
101 StringBuilder sb = new StringBuilder();
102 sb.append(readIS(url.openStream(), true));
103 return sb.toString().toString();