Author: bayard Date: Fri May 18 16:37:59 2007 New Revision: 539632 URL: http://svn.apache.org/viewvc?view=rev&rev=539632 Log: Applying Hiroshi's test from IO-117 with my fix. Fixes negative number possibilities in EndianUtils.readSwappedUnsignedInteger()
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=539632&r1=539631&r2=539632 ============================================================================== --- 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 Fri May 18 16:37:59 2007 @@ -182,10 +182,13 @@ * @return the value read */ public static long readSwappedUnsignedInteger(byte[] data, int offset) { - return ( ( ( data[ offset + 0 ] & 0xff ) << 0 ) + - ( ( data[ offset + 1 ] & 0xff ) << 8 ) + - ( ( data[ offset + 2 ] & 0xff ) << 16 ) + - ( ( data[ offset + 3 ] & 0xff ) << 24 ) ); + long low = ( ( ( data[ offset + 0 ] & 0xff ) << 0 ) + + ( ( data[ offset + 1 ] & 0xff ) << 8 ) + + ( ( data[ offset + 2 ] & 0xff ) << 16 ) ); + + long high = data[ offset + 3 ] & 0xff; + + return (high << 24) + (0xffffffffL & low); } /** @@ -368,10 +371,13 @@ int value3 = read( input ); int value4 = read( input ); - return ( ( value1 & 0xff ) << 0 ) + - ( ( value2 & 0xff ) << 8 ) + - ( ( value3 & 0xff ) << 16 ) + - ( ( value4 & 0xff ) << 24 ); + long low = ( ( ( value1 & 0xff ) << 0 ) + + ( ( value2 & 0xff ) << 8 ) + + ( ( value3 & 0xff ) << 16 ) ); + + long high = value4 & 0xff; + + return (high << 24) + (0xffffffffL & low); } /** 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=539632&r1=539631&r2=539632 ============================================================================== --- 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 Fri May 18 16:37:59 2007 @@ -263,4 +263,17 @@ } } + // tests #IO-117 + public void testUnsignedOverrun() throws Exception { + byte[] target = new byte[] { 0, 0, 0, (byte)0x80 }; + long expected = 0x80000000L; + + long actual = EndianUtils.readSwappedUnsignedInteger(target, 0); + assertEquals("readSwappedUnsignedInteger(byte[], int) was incorrect", expected, actual); + + ByteArrayInputStream in = new ByteArrayInputStream(target); + actual = EndianUtils.readSwappedUnsignedInteger(in); + assertEquals("readSwappedUnsignedInteger(InputStream) was incorrect", expected, actual); + } + } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]