[Fwd: BUG - different writing and reading of Long value]

Jan Kozusznik <[email protected]>
Newsgroups gmane.comp.java.netbeans.modules.mdr.devel
Message-ID <[email protected]>
Hello developers,

there was some different bug. I found that variables ch1,ch2 ...ch4 may be
Long value. There was problem because sometime ch4<<4 has negative
result. So correct code is:

      case T_LONG | 0x80: {

              //added by kozusznikj

               long ch1, ch2, ch3, ch4;

               long v_low,v_high;

              Â

               ch1 = inputStream.read();

               ch2 = inputStream.read();

               ch3 = inputStream.read();

               ch4 = inputStream.read();

               v_low = (ch4 << 24) | (ch3 << 16) | (ch2 <<
8) | ch1;

               ch1 = inputStream.read();

               ch2 = inputStream.read();

               ch3 = inputStream.read();

               ch4 = inputStream.read();

               v_high = ((ch4 << 24) | (ch3 << 16) | (ch2 <<
8) | ch1);

               return (v_high << 32) | v_low;

              Â

           }

thanks.

-------- Original Message --------

Subject:
BUG - different writing and reading of Long value

Date:
Mon, 29 Jul 2002 12:23:09 +0200

From:
Jan Kozusznik <[email protected]>

To:
[email protected]

Hello developers,

PROBLEM:

There was bug in saving value of type Long. When  I set Long attribute to
1027634400000 and later I restarted NBMDR this attribute had different value.
I found bug in IOUtils in method readLong. There was code:

      case T_LONG | 0x80: {

               int ch1, ch2, ch3, ch4;

               long v;

              Â

               ch1 = inputStream.read();

               ch2 = inputStream.read();

               ch3 = inputStream.read();

               ch4 = inputStream.read();

               v = (ch4 << 24) + (ch3 << 16) + (ch2 <<
8) + ch1;

               ch1 = inputStream.read();

               ch2 = inputStream.read();

               ch3 = inputStream.read();

               ch4 = inputStream.read();

               return v << 32 + ((ch4 << 24) + (ch3 <<
16) + (ch2 << 8) + ch1);

but in readLong there was code:

         outputStream.write(T_LONG | 0x80);

           outputStream.write((byte)(val & 0xff));

           outputStream.write((byte)((val & 0xff00) >>> 8));

           outputStream.write((byte)((val >>> 16) & 0xff));

           outputStream.write((byte)((val >>> 24) & 0xff));

           outputStream.write((byte)((val >>> 32) & 0xff));

           outputStream.write((byte)((val >>> 40) & 0xff));

           outputStream.write((byte)((val >>> 48) & 0xff));

           outputStream.write((byte)((val >>> 56) & 0xff));

As you can see lower 32-bits were writted as first ones and upper 32-bits
as second. But reading was reversed.

SOLUTION:

put in method readLong new code:

      case T_LONG | 0x80: {

            int ch1, ch2, ch3, ch4;

               long v_low,v_high;

               //add by kozusznikj

               ch1 = inputStream.read();

               ch2 = inputStream.read();

               ch3 = inputStream.read();

               ch4 = inputStream.read();

               v_low = (ch4 << 24) + (ch3 << 16) + (ch2 <<
8) + ch1;

               ch1 = inputStream.read();

               ch2 = inputStream.read();

               ch3 = inputStream.read();

               ch4 = inputStream.read();

               v_high = ((ch4 << 24) + (ch3 << 16) + (ch2 <<
8) + ch1);

               return (v_high << 32) + v_low;

Jan Kozusznik

Disnet Software,a.s.

--
mej se fajn
-----------
HonzaK.
:wq
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.