55import java .io .InputStream ;
66import java .net .HttpURLConnection ;
77import java .net .URL ;
8- import java .net .URLConnection ;
98import java .util .Hashtable ;
109import java .util .Map ;
1110
@@ -20,11 +19,14 @@ public class AjaxURLConnection extends HttpURLConnection {
2019
2120 protected AjaxURLConnection (URL url ) {
2221 super (url );
22+ ajax = /** @j2sNative url.ajax || */ null ;
2323 }
2424
2525 byte [] bytesOut ;
2626 String postOut = "" ;
2727
28+ private Object ajax ;
29+
2830 /**
2931 *
3032 * doAjax() is where the synchronous call to AJAX is to happen. or at least
@@ -48,7 +50,14 @@ protected AjaxURLConnection(URL url) {
4850 @ SuppressWarnings ("null" )
4951 private Object doAjax (boolean isBinary ) {
5052 J2SObjectInterface J2S = /** @j2sNative self.J2S || */ null ;
51- Object info = (/** @j2sNative {isBinary: isBinary } || */ null );
53+ Object info = null ;
54+ /** @j2sNative
55+ *
56+ * info = this.ajax || {};
57+ * if (!info.dataType) {
58+ * info.isBinary = !!isBinary;
59+ * }
60+ */
5261 Object result = J2S .doAjax (url .toString (), postOut , bytesOut , info );
5362 boolean isEmpty = false ;
5463 // the problem is that jsmol.php is still returning crlf even if output is 0 bytes
@@ -91,7 +100,7 @@ private InputStream getInputStreamAndResponse(URL url, boolean allowNWError) {
91100 BufferedInputStream is = getAttachedStreamData (url , false );
92101 if (is != null || getUseCaches () && (is = getCachedStream (url , allowNWError )) != null )
93102 return is ;
94- is = attachStreamData (url , doAjax (true ));
103+ is = attachStreamData (url , doAjax (ajax == null ));
95104 if (getUseCaches () && is != null )
96105 setCachedStream (url );
97106 isNetworkError (is );
@@ -104,17 +113,35 @@ private BufferedInputStream getCachedStream(URL url, boolean allowNWError) {
104113 Object data = urlCache .get (url .toString ());
105114 if (data == null )
106115 return null ;
107- BufferedInputStream bis = Rdr .toBIS (data );
116+ boolean isAjax = /** @j2sNative url.ajax || */ false ;
117+ BufferedInputStream bis = getBIS (data , isAjax );
108118 return (allowNWError || !isNetworkError (bis ) ? bis : null );
109119 }
110120
121+ private static BufferedInputStream getBIS (Object data , boolean isAjax ) {
122+ if (data == null )
123+ return null ;
124+ if (!isAjax )
125+ return Rdr .toBIS (data );
126+ BufferedInputStream bis = Rdr .toBIS ("" );
127+ /**
128+ * @j2sNative
129+ *
130+ * bis._ajaxData = data;
131+ */
132+ return bis ;
133+ }
134+
111135 private void setCachedStream (URL url ) {
112136 Object data = url ._streamData ;
113137 if (data != null )
114138 urlCache .put (url .toString (), data );
115139 }
116140
141+ @ SuppressWarnings ("unused" )
117142 private boolean isNetworkError (BufferedInputStream is ) {
143+ if (/** @j2sNative is._ajaxData || */ false )
144+ return false ;
118145 is .mark (15 );
119146 byte [] bytes = new byte [13 ];
120147 try {
@@ -138,29 +165,36 @@ private boolean isNetworkError(BufferedInputStream is) {
138165 * the first time in Java is usually just to see if it exists.
139166 *
140167 * @param url
141- * @return String, SB, or byte[]
168+ * @return String, SB, or byte[], or JSON dat
142169 */
170+ @ SuppressWarnings ("unused" )
143171 public static BufferedInputStream getAttachedStreamData (URL url , boolean andDelete ) {
144172
145173 Object data = null ;
174+ boolean isAjax = false ;
146175 /**
147176 * @j2sNative
148- *
149177 * data = url._streamData;
150178 * if (andDelete) url._streamData = null;
179+ * isAjax = (data && url.ajax && url.ajax.dataType == "json")
151180 */
152- return (data == null ? null : Rdr . toBIS ( data ) );
181+ return getBIS (data , isAjax );
153182 }
154183
184+ /**
185+ *
186+ * @param url
187+ * @param o
188+ * @return InputStream or possibly a wrapper for an empty string, but also with JSON data.
189+ */
155190 public static BufferedInputStream attachStreamData (URL url , Object o ) {
156-
157- /**
158- * @j2sNative
159- *
160- * url._streamData = o;
161- */
162-
163- return (o == null ? null : Rdr .toBIS (o ));
191+ /**
192+ * @j2sNative
193+ *
194+ * url._streamData = o;
195+ */
196+
197+ return getBIS (o , /** @j2sNative url.ajax || */ false );
164198 }
165199
166200 /**
0 commit comments