Skip to content

Commit 3753f36

Browse files
hansonrhansonr
authored andcommitted
resource caching fix
1 parent 17717ac commit 3753f36

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

sources/net.sf.j2s.java.core/src/swingjs/JSUtil.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.awt.Toolkit;
66
import java.io.BufferedInputStream;
77
import java.io.BufferedReader;
8+
import java.io.ByteArrayInputStream;
89
import java.io.File;
910
import java.io.IOException;
1011
import java.io.InputStream;
@@ -201,8 +202,30 @@ public static String getJavaResource(String resourceName, boolean isJavaPath,
201202
: path.endsWith(".css") ? processCSS(sdata, path) : path
202203
.endsWith(".js") ? processJS(sdata, resourceName) : sdata);
203204
}
205+
206+
public static InputStream getCachedResourceAsStream(String name) {
207+
208+
209+
// , isjavapath false, docachetrue, doprocefalse)
210+
211+
System.out.println("JSUtil getting Java resource " + name);
212+
String path = J2S.getResourcePath(name, false);
213+
if (path == null)
214+
return null;
215+
InputStream stream;
216+
Object data = getCachedFileData(path);
217+
if (data == null) {
218+
stream = getResourceAsStream(name);
219+
data = /** @j2sNative stream.$in.buf ||*/null;
220+
} else {
221+
stream = new BufferedInputStream(new ByteArrayInputStream((byte[]) data));
222+
}
223+
if (stream != null && useCache)
224+
cacheFileData(path, data);
225+
return stream;
226+
}
204227

205-
static void cacheFileData(String path, Object data) {
228+
public static void cacheFileData(String path, Object data) {
206229
if (data == null) {
207230
System.out.println("JSUtil releasing cached bytes for " + path);
208231
getFileCache().remove(path);
@@ -437,7 +460,8 @@ public static void saveFile(String fileName, Object data, String mimeType, Strin
437460
public static InputStream getResourceAsStream(String name) {
438461
return Toolkit.getDefaultToolkit().getClass().getClassLoader().getResourceAsStream(name);
439462
}
440-
463+
464+
441465
/**
442466
* All classes created by Clazz have static class loaders which are just minimal
443467
* objects in Clazz var inF.

0 commit comments

Comments
 (0)