package jeql.io; import java.io.DataOutputStream; import java.io.IOException; /** * A class that gives most of the functionality of DataOutputStream, but is endian aware. * Uses a real java.io.DataOutputStream to actually do the writing. */ public class EndianDataOutputStream { private java.io.DataOutputStream outputStream; /** Creates new EndianDataOutputStream */ public EndianDataOutputStream(java.io.OutputStream out) { outputStream = new DataOutputStream(out); } /** close stream**/ public void close() throws IOException { outputStream.close(); } /** write bytes */ public void write(byte[] b, int off, int len) throws IOException { outputStream.write(b, off, len); } /** flush stream**/ public void flush() throws IOException { outputStream.flush(); } /** write a byte in LittleEndian - this is exactly the same as the BigEndian version since there's no endian in a single byte */ public void writeByteLE(int b) throws IOException { outputStream.writeByte(b); } /** write a byte in BigEndian - this is exactly the same as the LittleEndian version since there's no endian in a single byte */ public void writeByteBE(int b) throws IOException { outputStream.writeByte(b); } /** 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 */ public void writeBytesLE(String s) throws IOException { outputStream.writeBytes(s); } /** 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 */ public void writeBytesBE(String s) throws IOException { outputStream.writeBytes(s); } /** write a 16bit short in BigEndian*/ public void writeShortBE(int s) throws IOException { outputStream.writeShort(s); } /** write a 16bit short in LittleEndian*/ public void writeShortLE(int s) throws IOException { outputStream.writeByte(s); outputStream.writeByte(s >> 8); } /** write a 32bit int in BigEndian*/ public void writeIntBE(int i) throws IOException { outputStream.writeInt(i); } /** write a 32bit int in LittleEndian*/ public void writeIntLE(int i) throws IOException { outputStream.writeByte(i); outputStream.writeByte(i >> 8); outputStream.writeByte(i >> 16); outputStream.writeByte(i >> 24); } /** write a 64bit long in BigEndian*/ public void writeLongBE(long l) throws IOException { outputStream.writeLong(l); } /** write a 64bit long in LittleEndian*/ public void writeLongLE(long l) throws IOException { outputStream.writeByte((byte) (l)); outputStream.writeByte((byte) (l >> 8)); outputStream.writeByte((byte) (l >> 16)); outputStream.writeByte((byte) (l >> 24)); outputStream.writeByte((byte) (l >> 32)); outputStream.writeByte((byte) (l >> 40)); outputStream.writeByte((byte) (l >> 48)); outputStream.writeByte((byte) (l >> 56)); } /** write a 64bit double in BigEndian*/ public void writeDoubleBE(double d) throws IOException { outputStream.writeDouble(d); } /** write a 64bit double in LittleEndian*/ public void writeDoubleLE(double d) throws IOException { this.writeLongLE(Double.doubleToLongBits(d)); } }