// // 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; } }
file: /Techref/language/java/nosugar/comms/code/ByteSwappedRandomAccessFile.java, 2KB, , updated: 1996/10/16 20:52, local time: 2024/8/26 10:21,
18.190.153.9:LOG IN
|
©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://massmind.org/techref/language/java/nosugar/comms/code/ByteSwappedRandomAccessFile.java"> language java nosugar comms code ByteSwappedRandomAccessFile</A> |
Did you find what you needed? |
Welcome to massmind.org! |
Welcome to massmind.org! |
.