jaroslav@114
|
1 |
/**
|
jaroslav@114
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@114
|
3 |
* Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@114
|
4 |
*
|
jaroslav@114
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@114
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@114
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@114
|
8 |
*
|
jaroslav@114
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@114
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@114
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@114
|
12 |
* GNU General Public License for more details.
|
jaroslav@114
|
13 |
*
|
jaroslav@114
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@114
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@114
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@114
|
17 |
*/
|
jaroslav@114
|
18 |
package org.apidesign.vm4brwsr;
|
jaroslav@114
|
19 |
|
jaroslav@181
|
20 |
import java.io.IOException;
|
jaroslav@1493
|
21 |
import java.io.InputStream;
|
jaroslav@1497
|
22 |
import java.net.URL;
|
jaroslav@1497
|
23 |
import java.util.Enumeration;
|
jaroslav@181
|
24 |
|
jaroslav@114
|
25 |
/**
|
jaroslav@114
|
26 |
*
|
jaroslav@114
|
27 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@114
|
28 |
*/
|
jaroslav@1493
|
29 |
public class Resources {
|
jaroslav@1493
|
30 |
public static String loadKO() throws IOException {
|
jaroslav@1493
|
31 |
InputStream is = Resources.class.getResourceAsStream("ko.js");
|
jaroslav@1497
|
32 |
return readIS(is, false);
|
jaroslav@1497
|
33 |
}
|
jaroslav@1497
|
34 |
|
jaroslav@1497
|
35 |
static String loadClazz() throws IOException {
|
jaroslav@1497
|
36 |
InputStream is = Resources.class.getResourceAsStream("Bck2BrwsrToolkit.class");
|
jaroslav@1497
|
37 |
return readIS(is, false);
|
jaroslav@1497
|
38 |
}
|
jaroslav@1497
|
39 |
|
jaroslav@1497
|
40 |
private static String readIS(InputStream is, boolean asString) throws IOException {
|
jaroslav@1493
|
41 |
if (is == null) {
|
jaroslav@1493
|
42 |
return "No resource found!";
|
jaroslav@1493
|
43 |
}
|
jaroslav@1493
|
44 |
byte[] arr = new byte[4092];
|
jaroslav@1493
|
45 |
int len = is.read(arr);
|
jaroslav@1497
|
46 |
if (len < 5) {
|
jaroslav@1497
|
47 |
return "No data read! Len: " + len;
|
jaroslav@1493
|
48 |
}
|
jaroslav@1497
|
49 |
|
jaroslav@1497
|
50 |
if (asString) {
|
jaroslav@1497
|
51 |
return new String(arr, 0, len, "UTF-8").toString().toString();
|
jaroslav@1496
|
52 |
}
|
jaroslav@1496
|
53 |
|
jaroslav@1496
|
54 |
StringBuilder sb = new StringBuilder();
|
jaroslav@1496
|
55 |
sb.append("[");
|
jaroslav@1496
|
56 |
for (int i = 0; i < len; i++) {
|
jaroslav@1496
|
57 |
sb.append(arr[i]).append(", ");
|
jaroslav@1496
|
58 |
}
|
jaroslav@1496
|
59 |
|
jaroslav@1496
|
60 |
return sb.toString().toString();
|
jaroslav@1496
|
61 |
}
|
jaroslav@1462
|
62 |
static long bytesToLong(byte b1, byte b2, int shift) {
|
jaroslav@1462
|
63 |
return (((long)b1 << 56) +
|
jaroslav@1462
|
64 |
((long)b2 & 255) << 48) >> shift;
|
jaroslav@1462
|
65 |
}
|
jaroslav@1497
|
66 |
|
jaroslav@1558
|
67 |
public static String loadHello() throws IOException {
|
jaroslav@1497
|
68 |
Enumeration<URL> en;
|
jaroslav@1497
|
69 |
try {
|
jaroslav@1497
|
70 |
en = Resources.class.getClassLoader().getResources("META-INF/ahoj");
|
jaroslav@1497
|
71 |
} catch (SecurityException ex) {
|
jaroslav@1497
|
72 |
return "SecurityException";
|
jaroslav@1497
|
73 |
}
|
jaroslav@1497
|
74 |
StringBuilder sb = new StringBuilder();
|
jaroslav@1497
|
75 |
while (en.hasMoreElements()) {
|
jaroslav@1497
|
76 |
URL url = en.nextElement();
|
jaroslav@1497
|
77 |
sb.append(readIS(url.openStream(), true));
|
jaroslav@1497
|
78 |
}
|
jaroslav@1497
|
79 |
return sb.toString().toString();
|
jaroslav@1497
|
80 |
}
|
jaroslav@1558
|
81 |
public static String loadJustHello() throws IOException {
|
jaroslav@1497
|
82 |
URL url = Resources.class.getResource("/META-INF/ahoj");
|
jaroslav@1497
|
83 |
StringBuilder sb = new StringBuilder();
|
jaroslav@1497
|
84 |
sb.append(readIS(url.openStream(), true));
|
jaroslav@1497
|
85 |
return sb.toString().toString();
|
jaroslav@1497
|
86 |
}
|
jaroslav@114
|
87 |
}
|