1313import java .util .List ;
1414import java .util .Map ;
1515import java .util .Map .Entry ;
16+ import java .util .function .Function ;
1617
1718import javajs .api .js .J2SObjectInterface ;
1819import 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}
0 commit comments