3 import java.io.BufferedInputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
7 import java.net.URLConnection;
9 import javajs.api.js.J2SObjectInterface;
13 * A method to allow a JavaScript Ajax
16 public class AjaxURLConnection extends URLConnection {
18 protected AjaxURLConnection(URL url) {
27 * doAjax() is where the synchronous call to AJAX is to happen. or at least
28 * where we wait for the asynchronous call to return. This method should fill
29 * the dataIn field with either a string or byte array, or null if you want to
32 * url, bytesOut, and postOut are all available for use
34 * the method is "private", but in JavaScript that can still be overloaded.
35 * Just set something to org.jmol.awtjs.JmolURLConnection.prototype.doAjax
40 * @return file data as a javajs.util.SB or byte[] depending upon the file
45 @SuppressWarnings("null")
46 private Object doAjax(boolean isBinary) {
47 J2SObjectInterface j2s = null;
56 return j2s._doAjax(url, postOut, bytesOut, isBinary);
60 public void connect() throws IOException {
61 // not expected to be used.
64 public void outputBytes(byte[] bytes) {
65 // type = "application/octet-stream;";
69 public void outputString(String post) {
71 // type = "application/x-www-form-urlencoded";
75 public InputStream getInputStream() {
76 BufferedInputStream is = getAttachedStreamData(url, false);
77 return (is == null ? attachStreamData(url, doAjax(true)) : is);
81 * J2S will attach the data (String, SB, or byte[]) to any URL that is
82 * retrieved using a ClassLoader. This improves performance by
83 * not going back to the server every time a second time, since
84 * the first time in Java is usually just to see if it exists.
87 * @return String, SB, or byte[]
89 public static BufferedInputStream getAttachedStreamData(URL url, boolean andDelete) {
95 * data = url._streamData;
96 * if (andDelete) url._streamData = null;
100 return (data == null ? null : Rdr.toBIS(data));
103 public static BufferedInputStream attachStreamData(URL url, Object o) {
107 * url._streamData = o;
110 return (o == null ? null : Rdr.toBIS(o));
114 * @return javajs.util.SB or byte[], depending upon the file type
116 public Object getContents() {
117 return doAjax(false);