2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012-2015 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;
26 import org.apidesign.bck2brwsr.core.ExtraJavaScript;
30 * @author Jaroslav Tulach <jtulach@netbeans.org>
32 @ExtraJavaScript(resource = "org/apidesign/vm4brwsr/var.js", processByteCode = true)
33 @JavaScriptResource("obj.js")
34 public class Resources {
35 @JavaScriptBody(args = {}, body = "return obj;")
36 static Object retObj() {
40 public static boolean isObj() {
41 return retObj() != null;
43 public static boolean isResource(String name) {
44 return Resources.class.getResource(name) != null;
47 public static String loadKO() throws IOException {
48 InputStream is = Resources.class.getResourceAsStream("ko.js");
49 return readIS(is, false);
52 static String loadClazz() throws IOException {
53 Object o = new Resources();
54 InputStream is = o.getClass().getResourceAsStream("Bck2BrwsrToolkit.class");
55 return readIS(is, false);
58 private static String readIS(InputStream is, boolean asString) throws IOException {
60 return "No resource found!";
62 byte[] arr = new byte[4092];
63 int len = is.read(arr);
65 return "No data read! Len: " + len;
69 return new String(arr, 0, len, "UTF-8").toString().toString();
72 StringBuilder sb = new StringBuilder();
74 for (int i = 0; i < len; i++) {
75 sb.append(arr[i]).append(", ");
78 return sb.toString().toString();
80 static long bytesToLong(byte b1, byte b2, int shift) {
81 return (((long)b1 << 56) +
82 ((long)b2 & 255) << 48) >> shift;
85 public static String loadHello() throws IOException {
88 en = Resources.class.getClassLoader().getResources("META-INF/ahoj");
89 } catch (SecurityException ex) {
90 return "SecurityException";
92 StringBuilder sb = new StringBuilder();
93 while (en.hasMoreElements()) {
94 URL url = en.nextElement();
95 sb.append(readIS(url.openStream(), true));
97 String s = sb.toString();
101 public static String loadJustHello() throws IOException {
102 URL url = Resources.class.getResource("/META-INF/ahoj");
103 StringBuilder sb = new StringBuilder();
104 sb.append(readIS(url.openStream(), true));
105 return sb.toString().toString();