You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ba...@apache.org on 2006/12/06 01:23:19 UTC

svn commit: r482841 - in /jakarta/commons/proper/io/trunk/src: java/org/apache/commons/io/EndianUtils.java test/org/apache/commons/io/EndianUtilsTest.java

Author: bayard
Date: Tue Dec  5 16:23:18 2006
New Revision: 482841

URL: http://svn.apache.org/viewvc?view=rev&rev=482841
Log:
Committing my patch from #IO-101, fixing an <int> overrun in readSwappedLong. Many thanks to Jose Pinto for finding this

Modified:
    jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/EndianUtils.java
    jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/EndianUtilsTest.java

Modified: jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/EndianUtils.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/EndianUtils.java?view=diff&rev=482841&r1=482840&r2=482841
==============================================================================
--- jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/EndianUtils.java (original)
+++ jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/EndianUtils.java Tue Dec  5 16:23:18 2006
@@ -218,12 +218,12 @@
             ( ( data[ offset + 0 ] & 0xff ) << 0 ) +
             ( ( data[ offset + 1 ] & 0xff ) << 8 ) +
             ( ( data[ offset + 2 ] & 0xff ) << 16 ) +
-            ( ( data[ offset + 3 ] & 0xff ) << 24 );
+            ( ( (long) ( data[ offset + 3 ] & 0xff ) ) << 24 );
         long high = 
             ( ( data[ offset + 4 ] & 0xff ) << 0 ) +
             ( ( data[ offset + 5 ] & 0xff ) << 8 ) +
             ( ( data[ offset + 6 ] & 0xff ) << 16 ) +
-            ( ( data[ offset + 7 ] & 0xff ) << 24 );
+            ( ( (long) ( data[ offset + 7 ] & 0xff ) ) << 24 );
         return low + (high << 32);
     }
 

Modified: jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/EndianUtilsTest.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/EndianUtilsTest.java?view=diff&rev=482841&r1=482840&r2=482841
==============================================================================
--- jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/EndianUtilsTest.java (original)
+++ jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/EndianUtilsTest.java Tue Dec  5 16:23:18 2006
@@ -242,4 +242,25 @@
         assertEquals( 0x01, bytes[7] );
     }
 
+    // tests #IO-101
+    public void testSymmetryOfLong() throws IOException {
+
+        double[] tests = new double[] {34.345, -345.5645, 545.12, 10.043, 7.123456789123};
+        for (int i = 0; i< tests.length ;i++) {
+
+            // testing the real problem
+            byte[] buffer = new byte[8];
+            long ln1 = Double.doubleToLongBits( tests[i] );
+            EndianUtils.writeSwappedLong(buffer, 0, ln1);
+            long ln2 = EndianUtils.readSwappedLong(buffer, 0);
+            assertEquals( ln1, ln2 );
+
+            // testing the bug report
+            buffer = new byte[8];
+            EndianUtils.writeSwappedDouble(buffer, 0, tests[i]);
+            double val = EndianUtils.readSwappedDouble(buffer, 0);
+            assertEquals( tests[i], val, 0 );
+        }
+    }
+
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org