X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjavajs%2Futil%2FAjaxURLConnection.java;h=0b038c337204441798bb09befa0b65c7a2a5008a;hb=5e556aaaedbcf56063c72677364c7fe2b961230d;hp=cef98c8511feb5ddb2bb3d698a12da2d5ac5c3bb;hpb=47d72e9b5fe23ec803cc7f8da284ddca72493745;p=jalview.git diff --git a/src/javajs/util/AjaxURLConnection.java b/src/javajs/util/AjaxURLConnection.java index cef98c8..0b038c3 100644 --- a/src/javajs/util/AjaxURLConnection.java +++ b/src/javajs/util/AjaxURLConnection.java @@ -6,7 +6,6 @@ import java.io.InputStream; import java.net.URL; import java.net.URLConnection; -import javajs.api.ResettableStream; import javajs.api.js.J2SObjectInterface; /** @@ -35,6 +34,9 @@ public class AjaxURLConnection extends URLConnection { * the method is "private", but in JavaScript that can still be overloaded. * Just set something to org.jmol.awtjs.JmolURLConnection.prototype.doAjax * + * + * @param isBinary + * * @return file data as a javajs.util.SB or byte[] depending upon the file * type. * @@ -42,16 +44,16 @@ public class AjaxURLConnection extends URLConnection { */ @SuppressWarnings("null") private Object doAjax(boolean isBinary) { - J2SObjectInterface jmol = null; + J2SObjectInterface j2s = null; /** * @j2sNative * - * jmol = J2S || Jmol; + * j2s = J2S; * */ { } - return jmol._doAjax(url, postOut, bytesOut, isBinary); + return j2s._doAjax(url, postOut, bytesOut, isBinary); } @Override @@ -71,48 +73,44 @@ public class AjaxURLConnection extends URLConnection { @Override public InputStream getInputStream() { - BufferedInputStream bis = getAttachedStreamData(url); - if (bis != null) - return bis; - Object o = doAjax(true); - return ( - AU.isAB(o) ? Rdr.getBIS((byte[]) o) - : o instanceof SB ? Rdr.getBIS(Rdr.getBytesFromSB((SB) o)) - : o instanceof String ? Rdr.getBIS(((String) o).getBytes()) - : bis - ); + BufferedInputStream is = getAttachedStreamData(url, false); + return (is == null ? attachStreamData(url, doAjax(true)) : is); } - @SuppressWarnings("unused") + /** - * J2S will attach a BufferedInputStream to any URL that is + * J2S will attach the data (String, SB, or byte[]) to any URL that is * retrieved using a ClassLoader. This improves performance by * not going back to the server every time a second time, since * the first time in Java is usually just to see if it exists. * - * This stream can be re-used, but it has to be reset. Java for some - * reason does not allow a BufferedInputStream to fully reset its - * inner streams. We enable that by force-casting the stream as a - * javax.io stream and then applying resetStream() to that. - * - * * @param url - * @return + * @return String, SB, or byte[] */ - public static BufferedInputStream getAttachedStreamData(URL url) { - BufferedInputStream bis = null; + public static BufferedInputStream getAttachedStreamData(URL url, boolean andDelete) { + + Object data = null; /** * @j2sNative * - * bis = url._streamData; + * data = url._streamData; + * if (andDelete) url._streamData = null; */ { } - if (bis != null) - ((ResettableStream) (Object) bis).resetStream(); - return bis; + return (data == null ? null : Rdr.toBIS(data)); } - /** + public static BufferedInputStream attachStreamData(URL url, Object o) { + /** + * @j2sNative + * + * url._streamData = o; + */ + + return (o == null ? null : Rdr.toBIS(o)); + } + + /** * @return javajs.util.SB or byte[], depending upon the file type */ public Object getContents() {