Merge branch 'master' of https://source.jalview.org/git/jalviewjs.git
[jalviewjs.git] / src / javajs / util / BinaryDocument.java
index 18c305f..172ed7c 100644 (file)
-/* $RCSfile$\r
- * $Author: egonw $\r
- * $Date: 2006-03-18 15:59:33 -0600 (Sat, 18 Mar 2006) $\r
- * $Revision: 4652 $\r
- *\r
- * Copyright (C) 2003-2005  Miguel, Jmol Development, www.jmol.org\r
- *\r
- * Contact: hansonr@stolaf.edu\r
- *\r
- *  This library is free software; you can redistribute it and/or\r
- *  modify it under the terms of the GNU Lesser General Public\r
- *  License as published by the Free Software Foundation; either\r
- *  version 2.1 of the License, or (at your option) any later version.\r
- *\r
- *  This library is distributed in the hope that it will be useful,\r
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
- *  Lesser General Public License for more details.\r
- *\r
- *  You should have received a copy of the GNU Lesser General Public\r
- *  License along with this library; if not, write to the Free Software\r
- *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.\r
- */\r
-package javajs.util;\r
-\r
-\r
-import java.io.BufferedInputStream;\r
-import java.io.DataInputStream;\r
-import java.util.Map;\r
-\r
-\r
-import javajs.api.GenericBinaryDocument;\r
-import javajs.api.GenericZipTools;\r
-\r
-\r
-//import java.io.RandomAccessFile;\r
-\r
-/* a basic binary file reader (extended by CompoundDocument). \r
- * \r
- * random access file info: \r
- * http://java.sun.com/docs/books/tutorial/essential/io/rafs.html\r
- * \r
- * SHOOT! random access is only for applications, not applets!\r
- * \r
- * Note that YOU are responsible for determining whether a file\r
- * is bigEndian or littleEndian; the default is bigEndian.\r
- * \r
- * JavaScript note: readShort() malfunctioned because (short) (xx << 8) \r
- * isn't the same as (int) (xx << 8); same problem in java.io.DataStream\r
- * \r
- * \r
- */\r
-\r
-public class BinaryDocument extends BC implements GenericBinaryDocument {\r
-\r
-  /**\r
-   * @j2sIgnore\r
-   */\r
-  public BinaryDocument() {  \r
-  }\r
-\r
-\r
-  // called by reflection\r
-  \r
-  protected DataInputStream stream;\r
-  protected boolean isRandom = false;\r
-  public boolean isBigEndian = true;\r
-  protected GenericZipTools jzt;\r
-\r
-  @Override\r
-  public void close() {\r
-    if (stream != null)\r
-      try {\r
-        stream.close();\r
-      } catch (Exception e) {\r
-        // ignore\r
-      }\r
-    if (out != null)\r
-       out.closeChannel();\r
-  }\r
-  \r
-  @Override\r
-  public void setStream(GenericZipTools jzt, BufferedInputStream bis, boolean isBigEndian) {\r
-    if (jzt != null)\r
-      this.jzt = jzt;\r
-    if (bis != null)\r
-      stream = new DataInputStream(bis);\r
-    this.isBigEndian = isBigEndian;\r
-  }\r
-  \r
-  @Override\r
-  public void setStreamData(DataInputStream stream, boolean isBigEndian) {\r
-    if (stream != null)\r
-      this.stream = stream;\r
-    this.isBigEndian = isBigEndian;\r
-  }\r
-  \r
-  public void setRandom(boolean TF) {\r
-    isRandom = TF;\r
-    //CANNOT be random for web \r
-  }\r
-  \r
-  @Override\r
-  public byte readByte() throws Exception {\r
-    nBytes++;\r
-    return ioReadByte();\r
-  }\r
-\r
-  private byte ioReadByte() throws Exception {\r
-    byte b = stream.readByte();\r
-    if (out != null)\r
-      out.writeByteAsInt(b);\r
-    return b;\r
-  }\r
-\r
-  @Override\r
-  public int readByteArray(byte[] b, int off, int len) throws Exception {\r
-    int n = ioRead(b, off, len);\r
-    nBytes += n;\r
-    return n;\r
-  }\r
-\r
-  private int ioRead(byte[] b, int off, int len) throws Exception {\r
-    int m = 0;\r
-    while (len > 0) {\r
-      int n = stream.read(b, off, len);\r
-      m += n;\r
-      if (n > 0 && out != null)\r
-        writeBytes(b, off, n);\r
-      if (n >= len)\r
-        break;\r
-      off += n;\r
-      len -= n;\r
-    }\r
-    return m;\r
-  }\r
-\r
-  public void writeBytes(byte[] b, int off, int n) throws Exception {\r
-    out.write(b, off, n);\r
-  }\r
-\r
-  @Override\r
-  public String readString(int nChar) throws Exception {\r
-    byte[] temp = new byte[nChar];\r
-    int n = readByteArray(temp, 0, nChar);\r
-    return new String(temp, 0, n, "UTF-8");\r
-  }\r
-  \r
-  @Override\r
-  public short readShort() throws Exception {\r
-    nBytes += 2;\r
-    short n = (isBigEndian ? ioReadShort()\r
-        : (short) ((ioReadByte() & 0xff) \r
-                 | (ioReadByte() & 0xff) << 8));\r
-    /**\r
-     * @j2sNative\r
-     *\r
-     * return (n > 0x7FFF ? n - 0x10000 : n);\r
-     */\r
-    {\r
-      return n;\r
-    }\r
-  }\r
-\r
-  private short ioReadShort() throws Exception {\r
-    short b = stream.readShort();\r
-    if (out != null)\r
-      writeShort(b);\r
-    return b;\r
-  }\r
-\r
-\r
-  public void writeShort(short i) throws Exception {\r
-    out.writeByteAsInt(i >> 8);\r
-    out.writeByteAsInt(i);\r
-  }\r
-\r
-  @Override\r
-  public int readIntLE() throws Exception {\r
-    nBytes += 4;\r
-    return readLEInt();\r
-  }\r
-  \r
-  @Override\r
-  public int readInt() throws Exception {\r
-    nBytes += 4;\r
-    return (isBigEndian ? ioReadInt() : readLEInt());\r
-  }\r
-  \r
-  private int ioReadInt() throws Exception {\r
-    int i = stream.readInt();\r
-    if (out != null)\r
-      writeInt(i);\r
-    return i;\r
-  }\r
-\r
-  public void writeInt(int i) throws Exception {\r
-    out.writeByteAsInt(i >> 24);\r
-    out.writeByteAsInt(i >> 16);\r
-    out.writeByteAsInt(i >> 8);\r
-    out.writeByteAsInt(i);\r
-  }\r
-\r
-  @Override\r
-  public int swapBytesI(int n) {\r
-    return (((n >> 24) & 0xff)\r
-        | ((n >> 16) & 0xff) << 8\r
-        | ((n >> 8) & 0xff) << 16 \r
-        | (n & 0xff) << 24);\r
-  }\r
-\r
-  @Override\r
-  public short swapBytesS(short n) {\r
-    return (short) ((((n >> 8) & 0xff)\r
-        | (n & 0xff) << 8));\r
-  }\r
-\r
-  \r
-  @Override\r
-  public int readUnsignedShort() throws Exception {\r
-    nBytes += 2;\r
-    int a = (ioReadByte() & 0xff);\r
-    int b = (ioReadByte() & 0xff);\r
-    return (isBigEndian ? (a << 8) + b : (b << 8) + a);\r
-  }\r
-  \r
-  @Override\r
-  public long readLong() throws Exception {\r
-    nBytes += 8;\r
-    return (isBigEndian ? ioReadLong()\r
-       : ((((long) ioReadByte()) & 0xff)\r
-        | (((long) ioReadByte()) & 0xff) << 8\r
-        | (((long) ioReadByte()) & 0xff) << 16\r
-        | (((long) ioReadByte()) & 0xff) << 24\r
-        | (((long) ioReadByte()) & 0xff) << 32\r
-        | (((long) ioReadByte()) & 0xff) << 40\r
-        | (((long) ioReadByte()) & 0xff) << 48 \r
-        | (((long) ioReadByte()) & 0xff) << 54));\r
-  }\r
-\r
-  private long ioReadLong() throws Exception {\r
-    long b = stream.readLong();\r
-    if (out != null)\r
-      writeLong(b);\r
-    return b;\r
-  }\r
-\r
-  public void writeLong(long b) throws Exception {\r
-    writeInt((int)((b >> 32) & 0xFFFFFFFFl));\r
-    writeInt((int)(b & 0xFFFFFFFFl));\r
-  }\r
-\r
-  private int readLEInt() throws Exception {\r
-    ioRead(t8, 0, 4);\r
-    return bytesToInt(t8, 0, false);\r
-  }\r
-\r
-  byte[] t8 = new byte[8];\r
-  \r
-  @Override\r
-  public float readFloat() throws Exception {\r
-    return intToFloat(readInt());\r
-  }\r
-\r
-  @Override\r
-  public double readDouble() throws Exception {\r
-    /**\r
-     * \r
-     * reading the float equivalent here in JavaScript\r
-     * \r
-     * @j2sNative\r
-     * \r
-     * this.readByteArray(this.t8, 0, 8);\r
-     * return this.bytesToDoubleToFloat(this.t8, 0, this.isBigEndian);\r
-     *  \r
-     */\r
-    {\r
-      nBytes += 8;\r
-      return (isBigEndian ? ioReadDouble() : Double.longBitsToDouble(readLELong()));  \r
-    }\r
-  }\r
-  \r
-  private double ioReadDouble() throws Exception {\r
-    double d = stream.readDouble();\r
-    if (out != null)\r
-      writeLong(Double.doubleToRawLongBits(d));\r
-    return d;\r
-  }\r
-\r
-  private long readLELong() throws Exception {\r
-    return ((((long) ioReadByte()) & 0xff)\r
-          | (((long) ioReadByte()) & 0xff) << 8\r
-          | (((long) ioReadByte()) & 0xff) << 16 \r
-          | (((long) ioReadByte()) & 0xff) << 24\r
-          | (((long) ioReadByte()) & 0xff) << 32\r
-          | (((long) ioReadByte()) & 0xff) << 40\r
-          | (((long) ioReadByte()) & 0xff) << 48\r
-          | (((long) ioReadByte()) & 0xff) << 56);\r
-  }\r
-\r
-  @Override\r
-  public void seek(long offset) {\r
-    // slower, but all that is available using the applet\r
-    try {\r
-      if (offset == nBytes)\r
-        return;\r
-      if (offset < nBytes) {\r
-        stream.reset();\r
-        if (out != null && nBytes != 0)\r
-          out.reset();\r
-        nBytes = 0;\r
-      } else {\r
-        offset -= nBytes;\r
-      }\r
-      if (out == null) {\r
-        stream.skipBytes((int)offset);\r
-      } else {\r
-        readByteArray(new byte[(int)offset], 0, (int) offset);\r
-      }\r
-      nBytes += offset;\r
-    } catch (Exception e) {\r
-      System.out.println(e.toString());\r
-    }\r
-  }\r
-\r
-  long nBytes;\r
-  \r
-  @Override\r
-  public long getPosition() {\r
-    return nBytes;\r
-  }\r
-\r
-  OC out;\r
-  @Override\r
-  public void setOutputChannel(OC out) {\r
-      this.out = out;\r
-  }\r
-\r
-  @Override\r
-  public SB getAllDataFiles(String binaryFileList, String firstFile) {\r
-    return null;\r
-  }\r
-\r
-  @Override\r
-  public void getAllDataMapped(String replace, String string,\r
-                               Map<String, String> fileData) {\r
-  }\r
-\r
-\r
-/*  random access -- application only:\r
- * \r
-    void seekFile(long offset) {\r
-    try {\r
-      file.seek(offset);\r
-    } catch (Exception e) {\r
-      System.out.println(e.getMessage());\r
-    }\r
-  }\r
-*/\r
-}\r
+/* $RCSfile$
+ * $Author: egonw $
+ * $Date: 2006-03-18 15:59:33 -0600 (Sat, 18 Mar 2006) $
+ * $Revision: 4652 $
+ *
+ * Copyright (C) 2003-2005  Miguel, Jmol Development, www.jmol.org
+ *
+ * Contact: hansonr@stolaf.edu
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Lesser General Public
+ *  License as published by the Free Software Foundation; either
+ *  version 2.1 of the License, or (at your option) any later version.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Lesser General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Lesser General Public
+ *  License along with this library; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+package javajs.util;
+
+
+import java.io.BufferedInputStream;
+import java.io.DataInputStream;
+import java.util.Map;
+
+
+import javajs.api.GenericBinaryDocument;
+import javajs.api.GenericZipTools;
+
+
+//import java.io.RandomAccessFile;
+
+/* a basic binary file reader (extended by CompoundDocument). 
+ * 
+ * random access file info: 
+ * http://java.sun.com/docs/books/tutorial/essential/io/rafs.html
+ * 
+ * SHOOT! random access is only for applications, not applets!
+ * 
+ * Note that YOU are responsible for determining whether a file
+ * is bigEndian or littleEndian; the default is bigEndian.
+ * 
+ * JavaScript note: readShort() malfunctioned because (short) (xx << 8) 
+ * isn't the same as (int) (xx << 8); same problem in java.io.DataStream
+ * 
+ * 
+ */
+
+public class BinaryDocument extends BC implements GenericBinaryDocument {
+
+  /**
+   * @j2sIgnore
+   */
+  public BinaryDocument() {  
+  }
+
+
+  // called by reflection
+  
+  protected DataInputStream stream;
+  protected boolean isRandom = false;
+  public boolean isBigEndian = true;
+  protected GenericZipTools jzt;
+
+  @Override
+  public void close() {
+    if (stream != null)
+      try {
+        stream.close();
+      } catch (Exception e) {
+        // ignore
+      }
+    if (out != null)
+       out.closeChannel();
+  }
+  
+  @Override
+  public void setStream(GenericZipTools jzt, BufferedInputStream bis, boolean isBigEndian) {
+    if (jzt != null)
+      this.jzt = jzt;
+    if (bis != null)
+      stream = new DataInputStream(bis);
+    this.isBigEndian = isBigEndian;
+  }
+  
+  @Override
+  public void setStreamData(DataInputStream stream, boolean isBigEndian) {
+    if (stream != null)
+      this.stream = stream;
+    this.isBigEndian = isBigEndian;
+  }
+  
+  public void setRandom(boolean TF) {
+    isRandom = TF;
+    //CANNOT be random for web 
+  }
+  
+  @Override
+  public byte readByte() throws Exception {
+    nBytes++;
+    return ioReadByte();
+  }
+
+  private byte ioReadByte() throws Exception {
+    byte b = stream.readByte();
+    if (out != null)
+      out.writeByteAsInt(b);
+    return b;
+  }
+
+  @Override
+  public int readByteArray(byte[] b, int off, int len) throws Exception {
+    int n = ioRead(b, off, len);
+    nBytes += n;
+    return n;
+  }
+
+  private int ioRead(byte[] b, int off, int len) throws Exception {
+    int m = 0;
+    while (len > 0) {
+      int n = stream.read(b, off, len);
+      m += n;
+      if (n > 0 && out != null)
+        writeBytes(b, off, n);
+      if (n >= len)
+        break;
+      off += n;
+      len -= n;
+    }
+    return m;
+  }
+
+  public void writeBytes(byte[] b, int off, int n) throws Exception {
+    out.write(b, off, n);
+  }
+
+  @Override
+  public String readString(int nChar) throws Exception {
+    byte[] temp = new byte[nChar];
+    int n = readByteArray(temp, 0, nChar);
+    return new String(temp, 0, n, "UTF-8");
+  }
+  
+  @Override
+  public short readShort() throws Exception {
+    nBytes += 2;
+    short n = (isBigEndian ? ioReadShort()
+        : (short) ((ioReadByte() & 0xff) 
+                 | (ioReadByte() & 0xff) << 8));
+    /**
+     * @j2sNative
+     *
+     * return (n > 0x7FFF ? n - 0x10000 : n);
+     */
+    {
+      return n;
+    }
+  }
+
+  private short ioReadShort() throws Exception {
+    short b = stream.readShort();
+    if (out != null)
+      writeShort(b);
+    return b;
+  }
+
+
+  public void writeShort(short i) throws Exception {
+    out.writeByteAsInt(i >> 8);
+    out.writeByteAsInt(i);
+  }
+
+  @Override
+  public int readIntLE() throws Exception {
+    nBytes += 4;
+    return readLEInt();
+  }
+  
+  @Override
+  public int readInt() throws Exception {
+    nBytes += 4;
+    return (isBigEndian ? ioReadInt() : readLEInt());
+  }
+  
+  private int ioReadInt() throws Exception {
+    int i = stream.readInt();
+    if (out != null)
+      writeInt(i);
+    return i;
+  }
+
+  public void writeInt(int i) throws Exception {
+    out.writeByteAsInt(i >> 24);
+    out.writeByteAsInt(i >> 16);
+    out.writeByteAsInt(i >> 8);
+    out.writeByteAsInt(i);
+  }
+
+  @Override
+  public int swapBytesI(int n) {
+    return (((n >> 24) & 0xff)
+        | ((n >> 16) & 0xff) << 8
+        | ((n >> 8) & 0xff) << 16 
+        | (n & 0xff) << 24);
+  }
+
+  @Override
+  public short swapBytesS(short n) {
+    return (short) ((((n >> 8) & 0xff)
+        | (n & 0xff) << 8));
+  }
+
+  
+  @Override
+  public int readUnsignedShort() throws Exception {
+    nBytes += 2;
+    int a = (ioReadByte() & 0xff);
+    int b = (ioReadByte() & 0xff);
+    return (isBigEndian ? (a << 8) + b : (b << 8) + a);
+  }
+  
+  @Override
+  public long readLong() throws Exception {
+    nBytes += 8;
+    return (isBigEndian ? ioReadLong()
+       : ((((long) ioReadByte()) & 0xff)
+        | (((long) ioReadByte()) & 0xff) << 8
+        | (((long) ioReadByte()) & 0xff) << 16
+        | (((long) ioReadByte()) & 0xff) << 24
+        | (((long) ioReadByte()) & 0xff) << 32
+        | (((long) ioReadByte()) & 0xff) << 40
+        | (((long) ioReadByte()) & 0xff) << 48 
+        | (((long) ioReadByte()) & 0xff) << 54));
+  }
+
+  private long ioReadLong() throws Exception {
+    long b = stream.readLong();
+    if (out != null)
+      writeLong(b);
+    return b;
+  }
+
+  public void writeLong(long b) throws Exception {
+    writeInt((int)((b >> 32) & 0xFFFFFFFFl));
+    writeInt((int)(b & 0xFFFFFFFFl));
+  }
+
+  private int readLEInt() throws Exception {
+    ioRead(t8, 0, 4);
+    return bytesToInt(t8, 0, false);
+  }
+
+  byte[] t8 = new byte[8];
+  
+  @Override
+  public float readFloat() throws Exception {
+    return intToFloat(readInt());
+  }
+
+  @Override
+  public double readDouble() throws Exception {
+    /**
+     * 
+     * reading the float equivalent here in JavaScript
+     * 
+     * @j2sNative
+     * 
+     * this.readByteArray(this.t8, 0, 8);
+     * return this.bytesToDoubleToFloat(this.t8, 0, this.isBigEndian);
+     *  
+     */
+    {
+      nBytes += 8;
+      return (isBigEndian ? ioReadDouble() : Double.longBitsToDouble(readLELong()));  
+    }
+  }
+  
+  private double ioReadDouble() throws Exception {
+    double d = stream.readDouble();
+    if (out != null)
+      writeLong(Double.doubleToRawLongBits(d));
+    return d;
+  }
+
+  private long readLELong() throws Exception {
+    return ((((long) ioReadByte()) & 0xff)
+          | (((long) ioReadByte()) & 0xff) << 8
+          | (((long) ioReadByte()) & 0xff) << 16 
+          | (((long) ioReadByte()) & 0xff) << 24
+          | (((long) ioReadByte()) & 0xff) << 32
+          | (((long) ioReadByte()) & 0xff) << 40
+          | (((long) ioReadByte()) & 0xff) << 48
+          | (((long) ioReadByte()) & 0xff) << 56);
+  }
+
+  @Override
+  public void seek(long offset) {
+    // slower, but all that is available using the applet
+    try {
+      if (offset == nBytes)
+        return;
+      if (offset < nBytes) {
+        stream.reset();
+        if (out != null && nBytes != 0)
+          out.reset();
+        nBytes = 0;
+      } else {
+        offset -= nBytes;
+      }
+      if (out == null) {
+        stream.skipBytes((int)offset);
+      } else {
+        readByteArray(new byte[(int)offset], 0, (int) offset);
+      }
+      nBytes += offset;
+    } catch (Exception e) {
+      System.out.println(e.toString());
+    }
+  }
+
+  long nBytes;
+  
+  @Override
+  public long getPosition() {
+    return nBytes;
+  }
+
+  OC out;
+  @Override
+  public void setOutputChannel(OC out) {
+      this.out = out;
+  }
+
+  @Override
+  public SB getAllDataFiles(String binaryFileList, String firstFile) {
+    return null;
+  }
+
+  @Override
+  public void getAllDataMapped(String replace, String string,
+                               Map<String, String> fileData) {
+  }
+
+
+/*  random access -- application only:
+ * 
+    void seekFile(long offset) {
+    try {
+      file.seek(offset);
+    } catch (Exception e) {
+      System.out.println(e.getMessage());
+    }
+  }
+*/
+}