2 * $Date: 2006-03-18 15:59:33 -0600 (Sat, 18 Mar 2006) $
5 * Some portions of this file have been modified by Robert Hanson hansonr.at.stolaf.edu 2012-2017
6 * for use in SwingJS via transpilation into JavaScript using Java2Script.
8 * Copyright (C) 2003-2005 Miguel, Jmol Development, www.jmol.org
10 * Contact: hansonr@stolaf.edu
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
29 import java.io.BufferedInputStream;
30 import java.io.DataInputStream;
31 import java.io.IOException;
34 import javajs.api.GenericBinaryDocument;
35 import javajs.api.GenericOutputChannel;
37 /* a basic binary file reader (extended by CompoundDocument).
39 * Note that YOU are responsible for determining whether a file
40 * is bigEndian or littleEndian; the default is bigEndian.
42 * JavaScript note: readShort() malfunctioned because (short) (xx << 8)
43 * isn't the same as (int) (xx << 8); same problem in java.io.DataStream
48 public class BinaryDocument extends BC implements GenericBinaryDocument {
50 public BinaryDocument() {
54 // called by reflection
56 protected DataInputStream stream;
57 protected boolean isRandom = false;
58 protected boolean isBigEndian = true;
59 protected BufferedInputStream bis;
60 protected long nBytes;
61 protected GenericOutputChannel out;
69 } catch (IOException e) {
77 public BinaryDocument setStream(BufferedInputStream bis, boolean isBigEndian) {
80 stream = new DataInputStream(bis);
82 this.isBigEndian = isBigEndian;
87 public BufferedInputStream getInputStream() {
92 public void setStreamData(DataInputStream stream, boolean isBigEndian) {
95 this.isBigEndian = isBigEndian;
99 public void setOutputChannel(GenericOutputChannel out) {
103 public void setRandom(boolean TF) {
105 //CANNOT be random for web
109 public byte readByte() throws IOException {
115 public int readUInt8() throws IOException {
117 int b = stream.readUnsignedByte();
119 out.writeByteAsInt(b);
123 private byte ioReadByte() throws IOException {
124 byte b = stream.readByte();
126 out.writeByteAsInt(b);
131 public byte[] readBytes(int n) throws IOException {
132 byte[] b = new byte[n];
133 readByteArray(b, 0, n);
138 public int readByteArray(byte[] b, int off, int len) throws IOException {
139 int n = ioRead(b, off, len);
144 private int ioRead(byte[] b, int off, int len) throws IOException {
147 int n = stream.read(b, off, len);
149 if (n > 0 && out != null)
150 out.write(b, off, n);
160 public String readString(int nChar) throws IOException {
161 byte[] temp = new byte[nChar];
162 int n = readByteArray(temp, 0, nChar);
163 return new String(temp, 0, n, "UTF-8");
167 public short readShort() throws IOException {
169 short n = (isBigEndian ? ioReadShort()
170 : (short) ((ioReadByte() & 0xff)
171 | (ioReadByte() & 0xff) << 8));
175 * return (n > 0x7FFF ? n - 0x10000 : n);
182 private short ioReadShort() throws IOException {
183 short b = stream.readShort();
191 public int readIntLE() throws IOException {
197 public int readInt() throws IOException {
199 return (isBigEndian ? ioReadInt() : readLEInt());
202 private int ioReadInt() throws IOException {
203 int i = stream.readInt();
210 public int swapBytesI(int n) {
211 return (((n >> 24) & 0xff)
212 | ((n >> 16) & 0xff) << 8
213 | ((n >> 8) & 0xff) << 16
218 public short swapBytesS(short n) {
219 return (short) ((((n >> 8) & 0xff)
225 public int readUnsignedShort() throws IOException {
227 int a = (ioReadByte() & 0xff);
228 int b = (ioReadByte() & 0xff);
229 return (isBigEndian ? (a << 8) + b : (b << 8) + a);
233 public long readLong() throws IOException {
235 return (isBigEndian ? ioReadLong()
236 : ((((long) ioReadByte()) & 0xff)
237 | (((long) ioReadByte()) & 0xff) << 8
238 | (((long) ioReadByte()) & 0xff) << 16
239 | (((long) ioReadByte()) & 0xff) << 24
240 | (((long) ioReadByte()) & 0xff) << 32
241 | (((long) ioReadByte()) & 0xff) << 40
242 | (((long) ioReadByte()) & 0xff) << 48
243 | (((long) ioReadByte()) & 0xff) << 54));
246 private long ioReadLong() throws IOException {
247 long b = stream.readLong();
253 private int readLEInt() throws IOException {
255 return bytesToInt(t8, 0, false);
258 byte[] t8 = new byte[8];
261 public float readFloat() throws Exception {
262 return intToFloat(readInt());
265 @SuppressWarnings("unused")
267 public double readDouble() throws IOException {
270 * reading the float equivalent here in JavaScript
279 return (isBigEndian ? ioReadDouble()
280 : Double.longBitsToDouble(readLELong()));
282 // this is the JavaScript-only part
283 this.readByteArray(this.t8, 0, 8);
284 return bytesToDoubleToFloat(this.t8, 0, this.isBigEndian);
287 private double ioReadDouble() throws IOException {
288 double d = stream.readDouble();
290 out.writeLong(Double.doubleToRawLongBits(d));
294 private long readLELong() throws IOException {
295 return ((((long) ioReadByte()) & 0xff)
296 | (((long) ioReadByte()) & 0xff) << 8
297 | (((long) ioReadByte()) & 0xff) << 16
298 | (((long) ioReadByte()) & 0xff) << 24
299 | (((long) ioReadByte()) & 0xff) << 32
300 | (((long) ioReadByte()) & 0xff) << 40
301 | (((long) ioReadByte()) & 0xff) << 48
302 | (((long) ioReadByte()) & 0xff) << 56);
306 public void seek(long offset) {
307 // slower, but all that is available using the applet
309 if (offset == nBytes)
311 if (offset < nBytes) {
313 if (out != null && nBytes != 0)
320 stream.skipBytes((int)offset);
322 readByteArray(new byte[(int)offset], 0, (int) offset);
325 } catch (IOException e) {
326 System.out.println(e.toString());
331 public long getPosition() {
336 public SB getAllDataFiles(String binaryFileList, String firstFile) {
341 public void getAllDataMapped(String replace, String string,
342 Map<String, String> fileData) {
346 /* random access -- application only:
348 void seekFile(long offset) {
351 } catch (IOException e) {
352 System.out.println(e.getMessage());