Ver código fonte

HADOOP-6522. Fix decoding of codepoint zero in UTF8.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/branch-0.21@908680 13f79535-47bb-0310-9956-ffa450edef68
Doug Cutting 15 anos atrás
pai
commit
8f18af8f54

+ 2 - 0
CHANGES.txt

@@ -1147,6 +1147,8 @@ Release 0.21.0 - Unreleased
     HADOOP-6290. Prevent duplicate slf4j-simple jar via Avro's classpath.
     (Owen O'Malley via cdouglas)
 
+    HADOOP-6522. Fix decoding of codepoint zero in UTF8. (cutting)
+
 Release 0.20.2 - Unreleased
 
   NEW FEATURES

+ 2 - 2
src/java/org/apache/hadoop/io/UTF8.java

@@ -253,7 +253,7 @@ public class UTF8 implements WritableComparable {
     int utf8Length = 0;
     for (int i = 0; i < stringLength; i++) {
       int c = string.charAt(i);
-      if ((c >= 0x0001) && (c <= 0x007F)) {
+      if (c <= 0x007F) {
         utf8Length++;
       } else if (c > 0x07FF) {
         utf8Length += 3;
@@ -270,7 +270,7 @@ public class UTF8 implements WritableComparable {
     final int end = start + length;
     for (int i = start; i < end; i++) {
       int code = s.charAt(i);
-      if (code >= 0x01 && code <= 0x7F) {
+      if (code <= 0x7F) {
         out.writeByte((byte)code);
       } else if (code <= 0x07FF) {
         out.writeByte((byte)(0xC0 | ((code >> 6) & 0x1F)));

+ 13 - 3
src/test/core/org/apache/hadoop/io/TestUTF8.java

@@ -22,6 +22,7 @@ import junit.framework.TestCase;
 import java.util.Random;
 
 /** Unit tests for UTF8. */
+@SuppressWarnings("deprecation")
 public class TestUTF8 extends TestCase {
   public TestUTF8(String name) { super(name); }
 
@@ -37,13 +38,13 @@ public class TestUTF8 extends TestCase {
   }
 
   public void testWritable() throws Exception {
-    for (int i = 0; i < 10; i++) {
+    for (int i = 0; i < 10000; i++) {
       TestWritable.testWritable(new UTF8(getTestString()));
     }
   }
 
   public void testGetBytes() throws Exception {
-    for (int i = 0; i < 10; i++) {
+    for (int i = 0; i < 10000; i++) {
 
       // generate a random string
       String before = getTestString();
@@ -57,7 +58,7 @@ public class TestUTF8 extends TestCase {
     DataOutputBuffer out = new DataOutputBuffer();
     DataInputBuffer in = new DataInputBuffer();
 
-    for (int i = 0; i < 10; i++) {
+    for (int i = 0; i < 10000; i++) {
       // generate a random string
       String before = getTestString();
 
@@ -82,5 +83,14 @@ public class TestUTF8 extends TestCase {
     }
 
   }
+
+  public void testNullEncoding() throws Exception {
+    String s = new String(new char[] { 0 });
+
+    DataOutputBuffer dob = new DataOutputBuffer();
+    new UTF8(s).write(dob);
+
+    assertEquals(s, new String(dob.getData(), 2, dob.getLength()-2, "UTF-8"));
+  }
 	
 }