3 * $Date: 2006-03-18 15:59:33 -0600 (Sat, 18 Mar 2006) $
6 * Copyright (C) 2003-2005 Miguel, Jmol Development, www.jmol.org
8 * Contact: hansonr@stolaf.edu
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
27 import java.io.BufferedInputStream;
28 import java.io.DataInputStream;
32 import javajs.api.GenericBinaryDocument;
33 import javajs.api.GenericZipTools;
36 //import java.io.RandomAccessFile;
38 /* a basic binary file reader (extended by CompoundDocument).
40 * random access file info:
41 * http://java.sun.com/docs/books/tutorial/essential/io/rafs.html
43 * SHOOT! random access is only for applications, not applets!
45 * Note that YOU are responsible for determining whether a file
46 * is bigEndian or littleEndian; the default is bigEndian.
48 * JavaScript note: readShort() malfunctioned because (short) (xx << 8)
49 * isn't the same as (int) (xx << 8); same problem in java.io.DataStream
54 public class BinaryDocument extends BC implements GenericBinaryDocument {
59 public BinaryDocument() {
63 // called by reflection
65 protected DataInputStream stream;
66 protected boolean isRandom = false;
67 public boolean isBigEndian = true;
68 protected GenericZipTools jzt;
75 } catch (Exception e) {
83 public void setStream(GenericZipTools jzt, BufferedInputStream bis, boolean isBigEndian) {
87 stream = new DataInputStream(bis);
88 this.isBigEndian = isBigEndian;
92 public void setStreamData(DataInputStream stream, boolean isBigEndian) {
95 this.isBigEndian = isBigEndian;
98 public void setRandom(boolean TF) {
100 //CANNOT be random for web
104 public byte readByte() throws Exception {
109 private byte ioReadByte() throws Exception {
110 byte b = stream.readByte();
112 out.writeByteAsInt(b);
117 public int readByteArray(byte[] b, int off, int len) throws Exception {
118 int n = ioRead(b, off, len);
123 private int ioRead(byte[] b, int off, int len) throws Exception {
126 int n = stream.read(b, off, len);
128 if (n > 0 && out != null)
129 writeBytes(b, off, n);
138 public void writeBytes(byte[] b, int off, int n) throws Exception {
139 out.write(b, off, n);
143 public String readString(int nChar) throws Exception {
144 byte[] temp = new byte[nChar];
145 int n = readByteArray(temp, 0, nChar);
146 return new String(temp, 0, n, "UTF-8");
150 public short readShort() throws Exception {
152 short n = (isBigEndian ? ioReadShort()
153 : (short) ((ioReadByte() & 0xff)
154 | (ioReadByte() & 0xff) << 8));
158 * return (n > 0x7FFF ? n - 0x10000 : n);
165 private short ioReadShort() throws Exception {
166 short b = stream.readShort();
173 public void writeShort(short i) throws Exception {
174 out.writeByteAsInt(i >> 8);
175 out.writeByteAsInt(i);
179 public int readIntLE() throws Exception {
185 public int readInt() throws Exception {
187 return (isBigEndian ? ioReadInt() : readLEInt());
190 private int ioReadInt() throws Exception {
191 int i = stream.readInt();
197 public void writeInt(int i) throws Exception {
198 out.writeByteAsInt(i >> 24);
199 out.writeByteAsInt(i >> 16);
200 out.writeByteAsInt(i >> 8);
201 out.writeByteAsInt(i);
205 public int swapBytesI(int n) {
206 return (((n >> 24) & 0xff)
207 | ((n >> 16) & 0xff) << 8
208 | ((n >> 8) & 0xff) << 16
213 public short swapBytesS(short n) {
214 return (short) ((((n >> 8) & 0xff)
220 public int readUnsignedShort() throws Exception {
222 int a = (ioReadByte() & 0xff);
223 int b = (ioReadByte() & 0xff);
224 return (isBigEndian ? (a << 8) + b : (b << 8) + a);
228 public long readLong() throws Exception {
230 return (isBigEndian ? ioReadLong()
231 : ((((long) ioReadByte()) & 0xff)
232 | (((long) ioReadByte()) & 0xff) << 8
233 | (((long) ioReadByte()) & 0xff) << 16
234 | (((long) ioReadByte()) & 0xff) << 24
235 | (((long) ioReadByte()) & 0xff) << 32
236 | (((long) ioReadByte()) & 0xff) << 40
237 | (((long) ioReadByte()) & 0xff) << 48
238 | (((long) ioReadByte()) & 0xff) << 54));
241 private long ioReadLong() throws Exception {
242 long b = stream.readLong();
248 public void writeLong(long b) throws Exception {
249 writeInt((int)((b >> 32) & 0xFFFFFFFFl));
250 writeInt((int)(b & 0xFFFFFFFFl));
253 private int readLEInt() throws Exception {
255 return bytesToInt(t8, 0, false);
258 byte[] t8 = new byte[8];
261 public float readFloat() throws Exception {
262 return intToFloat(readInt());
266 public double readDouble() throws Exception {
269 * reading the float equivalent here in JavaScript
273 * this.readByteArray(this.t8, 0, 8);
274 * return this.bytesToDoubleToFloat(this.t8, 0, this.isBigEndian);
279 return (isBigEndian ? ioReadDouble() : Double.longBitsToDouble(readLELong()));
283 private double ioReadDouble() throws Exception {
284 double d = stream.readDouble();
286 writeLong(Double.doubleToRawLongBits(d));
290 private long readLELong() throws Exception {
291 return ((((long) ioReadByte()) & 0xff)
292 | (((long) ioReadByte()) & 0xff) << 8
293 | (((long) ioReadByte()) & 0xff) << 16
294 | (((long) ioReadByte()) & 0xff) << 24
295 | (((long) ioReadByte()) & 0xff) << 32
296 | (((long) ioReadByte()) & 0xff) << 40
297 | (((long) ioReadByte()) & 0xff) << 48
298 | (((long) ioReadByte()) & 0xff) << 56);
302 public void seek(long offset) {
303 // slower, but all that is available using the applet
305 if (offset == nBytes)
307 if (offset < nBytes) {
309 if (out != null && nBytes != 0)
316 stream.skipBytes((int)offset);
318 readByteArray(new byte[(int)offset], 0, (int) offset);
321 } catch (Exception e) {
322 System.out.println(e.toString());
329 public long getPosition() {
335 public void setOutputChannel(OC out) {
340 public SB getAllDataFiles(String binaryFileList, String firstFile) {
345 public void getAllDataMapped(String replace, String string,
346 Map<String, String> fileData) {
350 /* random access -- application only:
352 void seekFile(long offset) {
355 } catch (Exception e) {
356 System.out.println(e.getMessage());