001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032:
033: package com.vividsolutions.jump.io;
034:
035: import java.io.DataOutputStream;
036: import java.io.IOException;
037:
038: /**
039: * A class that gives most of the functionality of DataOutputStream, but is endian aware.
040: * Uses a real java.io.DataOutputStream to actually do the writing.
041: */
042: public class EndianDataOutputStream {
043: private java.io.DataOutputStream outputStream;
044:
045: /** Creates new EndianDataOutputStream */
046: public EndianDataOutputStream(java.io.OutputStream out) {
047: outputStream = new DataOutputStream(out);
048: }
049:
050: /** close stream**/
051: public void close() throws IOException {
052: outputStream.close();
053: }
054:
055: /** write bytes */
056: public void write(byte[] b, int off, int len) throws IOException {
057: outputStream.write(b, off, len);
058: }
059:
060: /** flush stream**/
061: public void flush() throws IOException {
062: outputStream.flush();
063: }
064:
065: /** write a byte in LittleEndian - this is exactly the same as the BigEndian version since there's no endian in a single byte */
066: public void writeByteLE(int b) throws IOException {
067: outputStream.writeByte(b);
068: }
069:
070: /** write a byte in BigEndian - this is exactly the same as the LittleEndian version since there's no endian in a single byte */
071: public void writeByteBE(int b) throws IOException {
072: outputStream.writeByte(b);
073: }
074:
075: /** write a set of bytes in LittleEndian - this is exactly the same as the BigEndian version since there's no endian in a single byte */
076: public void writeBytesLE(String s) throws IOException {
077: outputStream.writeBytes(s);
078: }
079:
080: /** write a set of bytes in BigEndian - this is exactly the same as the LittleEndian version since there's no endian in a single byte */
081: public void writeBytesBE(String s) throws IOException {
082: outputStream.writeBytes(s);
083: }
084:
085: /** write a 16bit short in BigEndian*/
086: public void writeShortBE(int s) throws IOException {
087: outputStream.writeShort(s);
088: }
089:
090: /** write a 16bit short in LittleEndian*/
091: public void writeShortLE(int s) throws IOException {
092: outputStream.writeByte(s);
093: outputStream.writeByte(s >> 8);
094: }
095:
096: /** write a 32bit int in BigEndian*/
097: public void writeIntBE(int i) throws IOException {
098: outputStream.writeInt(i);
099: }
100:
101: /** write a 32bit int in LittleEndian*/
102: public void writeIntLE(int i) throws IOException {
103: outputStream.writeByte(i);
104: outputStream.writeByte(i >> 8);
105: outputStream.writeByte(i >> 16);
106: outputStream.writeByte(i >> 24);
107: }
108:
109: /** write a 64bit long in BigEndian*/
110: public void writeLongBE(long l) throws IOException {
111: outputStream.writeLong(l);
112: }
113:
114: /** write a 64bit long in LittleEndian*/
115: public void writeLongLE(long l) throws IOException {
116: outputStream.writeByte((byte) (l));
117: outputStream.writeByte((byte) (l >> 8));
118: outputStream.writeByte((byte) (l >> 16));
119: outputStream.writeByte((byte) (l >> 24));
120: outputStream.writeByte((byte) (l >> 32));
121: outputStream.writeByte((byte) (l >> 40));
122: outputStream.writeByte((byte) (l >> 48));
123: outputStream.writeByte((byte) (l >> 56));
124: }
125:
126: /** write a 64bit double in BigEndian*/
127: public void writeDoubleBE(double d) throws IOException {
128: outputStream.writeDouble(d);
129: }
130:
131: /** write a 64bit double in LittleEndian*/
132: public void writeDoubleLE(double d) throws IOException {
133: this.writeLongLE(Double.doubleToLongBits(d));
134: }
135: }
|