001: /*
002: * EndianDataInputStream.java
003: *
004: * Created on September 6, 2002, 1:42 PM
005: */
006: /*
007: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
008: * for visualizing and manipulating spatial features with geometry and attributes.
009: *
010: * Copyright (C) 2003 Vivid Solutions
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
025: *
026: * For more information, contact:
027: *
028: * Vivid Solutions
029: * Suite #1A
030: * 2328 Government Street
031: * Victoria BC V8T 5G5
032: * Canada
033: *
034: * (250)385-6040
035: * www.vividsolutions.com
036: */
037:
038: package com.vividsolutions.jump.io;
039:
040: import java.io.DataInputStream;
041: import java.io.IOException;
042: import java.io.BufferedInputStream;
043:
044: /**
045: * A class that gives most of the functionality of DataInputStream, but is endian aware.
046: * Uses a real java.io.DataInputStream to actually do the writing.
047: */
048: public class EndianDataInputStream {
049: private java.io.DataInputStream inputStream;
050: private byte[] workSpace = new byte[8]; //chars are 16 bits, so we always quash the 1st 8 bits
051:
052: /** Creates new EndianDataInputStream */
053: public EndianDataInputStream(java.io.InputStream in) {
054: inputStream = new DataInputStream(new BufferedInputStream(in));
055: }
056:
057: /** close the stream**/
058: public void close() throws IOException {
059: inputStream.close();
060: }
061:
062: /** read a byte in BigEndian - the same as LE because its only 1 byte*/
063: public byte readByteBE() throws IOException {
064: return inputStream.readByte();
065: }
066:
067: /** read a byte in LittleEndian - the same as BE because its only 1 byte*/
068: public byte readByteLE() throws IOException {
069: return inputStream.readByte();
070: }
071:
072: /** read a byte in LittleEndian - the same as BE because its only 1 byte*/
073: public void readByteLEnum(byte[] b) throws IOException {
074: inputStream.readFully(b);
075: }
076:
077: /** read a byte in BigEndian - the same as LE because its only 1 byte. returns int as per java.io.DataStream*/
078: public int readUnsignedByteBE() throws IOException {
079: return inputStream.readUnsignedByte();
080: }
081:
082: /** read a byte in LittleEndian - the same as BE because its only 1 byte. returns int as per java.io.DataStream*/
083: public int readUnsignedByteLE() throws IOException {
084: return inputStream.readUnsignedByte();
085: }
086:
087: /** read a 16bit short in BE*/
088: public short readShortBE() throws IOException {
089: return inputStream.readShort();
090: }
091:
092: /** read a 16bit short in LE*/
093: public short readShortLE() throws IOException {
094: inputStream.readFully(workSpace, 0, 2);
095:
096: return (short) (((workSpace[1] & 0xff) << 8) | (workSpace[0] & 0xff));
097: }
098:
099: /** read a 32bit int in BE*/
100: public int readIntBE() throws IOException {
101: return inputStream.readInt();
102: }
103:
104: /** read a 32bit int in LE*/
105: public int readIntLE() throws IOException {
106: inputStream.readFully(workSpace, 0, 4);
107:
108: return ((workSpace[3] & 0xff) << 24)
109: | ((workSpace[2] & 0xff) << 16)
110: | ((workSpace[1] & 0xff) << 8) | (workSpace[0] & 0xff);
111: }
112:
113: /** read a 64bit long in BE*/
114: public long readLongBE() throws IOException {
115: return inputStream.readLong();
116: }
117:
118: /** read a 64bit long in LE*/
119: public long readLongLE() throws IOException {
120: inputStream.readFully(workSpace, 0, 8);
121:
122: return ((long) (workSpace[7] & 0xff) << 56)
123: | ((long) (workSpace[6] & 0xff) << 48)
124: | ((long) (workSpace[5] & 0xff) << 40)
125: | ((long) (workSpace[4] & 0xff) << 32)
126: | ((long) (workSpace[3] & 0xff) << 24)
127: | ((long) (workSpace[2] & 0xff) << 16)
128: | ((long) (workSpace[1] & 0xff) << 8)
129: | ((long) (workSpace[0] & 0xff));
130: }
131:
132: /** read a 64bit double in BE*/
133: public double readDoubleBE() throws IOException {
134: return inputStream.readDouble();
135: }
136:
137: /** read a 64bit double in LE*/
138: public double readDoubleLE() throws IOException {
139: long l;
140:
141: inputStream.readFully(workSpace, 0, 8);
142: l = ((long) (workSpace[7] & 0xff) << 56)
143: | ((long) (workSpace[6] & 0xff) << 48)
144: | ((long) (workSpace[5] & 0xff) << 40)
145: | ((long) (workSpace[4] & 0xff) << 32)
146: | ((long) (workSpace[3] & 0xff) << 24)
147: | ((long) (workSpace[2] & 0xff) << 16)
148: | ((long) (workSpace[1] & 0xff) << 8)
149: | ((long) (workSpace[0] & 0xff));
150:
151: return Double.longBitsToDouble(l);
152: }
153:
154: /** skip ahead in the stream
155: * @param num number of bytes to read ahead
156: */
157: public int skipBytes(int num) throws IOException {
158: return inputStream.skipBytes(num);
159: }
160: }
|