@@ -50,7 +50,19 @@ private Object doAjax(boolean isBinary) {
5050 J2SObjectInterface J2S = /** @j2sNative self.J2S || */ null ;
5151 Object info = (/** @j2sNative {isBinary: isBinary } || */ null );
5252 Object result = J2S .doAjax (url .toString (), postOut , bytesOut , info );
53- responseCode = /** @j2sNative info.xhr.status || */ 0 ;
53+ boolean isEmpty = false ;
54+ // the problem is that jsmol.php is still returning crlf even if output is 0 bytes
55+ // and it is not passing through the not-found state, just 200
56+ /**
57+ * @j2sNative
58+ *
59+ * isEmpty = (!result || result.length == 2 && result[0] == 13 && result[1] == 10);
60+ * if (isEmpty)
61+ * result = new Int8Array;
62+ *
63+ *
64+ */
65+ responseCode = isEmpty ? HTTP_NOT_FOUND : /** @j2sNative info.xhr.status || */ 0 ;
5466 return result ;
5567 }
5668
@@ -71,21 +83,29 @@ public void outputString(String post) {
7183
7284 @ Override
7385 public InputStream getInputStream () {
74- responseCode = 200 ;
86+ responseCode = -1 ;
87+ return getInputStreamAndResponse (url , false );
88+ }
89+
90+ private InputStream getInputStreamAndResponse (URL url , boolean allowNWError ) {
7591 BufferedInputStream is = getAttachedStreamData (url , false );
76- if (is != null || getUseCaches () && (is = getCachedStream (url )) != null )
92+ if (is != null || getUseCaches () && (is = getCachedStream (url , allowNWError )) != null )
7793 return is ;
7894 is = attachStreamData (url , doAjax (true ));
7995 if (getUseCaches () && is != null )
8096 setCachedStream (url );
97+ isNetworkError (is );
8198 return is ;
8299 }
83100
84101 static Map <String , Object > urlCache = new Hashtable <String , Object >();
85102
86- private BufferedInputStream getCachedStream (URL url ) {
103+ private BufferedInputStream getCachedStream (URL url , boolean allowNWError ) {
87104 Object data = urlCache .get (url .toString ());
88- return (data == null ? null : Rdr .toBIS (data ));
105+ if (data == null )
106+ return null ;
107+ BufferedInputStream bis = Rdr .toBIS (data );
108+ return (allowNWError || !isNetworkError (bis ) ? bis : null );
89109 }
90110
91111 private void setCachedStream (URL url ) {
@@ -94,6 +114,23 @@ private void setCachedStream(URL url) {
94114 urlCache .put (url .toString (), data );
95115 }
96116
117+ private boolean isNetworkError (BufferedInputStream is ) {
118+ is .mark (15 );
119+ byte [] bytes = new byte [13 ];
120+ try {
121+ is .read (bytes );
122+ is .reset ();
123+ for (int i = NETWORK_ERROR .length ; --i >= 0 ;)
124+ if (bytes [i ] != NETWORK_ERROR [i ])
125+ return false ;
126+ } catch (IOException e ) {
127+ }
128+ responseCode = HTTP_NOT_FOUND ;
129+ return true ;
130+ }
131+
132+ final private static int [] NETWORK_ERROR = new int [] { 78 , 101 , 116 , 119 , 111 , 114 , 107 , 69 , 114 , 114 , 111 , 114 };
133+
97134 /**
98135 * J2S will attach the data (String, SB, or byte[]) to any URL that is
99136 * retrieved using a ClassLoader. This improves performance by
@@ -133,39 +170,23 @@ public Object getContents() {
133170 return doAjax (false );
134171 }
135172
136- @ Override
137- public int getResponseCode () throws IOException {
138- /*
139- * We have the response code already
140- */
141- if (responseCode != -1 ) {
142- return responseCode ;
143- }
144-
145- /*
146- * Ensure that we have connected to the server. Record
147- * exception as we need to re-throw it if there isn't
148- * a status line.
149- */
150- Exception exc = null ;
151- try {
152- BufferedInputStream is = (BufferedInputStream ) getInputStream ();
153- if (responseCode != HTTP_OK )
154- return responseCode ;
155- if (is .available () > 40 )
156- return responseCode = HTTP_OK ;
157- is .mark (15 );
158- byte [] bytes = new byte [13 ];
159- is .read (bytes );
160- is .reset ();
161- String s = new String (bytes );
162- if (s .startsWith ("Network Error" ))
163- return responseCode = HTTP_NOT_FOUND ;
164- } catch (Exception e ) {
165- exc = e ;
166- }
167- return responseCode = HTTP_OK ;
168- }
173+ @ Override
174+ public int getResponseCode () throws IOException {
175+ /*
176+ * Check to see if have the response code already
177+ */
178+ if (responseCode == -1 ) {
179+ /*
180+ * Ensure that we have connected to the server. Record exception as we need to
181+ * re-throw it if there isn't a status line.
182+ */
183+ try {
184+ getInputStreamAndResponse (url , true );
185+ } catch (Exception e ) {
186+ }
187+ }
188+ return responseCode ;
189+ }
169190@ Override
170191public void disconnect () {
171192 // TODO Auto-generated method stub
0 commit comments