@@ -196,61 +196,65 @@ private Object doAjax(boolean isBinary, Function<Object, Void> whenDone) {
196196
197197 Object result ;
198198 String myURL = url .toString ();
199- boolean isEmpty = false ;
200199 if (myURL .startsWith ("file:/TEMP/" )) {
201-
202200 result = jsutil .getCachedBytes (myURL );
203- isEmpty = (result == null );
201+ boolean isEmpty = (result == null );
204202 if (whenDone != null ) {
205203 whenDone .apply (isEmpty ? null : result );
206204 return null ;
207205 }
208206 responseCode = (isEmpty ? HTTP_NOT_FOUND : HTTP_ACCEPTED );
209- } else {
210- if (myURL .startsWith ("file:" )) {
211- String j2s = /** @j2sNative Clazz._Loader.getJ2SLibBase() || */
212- null ;
213- if (myURL .startsWith ("file:/./" )) {
214- // file:/./xxxx
215- myURL = j2s + myURL .substring (7 );
216- } else if (myURL .startsWith ("file:/" + j2s )) {
217- // from classLoader
218- myURL = myURL .substring (6 );
207+ return result ;
208+ }
209+ if (myURL .startsWith ("file:" )) {
210+ String j2s = /** @j2sNative Clazz._Loader.getJ2SLibBase() || */
211+ null ;
212+ if (myURL .startsWith ("file:/./" )) {
213+ // file:/./xxxx
214+ myURL = j2s + myURL .substring (7 );
215+ } else if (myURL .startsWith ("file:/" + j2s )) {
216+ // from classLoader
217+ myURL = myURL .substring (6 );
218+ } else {
219+ String base = getFileDocumentDir ();
220+ if (base != null && myURL .indexOf (base ) == 0 ) {
221+ myURL = myURL .substring (base .length ());
219222 } else {
220- String base = getFileDocumentDir ();
221- if (base != null && myURL .indexOf (base ) == 0 ) {
222- myURL = myURL .substring (base .length ());
223- } else {
224- URL path = jsutil .getCodeBase ();
225- if (path != null ) {
226- j2s = path .toString ();
227- if (myURL .indexOf (j2s ) >= 0 )
228- myURL = path + myURL .split (j2s )[1 ];
229- else
230- myURL = path + myURL .substring (5 );
231- }
223+ URL path = jsutil .getCodeBase ();
224+ if (path != null ) {
225+ j2s = path .toString ();
226+ if (myURL .indexOf (j2s ) >= 0 )
227+ myURL = path + myURL .split (j2s )[1 ];
228+ else
229+ myURL = path + myURL .substring (5 );
232230 }
233231 }
234232 }
235- result = J2S .doAjax (myURL , postOut , bytesOut , info );
236- if (whenDone != null )
237- return null ;
238- // the problem is that jsmol.php is still returning crlf even if output is 0
239- // bytes
240- // and it is not passing through the not-found state, just 200
241- /**
242- * @j2sNative
243- *
244- * isEmpty = (!result || result.length == 2 && result[0] == 13 &&
245- * result[1] == 10); if (isEmpty) result = new Int8Array;
246- */
247-
248- responseCode = isEmpty ? HTTP_NOT_FOUND : /** @j2sNative info.xhr.status || */
249- 0 ;
250233 }
234+ result = J2S .doAjax (myURL , postOut , bytesOut , info );
235+ if (whenDone != null )
236+ return null ;
237+ setJQueryResponseCodeFromJQuery (result );
251238 return result ;
252239 }
253240
241+ private void setJQueryResponseCodeFromJQuery (Object result ) {
242+ // the problem is that jsmol.php is still returning crlf even if output is 0
243+ // bytes
244+ // and it is not passing through the not-found state, just 200
245+ Object info = this .info ;
246+ boolean isEmpty = false ;
247+ /**
248+ * @j2sNative
249+ *
250+ * isEmpty = (!result || result.length == 2 && result[0] == 13 &&
251+ * result[1] == 10); if (isEmpty) result = new Int8Array;
252+ */
253+
254+ responseCode = isEmpty ? HTTP_NOT_FOUND : /** @j2sNative info.xhr.status || */
255+ 0 ;
256+ }
257+
254258 private String getFileDocumentDir () {
255259 String base = jsutil .getDocumentBase ().getPath ();
256260 int pt = base .lastIndexOf ("/" );
@@ -386,7 +390,7 @@ public OutputStream getOutputStream() throws IOException {
386390 @ SuppressWarnings ({ "null" , "unused" })
387391 @ Override
388392 public InputStream getInputStream () throws FileNotFoundException {
389- InputStream is = /** @j2sNative this.is || */ null ;
393+ BufferedInputStream is = /** @j2sNative this.is || */ null ;
390394 if (is != null )
391395 return is ;
392396 responseCode = -1 ;
@@ -404,8 +408,7 @@ public void getBytesAsync(Function<byte[], Void> whenDone) {
404408 public Void apply (InputStream is ) {
405409 try {
406410 if (is != null ) {
407- byte [] bytes = /** @j2sNative is.readAllBytes$() || */ null ;
408- whenDone .apply (bytes );
411+ whenDone .apply (/** @j2sNative is.readAllBytes$() || */ null );
409412 return null ;
410413 }
411414 } catch (Exception e ) {
@@ -420,8 +423,9 @@ public Void apply(InputStream is) {
420423
421424 @ SuppressWarnings ({ "null" , "unused" })
422425 private void getInputStreamAsync (Function <InputStream , Void > whenDone ) {
423- InputStream is = /** @j2sNative is = this.is || */ null ;
426+ BufferedInputStream is = /** @j2sNative is = this.is || */ null ;
424427 if (is != null ) {
428+ isNetworkError (is );
425429 whenDone .apply (is );
426430 return ;
427431 }
@@ -438,12 +442,13 @@ private void getInputStreamAndResponseAsync(Function<InputStream, Void> whenDone
438442 doAjax (true , new Function <Object , Void >() {
439443
440444 @ Override
441- public Void apply (Object data ) {
442- if (data instanceof String ) {
445+ public Void apply (Object response ) {
446+ if (response instanceof String ) {
443447 whenDone .apply (null );
444448 return null ;
445449 }
446- BufferedInputStream is = attachStreamData (url , data );
450+ setJQueryResponseCodeFromJQuery (response );
451+ BufferedInputStream is = attachStreamData (url , response );
447452 if (doCache () && is != null ) {
448453 isNetworkError (is );
449454 setCachedStream ();
@@ -457,7 +462,7 @@ public Void apply(Object data) {
457462 });
458463 }
459464
460- private InputStream getInputStreamAndResponse (boolean allowNWError ) {
465+ private BufferedInputStream getInputStreamAndResponse (boolean allowNWError ) {
461466 BufferedInputStream is = getAttachedStreamData (url , false );
462467 if (is != null || doCache () && (is = getCachedStream (allowNWError )) != null ) {
463468 return is ;
@@ -468,8 +473,7 @@ private InputStream getInputStreamAndResponse(boolean allowNWError) {
468473 setCachedStream ();
469474 return is ;
470475 }
471- if (!isNetworkError (is )) {
472- }
476+ isNetworkError (is );
473477 return is ;
474478 }
475479
@@ -548,6 +552,9 @@ private String getCacheKey() {
548552 @ SuppressWarnings ("unused" )
549553 private boolean isNetworkError (BufferedInputStream is ) {
550554 if (is != null ) {
555+ if (responseCode > 0 ) {
556+ return (responseCode >= 400 );
557+ }
551558 responseCode = HTTP_OK ;
552559 if (/** @j2sNative is._jsonData || */
553560 false )
0 commit comments