3 import java.io.BufferedInputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
7 import java.net.URLConnection;
9 import javajs.api.ResettableStream;
10 import javajs.api.js.J2SObjectInterface;
14 * A method to allow a JavaScript Ajax
17 public class AjaxURLConnection extends URLConnection {
19 protected AjaxURLConnection(URL url) {
28 * doAjax() is where the synchronous call to AJAX is to happen. or at least
29 * where we wait for the asynchronous call to return. This method should fill
30 * the dataIn field with either a string or byte array, or null if you want to
33 * url, bytesOut, and postOut are all available for use
35 * the method is "private", but in JavaScript that can still be overloaded.
36 * Just set something to org.jmol.awtjs.JmolURLConnection.prototype.doAjax
38 * @return file data as a javajs.util.SB or byte[] depending upon the file
43 @SuppressWarnings("null")
44 private Object doAjax(boolean isBinary) {
45 J2SObjectInterface jmol = null;
54 return jmol._doAjax(url, postOut, bytesOut, isBinary);
58 public void connect() throws IOException {
59 // not expected to be used.
62 public void outputBytes(byte[] bytes) {
63 // type = "application/octet-stream;";
67 public void outputString(String post) {
69 // type = "application/x-www-form-urlencoded";
73 public InputStream getInputStream() {
74 BufferedInputStream bis = getAttachedStreamData(url);
77 Object o = doAjax(true);
79 AU.isAB(o) ? Rdr.getBIS((byte[]) o)
80 : o instanceof SB ? Rdr.getBIS(Rdr.getBytesFromSB((SB) o))
81 : o instanceof String ? Rdr.getBIS(((String) o).getBytes())
85 @SuppressWarnings("unused")
87 * J2S will attach a BufferedInputStream to any URL that is
88 * retrieved using a ClassLoader. This improves performance by
89 * not going back to the server every time a second time, since
90 * the first time in Java is usually just to see if it exists.
92 * This stream can be re-used, but it has to be reset. Java for some
93 * reason does not allow a BufferedInputStream to fully reset its
94 * inner streams. We enable that by force-casting the stream as a
95 * javax.io stream and then applying resetStream() to that.
101 public static BufferedInputStream getAttachedStreamData(URL url) {
102 BufferedInputStream bis = null;
106 * bis = url._streamData;
111 ((ResettableStream) (Object) bis).resetStream();
116 * @return javajs.util.SB or byte[], depending upon the file type
118 public Object getContents() {
119 return doAjax(false);