Jim LaGrone <[email protected]> wrote on 08/12/2009 10:55:38 AM:

> On Aug 12, 2009, at 12:09 AM, Igor Peshansky wrote:
> 
> > You neglected to mention whether you're using the C++ backend or the 
> > Java one.
> 
> Java

Right.  I realized later that close() is broken in the C++ backend.
Your code with my proposed fix works now with the C++ backend as well.

> >  Regardless, I think I've found your problem:
> >
> >>   static def getDouble( reader: FileReader ) : Double throws
> > IOException {
> >>      val byte0 = reader.read(); Console.OUT.printf(" %X\t", byte0);
> >        ^^^^^^^^^
> > The inferred type of byte0 is Int (since that's what reader.read()
> > returns).
> 
> Reader class method read() appears to read a Byte

Sorry, you're right.  It is, of course, Byte.  However, Bytes are
promoted to Ints in arithmetic operations, just like in Java.

> InputStream class method read() appears to read an Int

Yes, and it's not supposed to be used outside of the hierarchy
(thus the protected modifier).

> InputStreamReader class method read() appears to read a Byte

Right, because it overrides the above in Reader.

> With no documentation in the source code things become rather 
> perplexing.

True.  We are actively working on that now.

> >>      val ret1 = ( ((byte7&0xff)<<56) | ((byte6&0xff)<<48) |
> > ((byte5&0xff)<<40) | ((byte4 & 0xff)<<32) );
> >        ^^^^^^^^
> > Ditto for the inferred type of ret1 (since the whole expression is 
> > using
> > Ints).
> >
> >>      return Double.fromLongBits((ret0|ret1) as Long);
> >                                               ^^^^^^^
> > This cast comes too late - the value has already been truncated by 
> > this
> > point.
> >
> > I think the solution is to explicitly specify the type of byte0 
> > through
> > byte7 as Long, like this:
> >
> >>      val byte0: Long = reader.read(); Console.OUT.printf(" %X\t",
> > byte0);
> >
> > etc.
> 
> This seems to fix my problem. Thanks!

Good.

> > Any reason you are not using the Marshal approach I suggested earlier?
> 
> The Marshal class appears to read MSB. My file is LSB.

Well, I wasn't suggesting you use Marshal.DOUBLE (which indeed reads
MSB).  But you could provide your own implementation of Marshal[Double]
that reads bytes in whatever order you wish.  For example:

class LSB {
    public static class MyDoubleMarshal implements Marshal[Double] {
        public def read(r: Reader): Double throws IOException {
            var l: Long = 0l;
            for (var shift: Int = 0; shift < 64; shift+=8) {
                val b : Long = r.read();
                l |= (b & 0xff) << shift;
            }
            return Double.fromLongBits(l);
        }

        public def write(w: Writer, d: Double): Void throws IOException {
            val l: Long = d.toLongBits();
            for (var shift: Int = 0; shift < 64; shift+=8) {
                val b = ((l >>> shift) & 0xffL) as Byte;
                w.write(b);
            }
        }
    }
    const DOUBLE = new MyDoubleMarshal();
}

You can then use the above as follows:

    val r: Reader = ...;
    val d: Double = r.read(LSB.DOUBLE);

or even

    val da = Rail.makeVar[Double](100);
    r.read(LSB.DOUBLE, da);

to read an array of 100 doubles in LSB format.
        Igor
-- 
Igor Peshansky  (note the spelling change!)
IBM T.J. Watson Research Center
XJ: No More Pain for XML's Gain (http://www.research.ibm.com/xj/)
X10: Parallel Productivity and Performance (http://x10.sf.net/)


------------------------------------------------------------------------------
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