浏览代码

HADOOP-3094. Fix BytesWritable.toString to avoid extending the sign bit.
Contributed by Owen O'Malley.




git-svn-id: https://svn.apache.org/repos/asf/hadoop/core/trunk@641613 13f79535-47bb-0310-9956-ffa450edef68

Christopher Douglas 17 年之前
父节点
当前提交
f5563e3dff
共有 3 个文件被更改,包括 15 次插入1 次删除
  1. 3 0
      CHANGES.txt
  2. 1 1
      src/java/org/apache/hadoop/io/BytesWritable.java
  3. 11 0
      src/test/org/apache/hadoop/io/TestBytesWritable.java

+ 3 - 0
CHANGES.txt

@@ -371,6 +371,9 @@ Trunk (unreleased changes)
     HADOOP-3046. Fix the raw comparators for Text and BytesWritables
     to use the provided length rather than recompute it. (omalley)
 
+    HADOOP-3094. Fix BytesWritable.toString to avoid extending the sign bit
+    (Owen O'Malley via cdouglas)
+
 Release 0.16.2 - Unreleased
 
   BUG FIXES

+ 1 - 1
src/java/org/apache/hadoop/io/BytesWritable.java

@@ -174,7 +174,7 @@ public class BytesWritable implements WritableComparable {
       if (idx != 0) {
         sb.append(' ');
       }
-      String num = Integer.toHexString((int) bytes[idx]);
+      String num = Integer.toHexString(0xff & bytes[idx]);
       // if it is only one digit, add a leading 0.
       if (num.length() < 2) {
         sb.append('0');

+ 11 - 0
src/test/org/apache/hadoop/io/TestBytesWritable.java

@@ -80,5 +80,16 @@ public class TestBytesWritable extends TestCase {
     assertTrue(buf[2].compareTo(buf[3]) > 0);
     assertTrue(buf[3].compareTo(buf[4]) < 0);
   }
+  
+  private void checkToString(byte[] input, String expected) {
+    String actual = new BytesWritable(input).toString();
+    assertEquals(expected, actual);
+  }
+
+  public void testToString() {
+    checkToString(new byte[]{0,1,2,0x10}, "00 01 02 10");
+    checkToString(new byte[]{-0x80, -0x7f, -0x1, -0x2, 1, 0}, 
+                  "80 81 ff fe 01 00");
+  }
 }