JAL-3032 adds Java 8 functionality (2/2)
[jalview.git] / src2 / javajs / util / AjaxURLConnection.java
1 package javajs.util;
2
3 import java.io.BufferedInputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.net.URL;
7 import java.net.URLConnection;
8
9 import javajs.api.js.J2SObjectInterface;
10
11 /**
12  * 
13  * A method to allow a JavaScript Ajax 
14  * 
15  */
16 public class AjaxURLConnection extends URLConnection {
17
18   protected AjaxURLConnection(URL url) {
19     super(url);
20   }
21
22   byte[] bytesOut;
23   String postOut = "";
24
25   /**
26    * 
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
30    * throw an error.
31    * 
32    * url, bytesOut, and postOut are all available for use
33    * 
34    * the method is "private", but in JavaScript that can still be overloaded.
35    * Just set something to org.jmol.awtjs.JmolURLConnection.prototype.doAjax
36    * 
37    * 
38    * @param isBinary 
39    * 
40    * @return file data as a javajs.util.SB or byte[] depending upon the file
41    *         type.
42    * 
43    * 
44    */
45   @SuppressWarnings("null")
46   private Object doAjax(boolean isBinary) {
47     J2SObjectInterface j2s = null;
48     /**
49      * @j2sNative
50      * 
51      *            j2s = J2S;
52      * 
53      */
54     {
55     }
56     return j2s._doAjax(url, postOut, bytesOut, isBinary);
57   }
58
59   @Override
60   public void connect() throws IOException {
61     // not expected to be used. 
62   }
63
64   public void outputBytes(byte[] bytes) {
65     //      type = "application/octet-stream;";
66     bytesOut = bytes;
67   }
68
69   public void outputString(String post) {
70     postOut = post;
71     //     type = "application/x-www-form-urlencoded";
72   }
73
74         @Override
75         public InputStream getInputStream() {
76                 BufferedInputStream is = getAttachedStreamData(url, false);
77                 return (is == null ? attachStreamData(url, doAjax(true)) : is);
78         }
79
80         /**
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. 
85          * 
86          * @param url
87          * @return String, SB, or byte[]
88          */
89         public static BufferedInputStream getAttachedStreamData(URL url, boolean andDelete) {
90         
91                 Object data = null;
92                 /**
93                  * @j2sNative
94                  * 
95                  *       data = url._streamData;
96                  *       if (andDelete) url._streamData = null;
97                  */
98                 {
99                 }
100                 return (data == null ? null : Rdr.toBIS(data));
101         }
102
103    public static BufferedInputStream attachStreamData(URL url, Object o) {
104            /**
105             * @j2sNative
106             * 
107             *   url._streamData = o;
108             */
109            
110             return (o == null ? null : Rdr.toBIS(o));
111   }
112
113   /**
114    * @return javajs.util.SB or byte[], depending upon the file type
115    */
116   public Object getContents() {
117     return doAjax(false);
118   }
119
120 }