Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 498a740

Browse files
committed
Info.coreAssets, readyFunction for main, explicit local core js
// BH 2025.04.20 adds Info.coreAssets:"coreAssets.zip" // BH 2025.04.18 enables Info.readyFunction for headless apps // BH 2025.04.17 adds option for explicit directory for core files different from j2sPath/core
1 parent 3be8d3b commit 498a740

File tree

6 files changed

+73
-26
lines changed

6 files changed

+73
-26
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20250419133113
1+
20250419180504
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20250419133113
1+
20250419180504
601 Bytes
Binary file not shown.

sources/net.sf.j2s.java.core/src/java/lang/Class.java

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2424,8 +2424,10 @@ public InputStream getResourceAsStream(String name) {
24242424
return null;
24252425
name = name.replace('\\','/');
24262426
String baseFolder = null;
2427-
String fname= name;
2427+
String fname = name;
24282428
URL url = null;
2429+
Map<String, Object> fileCache = null;
2430+
String javapath = "";
24292431
if (name.startsWith(File.temporaryDirectory)) {
24302432
data = JSUtil.getCachedFileData(name, true);
24312433
if (data == null)
@@ -2443,10 +2445,9 @@ public InputStream getResourceAsStream(String name) {
24432445
baseFolder = Clazz._Loader.getJ2SLibBase();
24442446
if (baseFolder.charAt(baseFolder.length - 1) != '/')
24452447
baseFolder += "/";
2446-
fname = baseFolder + name.substring (1);
24472448
} else {
24482449
baseFolder = Clazz._Loader.getJ2SLibBase(); // getClass().getClassLoader() uses full path
2449-
fname = baseFolder;
2450+
fname = "";
24502451
if (this.$_$base == null) {
24512452
// getClass().getResource() will be here
24522453
var pkgs = clazzName.split(".");
@@ -2461,31 +2462,33 @@ public InputStream getResourceAsStream(String name) {
24612462
}
24622463
fname += name;
24632464
}
2464-
var javapath = fname;
2465+
fname = fname.substring(1);
2466+
javapath = fname;
2467+
fname = baseFolder + fname;
24652468
try {
2466-
// if (fname.indexOf(":/") < 0) {
2467-
// var d = document.location.href.split("#")[0].split("?")[0].split("/");
2468-
// d[d.length - 1] = fname;
2469-
// fname = d.join("/");
2470-
// }
24712469
Clazz.load("java.net.URL");
24722470
url = Clazz.new_(java.net.URL.c$$S,["file:/" + fname]);
24732471
} catch (e) {
24742472
return null;
24752473
}
2476-
var fileCache = J2S.getSetJavaFileCache(null);
2477-
data = fileCache && fileCache.get$O(javapath);
2474+
fileCache = J2S.getSetJavaFileCache(null);
24782475
*/
24792476
}
2480-
if (data == null)
2481-
data = JSUtil.J2S.getFileData(fname.toString(),null,true,true);
2477+
data = (fileCache == null ? null : fileCache.get(javapath));
2478+
if (data == null && fileCache != null) {
2479+
data = fileCache.get(fname);
2480+
}
2481+
if (data == null) {
2482+
data = JSUtil.J2S.getFileData(fname,null,true,true);
2483+
}
24822484
/**
24832485
* @j2sNative
24842486
*
24852487
if (data == null || data == Boolean.FALSE || data == "error" || data.indexOf && data.indexOf("[Exception") == 0)
24862488
return null;
24872489
2488-
var bytes = (data.__BYTESIZE == 1 ? data : J2S._strToBytes(data));
2490+
// could be byte[] or BArray or String
2491+
var bytes = (data.__BYTESIZE == 1 ? data : data.data ? data.data : J2S._strToBytes(data));
24892492
Clazz.load("java.io.BufferedInputStream");
24902493
Clazz.load("java.io.ByteArrayInputStream");
24912494
var is = Clazz.new_(java.io.BufferedInputStream.c$$java_io_InputStream, [Clazz.new_(java.io.ByteArrayInputStream.c$$BA, [bytes])]);
@@ -2496,14 +2499,6 @@ public InputStream getResourceAsStream(String name) {
24962499
{
24972500
return null;
24982501
}
2499-
//
2500-
// name = resolveName(name);
2501-
// ClassLoader cl = getClassLoader0();
2502-
// if (cl == null) {
2503-
// // A system class.
2504-
// return ClassLoader.getSystemResourceAsStream(name);
2505-
// }
2506-
// return cl.getResourceAsStream(name);
25072502
}
25082503

25092504
/**

sources/net.sf.j2s.java.core/srcjs/js/j2sApplet.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// j2sApplet.js BH = Bob Hanson [email protected]
22

3+
// BH 2025.04.20 adds Info.coreAssets:"coreAssets.zip"
34
// BH 2025.04.18 enables Info.readyFunction for headless apps
45
// BH 2025.04.17 adds option for explicit directory for core files different from j2sPath/core
56
// BH 2024.11.09 makes equivalent J2S._debugCore and J2S._nozcore, as well as J2S._debugCode and J2S._nocore
@@ -2896,6 +2897,10 @@ if (ev.keyCode == 9 && ev.target["data-focuscomponent"]) {
28962897
alert("Java class " + clazz + " was not found.");
28972898
return;
28982899
}
2900+
var assets = applet.__Info.coreAssets;
2901+
if (assets) {
2902+
loadAssets(assets);
2903+
}
28992904
if (applet.__Info.code)
29002905
codePath += applet.__Info.code.replace(/\./g, "/");
29012906
codePath = codePath.substring(0,
@@ -2905,7 +2910,7 @@ if (ev.keyCode == 9 && ev.target["data-focuscomponent"]) {
29052910
Clazz.loadClass("java.lang.Thread").currentThread$().group.html5Applet = applet;
29062911
cl.main$SA(applet.__Info.args || []);
29072912
if (applet.__Info.readyFunction) {
2908-
applet.__Info.readyFunction(applet);
2913+
J2S._addExec([ this, function(){applet.__Info.readyFunction(applet);}, null, "Info.readyFunction" ]);
29092914
} else {
29102915
System.exit$(0);
29112916
}
@@ -3013,6 +3018,27 @@ if (ev.keyCode == 9 && ev.target["data-focuscomponent"]) {
30133018
return proto;
30143019
};
30153020

3021+
var loadAssets = function(assets) {
3022+
if (!assets) return;
3023+
if (typeof assets != "string") {
3024+
// assume array
3025+
for (var i = 0; i < assets.length; i++) {
3026+
loadAssets(assets[i]);
3027+
}
3028+
return;
3029+
}
3030+
try {
3031+
var bytes = J2S.getFileData(assets,null, true, true);
3032+
var bis = Clazz.loadClass("javajs.util.Rdr").getBIS$BA(bytes);
3033+
var cache = J2S.getSetJavaFileCache();
3034+
var len0 = cache.size$();
3035+
Clazz.loadClass("javajs.util.ZipTools").readFileAsMap$java_io_BufferedInputStream$java_util_Map$S(bis, cache);
3036+
System.out.println((cache.size$() - len0) + " items cached from " + assets);
3037+
} catch(e) {
3038+
System.out.println("error reading " + assets);
3039+
}
3040+
}
3041+
30163042
J2S.repaint = function(applet, asNewThread) {
30173043
// JmolObjectInterface
30183044
// asNewThread: true is from RepaintManager.repaintNow()

sources/net.sf.j2s.java.core/srcjs/swingjs2.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10686,6 +10686,7 @@ return jQuery;
1068610686
})(jQuery,document,"click mousemove mouseup touchmove touchend", "outjsmol");
1068710687
// j2sApplet.js BH = Bob Hanson [email protected]
1068810688

10689+
// BH 2025.04.20 adds Info.coreAssets:"coreAssets.zip"
1068910690
// BH 2025.04.18 enables Info.readyFunction for headless apps
1069010691
// BH 2025.04.17 adds option for explicit directory for core files different from j2sPath/core
1069110692
// BH 2024.11.09 makes equivalent J2S._debugCore and J2S._nozcore, as well as J2S._debugCode and J2S._nocore
@@ -13582,6 +13583,10 @@ if (ev.keyCode == 9 && ev.target["data-focuscomponent"]) {
1358213583
alert("Java class " + clazz + " was not found.");
1358313584
return;
1358413585
}
13586+
var assets = applet.__Info.coreAssets;
13587+
if (assets) {
13588+
loadAssets(assets);
13589+
}
1358513590
if (applet.__Info.code)
1358613591
codePath += applet.__Info.code.replace(/\./g, "/");
1358713592
codePath = codePath.substring(0,
@@ -13591,7 +13596,7 @@ if (ev.keyCode == 9 && ev.target["data-focuscomponent"]) {
1359113596
Clazz.loadClass("java.lang.Thread").currentThread$().group.html5Applet = applet;
1359213597
cl.main$SA(applet.__Info.args || []);
1359313598
if (applet.__Info.readyFunction) {
13594-
applet.__Info.readyFunction(applet);
13599+
J2S._addExec([ this, function(){applet.__Info.readyFunction(applet);}, null, "Info.readyFunction" ]);
1359513600
} else {
1359613601
System.exit$(0);
1359713602
}
@@ -13699,6 +13704,27 @@ if (ev.keyCode == 9 && ev.target["data-focuscomponent"]) {
1369913704
return proto;
1370013705
};
1370113706

13707+
var loadAssets = function(assets) {
13708+
if (!assets) return;
13709+
if (typeof assets != "string") {
13710+
// assume array
13711+
for (var i = 0; i < assets.length; i++) {
13712+
loadAssets(assets[i]);
13713+
}
13714+
return;
13715+
}
13716+
try {
13717+
var bytes = J2S.getFileData(assets,null, true, true);
13718+
var bis = Clazz.loadClass("javajs.util.Rdr").getBIS$BA(bytes);
13719+
var cache = J2S.getSetJavaFileCache();
13720+
var len0 = cache.size$();
13721+
Clazz.loadClass("javajs.util.ZipTools").readFileAsMap$java_io_BufferedInputStream$java_util_Map$S(bis, cache);
13722+
System.out.println((cache.size$() - len0) + " items cached from " + assets);
13723+
} catch(e) {
13724+
System.out.println("error reading " + assets);
13725+
}
13726+
}
13727+
1370213728
J2S.repaint = function(applet, asNewThread) {
1370313729
// JmolObjectInterface
1370413730
// asNewThread: true is from RepaintManager.repaintNow()

0 commit comments

Comments
 (0)