I am having trouble reading values from a binary file. The file is  
written in LSB order but X10 assumes MSB order. I have written methods  
to compensate for this. The methods for shorts, ints, and floats work,  
but I'm having trouble with the longs and doubles. Here is a test case.

Can someone verify this?

import x10.io.*;
import x10.io.Console;

class ByteTester {
        public static def main(args: Rail[String] ) {
                val fileName = "double-x10file.bin";
                write(fileName);
                
                //prints two floats -1.2251404E-32, 2.597222
                floatReader(2, fileName);
                
                //should print double 1.111111100000000e+01 (or equiv)
                doubleReader(1, fileName);
                return;
        }

        static def floatReader(n :Int, fileName: String ){
                var reader: FileReader;
                var i: Int = 0;
                try{                                    
                        reader = new FileReader( new File(fileName) );
                        for ( i = 0; i < n; i++ ){
                                val value = getFloat(reader);
                                Console.OUT.println( " -->\t" + value );
                        }               
                        reader.close();                 
                }catch(e:Exception){
                        Console.OUT.println("OOOPS!!");
                        e.printStackTrace();
                }
                return i;
        }       

        static def doubleReader(n :Int, fileName: String ){
                var reader: FileReader;
                var i: Int = 0;
                try{                                    
                        reader = new FileReader( new File(fileName) );
                        for ( i = 0; i < n; i++ ){
                                val value = getDouble(reader);
                                Console.OUT.println( " -->\t" + value );
                        }               
                        reader.close();                 
                }catch(e:Exception){
                        Console.OUT.println("OOOPS!!");
                        e.printStackTrace();
                }
                return i;
        }       

        /*writes 8 bytes*/
        public static def write(fileName: String){
                try{                                    
                        var writer:FileWriter = new FileWriter( new 
File(fileName));
                        writer.writeByte(0xa3 as byte);
                        writer.writeByte(0x73 as byte);
                        writer.writeByte(0x7e as byte);
                        writer.writeByte(0x8a as byte);

                        writer.writeByte(0xe3 as byte);
                        writer.writeByte(0x38 as byte);
                        writer.writeByte(0x26 as byte);
                        writer.writeByte(0x40 as byte);
                        writer.close();
                }catch(e:Exception){
                        Console.OUT.println("OOOPS!!");
                        e.printStackTrace();
                }       
        }

        /* reads 8 bytes in MSB and converts to LSB incorrectly */
        static def getDouble( reader: FileReader ) : Double throws  
IOException {
                val byte0 = reader.read(); Console.OUT.printf(" %X\t", byte0);
                val byte1 = reader.read(); Console.OUT.printf(" %X\t", byte1);
                val byte2 = reader.read(); Console.OUT.printf(" %X\t", byte2);
                val byte3 = reader.read(); Console.OUT.printf(" %X\t", byte3);
                val byte4 = reader.read(); Console.OUT.printf(" %X\t", byte4);
                val byte5 = reader.read(); Console.OUT.printf(" %X\t", byte5);
                val byte6 = reader.read(); Console.OUT.printf(" %X\t", byte6);
                val byte7 = reader.read(); Console.OUT.printf(" %X\t", byte7);
                
                val ret1 = ( ((byte7&0xff)<<56) | ((byte6&0xff)<<48) |  
((byte5&0xff)<<40) | ((byte4 & 0xff)<<32) );
                val ret0 = ( ((byte3&0xff)<<24) | ((byte2&0xff)<<16) |  
((byte1&0xff)<<8) | (byte0 & 0xff) );
                return Double.fromLongBits((ret0|ret1) as Long);
        }
        
        /* reads 4 bytes in MSB and converts to LSB correctly */
        static def getFloat( reader: FileReader ) :Float throws IOException {
                val byte0 = reader.read(); Console.OUT.printf(" %X \t",byte0);
                val byte1 = reader.read(); Console.OUT.printf(" %X \t",byte1);
                val byte2 = reader.read(); Console.OUT.printf(" %X \t",byte2);
                val byte3 = reader.read(); Console.OUT.printf(" %X \t",byte3);
                val ret = ( ((byte3&0xff)<<24) | ((byte2&0xff)<<16) |  
((byte1&0xff)<<8) | (byte0 & 0xff) );
                return Float.fromIntBits(ret);
        }
}

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with 
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
X10-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/x10-users

Reply via email to