jaroslav@644
|
1 |
/**
|
jaroslav@644
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@644
|
3 |
* Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@644
|
4 |
*
|
jaroslav@644
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@644
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@644
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@644
|
8 |
*
|
jaroslav@644
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@644
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@644
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@644
|
12 |
* GNU General Public License for more details.
|
jaroslav@644
|
13 |
*
|
jaroslav@644
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@644
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@644
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@644
|
17 |
*/
|
jaroslav@644
|
18 |
package org.apidesign.vm4brwsr;
|
jaroslav@644
|
19 |
|
jaroslav@644
|
20 |
import java.io.IOException;
|
jaroslav@644
|
21 |
import java.net.URL;
|
jaroslav@644
|
22 |
import java.util.zip.ZipEntry;
|
jaroslav@644
|
23 |
import java.util.zip.ZipInputStream;
|
jaroslav@644
|
24 |
import org.apidesign.bck2brwsr.core.JavaScriptBody;
|
jaroslav@644
|
25 |
|
jaroslav@644
|
26 |
/** Conversion from classpath to load function.
|
jaroslav@644
|
27 |
*
|
jaroslav@644
|
28 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@644
|
29 |
*/
|
jaroslav@644
|
30 |
final class Zips {
|
jaroslav@644
|
31 |
private Zips() {
|
jaroslav@644
|
32 |
}
|
jaroslav@644
|
33 |
|
jaroslav@644
|
34 |
public static void init() {
|
jaroslav@644
|
35 |
}
|
jaroslav@644
|
36 |
|
jaroslav@644
|
37 |
public static byte[] loadFromCp(Object[] classpath, String res) {
|
jaroslav@644
|
38 |
for (int i = 0; i < classpath.length; i++) {
|
jaroslav@644
|
39 |
Object c = classpath[i];
|
jaroslav@644
|
40 |
if (c instanceof String) {
|
jaroslav@644
|
41 |
try {
|
jaroslav@644
|
42 |
c = classpath[i] = toZip((String)c);
|
jaroslav@644
|
43 |
} catch (IOException ex) {
|
jaroslav@644
|
44 |
classpath[i] = ex;
|
jaroslav@644
|
45 |
}
|
jaroslav@644
|
46 |
}
|
jaroslav@644
|
47 |
if (c instanceof Zips) {
|
jaroslav@644
|
48 |
Object checkRes = ((Zips)c).findRes(res);
|
jaroslav@644
|
49 |
if (checkRes instanceof byte[]) {
|
jaroslav@644
|
50 |
return (byte[])checkRes;
|
jaroslav@644
|
51 |
}
|
jaroslav@644
|
52 |
}
|
jaroslav@644
|
53 |
}
|
jaroslav@644
|
54 |
return null;
|
jaroslav@644
|
55 |
}
|
jaroslav@644
|
56 |
|
jaroslav@644
|
57 |
@JavaScriptBody(args = { "res" }, body = "var r = this[res]; return r ? r : null;")
|
jaroslav@644
|
58 |
private native byte[] findRes(String res);
|
jaroslav@644
|
59 |
|
jaroslav@644
|
60 |
@JavaScriptBody(args = { "res", "arr" }, body = "this[res] = arr;")
|
jaroslav@644
|
61 |
private native void putRes(String res, byte[] arr);
|
jaroslav@644
|
62 |
|
jaroslav@644
|
63 |
private static Zips toZip(String path) throws IOException {
|
jaroslav@644
|
64 |
URL u = new URL(path);
|
jaroslav@644
|
65 |
ZipInputStream zip = new ZipInputStream(u.openStream());
|
jaroslav@644
|
66 |
Zips z = new Zips();
|
jaroslav@644
|
67 |
for (;;) {
|
jaroslav@644
|
68 |
ZipEntry entry = zip.getNextEntry();
|
jaroslav@644
|
69 |
if (entry == null) {
|
jaroslav@644
|
70 |
break;
|
jaroslav@644
|
71 |
}
|
jaroslav@644
|
72 |
byte[] arr = new byte[4096];
|
jaroslav@644
|
73 |
int offset = 0;
|
jaroslav@644
|
74 |
for (;;) {
|
jaroslav@644
|
75 |
int len = zip.read(arr, offset, arr.length - offset);
|
jaroslav@644
|
76 |
if (len == -1) {
|
jaroslav@644
|
77 |
break;
|
jaroslav@644
|
78 |
}
|
jaroslav@644
|
79 |
offset += len;
|
jaroslav@644
|
80 |
if (offset == arr.length) {
|
jaroslav@644
|
81 |
enlargeArray(arr, arr.length + 4096);
|
jaroslav@644
|
82 |
}
|
jaroslav@644
|
83 |
}
|
jaroslav@644
|
84 |
sliceArray(arr, offset);
|
jaroslav@644
|
85 |
z.putRes(entry.getName(), arr);
|
jaroslav@644
|
86 |
}
|
jaroslav@644
|
87 |
return z;
|
jaroslav@644
|
88 |
}
|
jaroslav@644
|
89 |
|
jaroslav@644
|
90 |
@JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(0);")
|
jaroslav@644
|
91 |
private static native void enlargeArray(byte[] arr, int len);
|
jaroslav@644
|
92 |
|
jaroslav@644
|
93 |
@JavaScriptBody(args = { "arr", "len" }, body = "arr.splice(len, arr.length - len);")
|
jaroslav@644
|
94 |
private static native void sliceArray(byte[] arr, int len);
|
jaroslav@644
|
95 |
|
jaroslav@644
|
96 |
|
jaroslav@644
|
97 |
}
|