please dont rip this site

Language Java Nosugar Comms Code Byteswappedrandomaccessfile.java

// 
//	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 19:52, local time: 2024/12/28 23:32,
TOP NEW HELP FIND: 
18.117.254.177: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?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://sxlist.com/techref/language/JAVA/nosugar/comms/code/ByteSwappedRandomAccessFile.java"> language JAVA nosugar comms code ByteSwappedRandomAccessFile</A>

Did you find what you needed?

 

Welcome to sxlist.com!


Site supported by
sales, advertizing,
& kind contributors
just like you!

Please don't rip/copy
(here's why

Copies of the site on CD
are available at minimal cost.
 

Welcome to sxlist.com!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .