Jmol/SwingJS update. Includes new transpiler and runtime
[jalview.git] / srcjar / javajs / util / OC.java
index 98151d1..a63489c 100644 (file)
@@ -60,6 +60,21 @@ public class OC extends OutputStream implements GenericOutputChannel {
        private byte[] bytes; // preset bytes; output only
   
        public boolean bigEndian = true;
+
+       private boolean isTemp;
+       
+       /**
+        * Setting isTemp=true informs OC that this is a temporary file 
+        * and not to send it to the user as a "download". Instead, the calling
+        * class can use .toByteArray() to retrieve the byte[] result.
+        * 
+        * @param tf
+        */
+       public void setTemp(boolean tf) {
+               isTemp = tf;
+       }
+
+
   
   @Override
   public boolean isBigEndian() {
@@ -70,6 +85,17 @@ public class OC extends OutputStream implements GenericOutputChannel {
        bigEndian = TF;
   }
 
+       /**
+        * Set up an output channel. String or byte data can be added without problem.
+        * 
+        * @param bytePoster a byte poster can take the output byte[] when closing and
+        *                   do something with them
+        * @param fileName   TODO: It is possible that JavaScript will call this with a
+        *                   function name for fileName
+        * @param asWriter   string-based
+        * @param os         the desired target OutputStream - not the calling stream!
+        * @return
+        */
   public OC setParams(BytePoster bytePoster, String fileName,
                                      boolean asWriter, OutputStream os) {
     this.bytePoster = bytePoster;
@@ -301,12 +327,12 @@ public class OC extends OutputStream implements GenericOutputChannel {
       return (sb == null ? null : sb.toString());
     }
     closed = true;
-    J2SObjectInterface jmol = null;
+    J2SObjectInterface J2S = null;
     Object _function = null;
     /**
      * @j2sNative
      * 
-     *            jmol = self.J2S || Jmol; _function = (typeof this.fileName == "function" ?
+     *            J2S = self.J2S || self.Jmol; _function = (typeof this.fileName == "function" ?
      *            this.fileName : null);
      * 
      */
@@ -318,12 +344,15 @@ public class OC extends OutputStream implements GenericOutputChannel {
         return ret;
       }
     }
-    if (jmol != null) {
+    if (J2S != null && !isTemp) {
+       
+      // action here generally will be for the browser to display a download message
+      // temp files will not be sent this way.
       Object data = (sb == null ? toByteArray() : sb.toString());
       if (_function == null)
-        jmol._doAjax(fileName, null, data, sb == null);
+        J2S.doAjax(fileName, null, data, sb == null);
       else
-        jmol._apply(_function, data);
+        J2S.applyFunc(_function, data);
     }
     return null;
   }
@@ -337,7 +366,8 @@ public class OC extends OutputStream implements GenericOutputChannel {
        }
        
   public byte[] toByteArray() {
-    return (bytes != null ? bytes : os instanceof ByteArrayOutputStream ? ((ByteArrayOutputStream)os).toByteArray() : null);
+    return (bytes != null ? bytes : (bytes = os instanceof ByteArrayOutputStream ? ((ByteArrayOutputStream)os).toByteArray() : 
+       sb == null ? null : sb.toBytes(0, sb.length())));
   }
 
   @Override
@@ -359,9 +389,14 @@ public class OC extends OutputStream implements GenericOutputChannel {
     return byteCount + " bytes";
   }
 
+  /**
+   * We have constructed some sort of byte[] that needs to be posted to a remote site.
+   * We don't do that posting here -- we let the bytePoster do it.
+   * 
+   * @return
+   */
   private String postByteArray() {
-    byte[] bytes = (sb == null ? toByteArray() : sb.toString().getBytes());
-    return bytePoster.postByteArray(fileName, bytes);
+    return bytePoster == null ? null : bytePoster.postByteArray(fileName, toByteArray());
   }
 
   public final static String[] urlPrefixes = { "http:", "https:", "sftp:", "ftp:",