This is an automated email from the ASF dual-hosted git repository.

stevel pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/hadoop.git


The following commit(s) were added to refs/heads/trunk by this push:
     new b1abb10  HADOOP-17430. Restore ability to set Text to empty byte array 
(#2545)
b1abb10 is described below

commit b1abb10ea273b53896afbf766ea16a59138ce6e9
Author: dgzdot <57993550+dgz...@users.noreply.github.com>
AuthorDate: Wed Jan 6 05:09:41 2021 +0800

    HADOOP-17430. Restore ability to set Text to empty byte array (#2545)
    
    
    
    Contributed by gaozhan.ding
---
 .../src/main/java/org/apache/hadoop/io/Text.java       | 12 ++++++++++--
 .../src/test/java/org/apache/hadoop/io/TestText.java   | 18 ++++++++++++++++++
 2 files changed, 28 insertions(+), 2 deletions(-)

diff --git 
a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/Text.java
 
b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/Text.java
index 6022b99..4915100 100644
--- 
a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/Text.java
+++ 
b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/Text.java
@@ -223,10 +223,18 @@ public class Text extends BinaryComparable
   }
 
   /**
-   * Set to a utf8 byte array.
+   * Set to a utf8 byte array. If the length of <code>utf8</code> is
+   * <em>zero</em>, actually clear {@link #bytes} and any existing
+   * data is lost.
    */
   public void set(byte[] utf8) {
-    set(utf8, 0, utf8.length);
+    if (utf8.length == 0) {
+      bytes = EMPTY_BYTES;
+      length = 0;
+      textLength = -1;
+    } else {
+      set(utf8, 0, utf8.length);
+    }
   }
 
   /**
diff --git 
a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/TestText.java
 
b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/TestText.java
index a72f06f..7ae5d7d 100644
--- 
a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/TestText.java
+++ 
b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/TestText.java
@@ -459,4 +459,22 @@ public class TestText {
             2, Text.utf8Length(new String(new char[]{(char)254})));
   }
 
+  @Test
+  public void testSetBytes(){
+    Text a = new Text(new byte[100]);
+    assertEquals("testSetBytes100 getLength error !",
+            100, a.getLength());
+    assertEquals("testSetBytes100 getBytes.length error !",
+            100, a.getBytes().length);
+    assertEquals("testSetBytes100 getTextLength error !",
+            100, a.getTextLength());
+
+    a.set(new byte[0]);
+    assertEquals("testSetBytes0 getLength error !",
+            0, a.getLength());
+    assertEquals("testSetBytes0 getBytes.length error !",
+            0, a.getBytes().length);
+    assertEquals("testSetBytes0 getTextLength error !",
+            0, a.getTextLength());
+  }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: common-commits-unsubscr...@hadoop.apache.org
For additional commands, e-mail: common-commits-h...@hadoop.apache.org

Reply via email to