Skip to content

Commit b46d77b

Browse files
committed
Enabling asynchronous file loading
1 parent 7fc486e commit b46d77b

File tree

11 files changed

+166
-13
lines changed

11 files changed

+166
-13
lines changed

sources/net.sf.j2s.java.core/src/java/net/HttpURLConnection.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ abstract public class HttpURLConnection extends URLConnection {
112112
* @return the key for the {@code n}<sup>th</sup> header field,
113113
* or {@code null} if the key does not exist.
114114
*/
115-
public String getHeaderFieldKey (int n) {
115+
@Override
116+
public String getHeaderFieldKey (int n) {
116117
return null;
117118
}
118119

@@ -264,7 +265,8 @@ public void setChunkedStreamingMode (int chunklen) {
264265
* or {@code null} if the value does not exist.
265266
* @see java.net.HttpURLConnection#getHeaderFieldKey(int)
266267
*/
267-
public String getHeaderField(int n) {
268+
@Override
269+
public String getHeaderField(int n) {
268270
return null;
269271
}
270272

@@ -548,7 +550,8 @@ public String getResponseMessage() throws IOException {
548550
return responseMessage;
549551
}
550552

551-
@SuppressWarnings("deprecation")
553+
@Override
554+
@SuppressWarnings("deprecation")
552555
public long getHeaderFieldDate(String name, long Default) {
553556
// String dateString = getHeaderField(name);
554557
// try {

sources/net.sf.j2s.java.core/src/java/net/URL.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.io.IOException;
3535
import java.io.InputStream;
3636
import java.util.Hashtable;
37+
import java.util.function.Function;
3738

3839
import javajs.util.AjaxURLConnection;
3940
import javajs.util.AjaxURLStreamHandlerFactory;
@@ -1102,6 +1103,15 @@ public final InputStream openStream() throws IOException {
11021103
return openConnection().getInputStream();
11031104
}
11041105

1106+
1107+
public void getBytesAsync(Function<byte[], Void> whenDone) {
1108+
try {
1109+
openConnection().getBytesAsync(whenDone);
1110+
} catch (IOException e) {
1111+
whenDone.apply(null);
1112+
}
1113+
}
1114+
11051115
/**
11061116
* same as openStream(), except here we might get string buffer data, not
11071117
* bytes, since we will call J2S.doAjax(false), not J2S.doAjax(true), which

sources/net.sf.j2s.java.core/src/java/net/URLConnection.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.util.Hashtable;
3838
import java.util.List;
3939
import java.util.Map;
40+
import java.util.function.Function;
4041

4142
import sun.net.www.MessageHeader;
4243
import sun.security.util.SecurityConstants;
@@ -1689,4 +1690,6 @@ public String toString() {
16891690
return this.getClass().getName() + ":" + url;
16901691
}
16911692

1693+
public abstract void getBytesAsync(Function<byte[], Void> whenDone);
1694+
16921695
}

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

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.util.List;
1414
import java.util.Map;
1515
import java.util.Map.Entry;
16+
import java.util.function.Function;
1617

1718
import javajs.api.js.J2SObjectInterface;
1819
import swingjs.JSUtil;
@@ -61,6 +62,7 @@ public String getHeaderField(String name) {
6162
return /** @j2sNative this.info && this.info.xhr && this.info.xhr.getResponseHeader(name) || */null;
6263
}
6364

65+
6466
/**
6567
*
6668
* doAjax() is where the synchronous call to AJAX is to happen. or at least
@@ -81,7 +83,7 @@ public String getHeaderField(String name) {
8183
*
8284
*/
8385
@SuppressWarnings("null")
84-
private Object doAjax(boolean isBinary) {
86+
private Object doAjax(boolean isBinary, Function<Object, Void> whenDone) {
8587
getBytesOut();
8688
J2SObjectInterface J2S = /** @j2sNative self.J2S || */
8789
null;
@@ -91,6 +93,9 @@ private Object doAjax(boolean isBinary) {
9193
*
9294
* info = this.ajax || {}; if (!info.dataType) { info.isBinary =
9395
* !!isBinary; }
96+
*
97+
* whenDone && (info.fWhenDone = function(data){whenDone.apply$O(data)});
98+
*
9499
*/
95100
this.info = info;
96101
Map<String, List<String>> map = getRequestProperties();
@@ -142,12 +147,18 @@ private Object doAjax(boolean isBinary) {
142147
if (myURL.startsWith("file:/TEMP/")) {
143148
result = JSUtil.getCachedFileData(myURL, true);
144149
isEmpty = (result == null);
150+
if (whenDone != null) {
151+
whenDone.apply(isEmpty ? null : result);
152+
return null;
153+
}
145154
responseCode = (isEmpty ? HTTP_NOT_FOUND : HTTP_ACCEPTED);
146155
} else {
147156
if (myURL.startsWith("file:")) {
148157
myURL = JSUtil.J2S.getResourcePath("", true) + myURL.substring(5);
149158
}
150159
result = J2S.doAjax(myURL, postOut, bytesOut, info);
160+
if (whenDone != null)
161+
return null;
151162
// the problem is that jsmol.php is still returning crlf even if output is 0
152163
// bytes
153164
// and it is not passing through the not-found state, just 200
@@ -157,6 +168,7 @@ private Object doAjax(boolean isBinary) {
157168
* isEmpty = (!result || result.length == 2 && result[0] == 13 &&
158169
* result[1] == 10); if (isEmpty) result = new Int8Array;
159170
*/
171+
160172
responseCode = isEmpty ? HTTP_NOT_FOUND : /** @j2sNative info.xhr.status || */
161173
0;
162174
}
@@ -201,14 +213,74 @@ public InputStream getInputStream() throws FileNotFoundException {
201213
throw new FileNotFoundException("opening " + url);
202214
return is;
203215
}
216+
217+
218+
@Override
219+
public void getBytesAsync(Function<byte[], Void> whenDone) {
220+
getInputStreamAsync(new Function<InputStream, Void>() {
221+
222+
@Override
223+
public Void apply(InputStream is) {
224+
try {
225+
if (is != null) {
226+
whenDone.apply(is.readAllBytes());
227+
return null;
228+
}
229+
} catch (IOException e) {
230+
}
231+
whenDone.apply(null);
232+
return null;
233+
}
234+
235+
});
236+
237+
}
238+
239+
private void getInputStreamAsync(Function<InputStream, Void> whenDone) {
240+
if (is != null) {
241+
whenDone.apply(is);
242+
return;
243+
}
244+
responseCode = -1;
245+
getInputStreamAndResponseAsync(whenDone);
246+
}
247+
248+
private void getInputStreamAndResponseAsync(Function<InputStream, Void> whenDone) {
249+
BufferedInputStream is = getAttachedStreamData(url, false);
250+
if (is != null || doCache()
251+
&& (is = getCachedStream(false)) != null) {
252+
whenDone.apply(is);
253+
return;
254+
}
255+
doAjax(true, new Function<Object, Void>() {
256+
257+
@Override
258+
public Void apply(Object data) {
259+
if (data instanceof String) {
260+
whenDone.apply(null);
261+
return null;
262+
}
263+
BufferedInputStream is = attachStreamData(url, data);
264+
if (doCache() && is != null) {
265+
isNetworkError(is);
266+
setCachedStream();
267+
} else if (isNetworkError(is)) {
268+
is = null;
269+
}
270+
whenDone.apply(is);
271+
return null;
272+
}
273+
274+
});
275+
}
204276

205277
private InputStream getInputStreamAndResponse(boolean allowNWError) {
206278
BufferedInputStream is = getAttachedStreamData(url, false);
207279
if (is != null || doCache()
208280
&& (is = getCachedStream(allowNWError)) != null) {
209281
return is;
210282
}
211-
is = attachStreamData(url, doAjax(ajax == null));
283+
is = attachStreamData(url, doAjax(ajax == null, null));
212284
if (doCache() && is != null) {
213285
isNetworkError(is);
214286
setCachedStream();
@@ -346,7 +418,7 @@ public static BufferedInputStream attachStreamData(URL url, Object o) {
346418
* @return javajs.util.SB or byte[], depending upon the file type
347419
*/
348420
public Object getContents() {
349-
return doAjax(false);
421+
return doAjax(false, null);
350422
}
351423

352424
@Override
@@ -393,4 +465,5 @@ public int getContentLength() {
393465
public String toString() {
394466
return (url == null ? "[AjaxURLConnection]" : url.toString());
395467
}
468+
396469
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.net.JarURLConnection;
88
import java.net.MalformedURLException;
99
import java.net.URL;
10+
import java.util.function.Function;
1011
import java.util.jar.JarEntry;
1112
import java.util.jar.JarException;
1213
import java.util.jar.JarFile;
@@ -72,4 +73,13 @@ public JarEntry getJarEntry() throws IOException {
7273
return null;
7374
}
7475

76+
@Override
77+
public void getBytesAsync(Function<byte[], Void> whenDone) {
78+
try {
79+
whenDone.apply(getInputStream().readAllBytes());
80+
} catch (IOException e) {
81+
whenDone.apply(null);
82+
}
83+
}
84+
7585
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Locale;
2020
import java.util.Map;
2121
import java.util.Properties;
22+
import java.util.function.Function;
2223
import java.util.zip.ZipEntry;
2324
import java.util.zip.ZipInputStream;
2425

@@ -1042,6 +1043,13 @@ public byte[] readAllBytes(InputStream is) throws IOException {
10421043
return is.readAllBytes();
10431044
}
10441045

1046+
@Override
1047+
public void getURLBytesAsync(URL url, Function<byte[], Void> whenDone) {
1048+
url.getBytesAsync(whenDone);
1049+
}
1050+
1051+
1052+
10451053
@Override
10461054
public long transferTo(InputStream is, OutputStream out) throws IOException {
10471055
return is.transferTo(out);

sources/net.sf.j2s.java.core/src/swingjs/api/JSUtilI.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.net.URL;
99
import java.util.HashMap;
1010
import java.util.Properties;
11+
import java.util.function.Function;
1112
import java.util.zip.ZipEntry;
1213
import java.util.zip.ZipInputStream;
1314

@@ -288,4 +289,6 @@ public interface JSUtilI {
288289

289290
void showStatus(String msg, boolean doFadeOut);
290291

292+
void getURLBytesAsync(URL url, Function<byte[], Void> whenDone);
293+
291294
}

sources/net.sf.j2s.java.core/src/test/Test_.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ class Testx {
77

88
public class Test_ {
99

10+
static boolean isBatch = false;
11+
1012
static public boolean j2sHeadless = true;
1113

1214
private static int i_ = 0;
@@ -104,7 +106,7 @@ public static void main(String[] args) {
104106

105107
int val = new Test_().test3();
106108
assert (val == 13 || val == 4);
107-
109+
isBatch = true;
108110
Test_Anon.main(args);
109111
Test_Appendable.main(args);
110112
Test_Array.main(args);

sources/net.sf.j2s.java.core/src/test/Test_URL.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.List;
2424
import java.util.Map;
2525
import java.util.Map.Entry;
26+
import java.util.function.Function;
2627
import java.util.zip.ZipEntry;
2728
import java.util.zip.ZipInputStream;
2829

@@ -46,6 +47,7 @@ protected static String getRequestMimeType() {
4647
protected static String getResponseMimeType() {
4748
return "application/json";
4849
}
50+
@SuppressWarnings("unused")
4951
public static void main(String[] args) {
5052

5153

@@ -77,9 +79,7 @@ public static void main(String[] args) {
7779
URL readme = Test_URL.class.getClassLoader().getResource("file://c:/test/t.htm");
7880

7981
System.out.println(readme);
80-
8182

82-
8383
try (InputStream in = new URL("https://stolaf.edu/people/hansonr").openStream()) {
8484

8585
// Java or JavaScript:
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package test;
2+
3+
import java.net.MalformedURLException;
4+
import java.net.URL;
5+
import java.util.function.Function;
6+
7+
public class Test_URL_Async extends Test_ {
8+
9+
@SuppressWarnings("unused")
10+
public static void main(String[] args) {
11+
if (!isBatch && (/** @j2sNative true || */
12+
false)) {
13+
try {
14+
new URL("https://stolaf.edu/people/hansonr").getBytesAsync(new Function<byte[], Void>() {
15+
16+
@Override
17+
public Void apply(byte[] b) {
18+
System.out.println("async response length " + (b == null ? 0 : b.length));
19+
return null;
20+
}
21+
22+
});
23+
} catch (MalformedURLException e3) {
24+
}
25+
}
26+
System.out.println("Test_URL_Async.main complete");
27+
}
28+
29+
}

0 commit comments

Comments
 (0)