// // ByteSwappedRandomAccessFile class // part of the set of documents known as Java no sugar. // Copyright (c) 1996 Sunil Gupta, sunil@magnetic.demon.co.uk // placed into the public domain by the author // // integers and shorts work, but floats are buggered // java doesnt come with byteswapping code, wot a crying shame. // import java.io.*; import EncapsulatedRandomAccessFile import ByteSwapper; public class ByteSwappedRandomAccessFile extends EncapsulatedRandomAccessFile { public boolean byteSwapping = false; //############################################################################ // Constructors //############################################################################ public ByteSwappedRandomAccessFile(File file, String mode) throws IOException { super(file,mode); } //############################################################################ // these need to be byteswapped //############################################################################ public short readShort() throws IOException { if (byteSwapping) return ( ByteSwapper.swapShort(super.readShort()) ); else return (super.readShort() ); } public long readLong() throws IOException { if (byteSwapping) return ( ByteSwapper.swapLong(super.readLong()) ); else return (super.readLong() ); } public int readInt() throws IOException { if (byteSwapping) return( ByteSwapper.swapInt(super.readInt()) ); else return (super.readInt()); } public int readUnsignedShort() throws IOException { if (byteSwapping) return ( ByteSwapper.swapShort( (short) super.readUnsignedShort()) ); else return (super.readUnsignedShort() ); } public double readDouble() throws IOException { //this is wrong but i dont quite know what to do return super.readDouble(); } public float readFloat() throws IOException { short mantissa,exponent; double real_mantissa; if (!byteSwapping) return ( super.readFloat()); else { mantissa = readShort(); exponent = readShort(); real_mantissa = ((float)mantissa) / 32768.0; /* 32768.0 is: 2**15 */ return (float) ldexp(real_mantissa,exponent); } } public static double ldexp(double x, int n) { double answer; answer = x * Math.pow(2.0, n); return answer; } }
.