Skip to content

Commit 016fd75

Browse files
hansonrhansonr
authored andcommitted
implements URLConnection.useCache() and .setDefaultUseCache()
1 parent 9db9a4c commit 016fd75

File tree

6 files changed

+24
-3
lines changed

6 files changed

+24
-3
lines changed
367 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20181121131826
1+
20181121134911
367 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20181121131826
1+
20181121134911
367 Bytes
Binary file not shown.

sources/net.sf.j2s.java.core/src/javajs/util/AjaxURLConnection.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import java.net.HttpURLConnection;
77
import java.net.URL;
88
import java.net.URLConnection;
9+
import java.util.Hashtable;
10+
import java.util.Map;
911

1012
import javajs.api.js.J2SObjectInterface;
1113

@@ -71,7 +73,25 @@ public void outputString(String post) {
7173
public InputStream getInputStream() {
7274
responseCode = 200;
7375
BufferedInputStream is = getAttachedStreamData(url, false);
74-
return (is == null ? attachStreamData(url, doAjax(true)) : is);
76+
if (is != null || getUseCaches() && (is = getCachedStream(url)) != null)
77+
return is;
78+
is = attachStreamData(url, doAjax(true));
79+
if (getUseCaches() && is != null)
80+
setCachedStream(url);
81+
return is;
82+
}
83+
84+
static Map<String, Object> urlCache = new Hashtable<String, Object>();
85+
86+
private BufferedInputStream getCachedStream(URL url) {
87+
Object data = urlCache.get(url.toString());
88+
return (data == null ? null : Rdr.toBIS(data));
89+
}
90+
91+
private void setCachedStream(URL url) {
92+
Object data = url._streamData;
93+
if (data != null)
94+
urlCache.put(url.toString(), data);
7595
}
7696

7797
/**
@@ -96,6 +116,7 @@ public static BufferedInputStream getAttachedStreamData(URL url, boolean andDele
96116
}
97117

98118
public static BufferedInputStream attachStreamData(URL url, Object o) {
119+
99120
/**
100121
* @j2sNative
101122
*

0 commit comments

Comments
 (0)