Skip to content

Commit b1abb10

Browse files
authored
HADOOP-17430. Restore ability to set Text to empty byte array (#2545)
Contributed by gaozhan.ding
1 parent 42eb9ff commit b1abb10

File tree

2 files changed

+28
-2
lines changed
  • hadoop-common-project/hadoop-common/src

2 files changed

+28
-2
lines changed

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/Text.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,18 @@ public void set(String string) {
223223
}
224224

225225
/**
226-
* Set to a utf8 byte array.
226+
* Set to a utf8 byte array. If the length of <code>utf8</code> is
227+
* <em>zero</em>, actually clear {@link #bytes} and any existing
228+
* data is lost.
227229
*/
228230
public void set(byte[] utf8) {
229-
set(utf8, 0, utf8.length);
231+
if (utf8.length == 0) {
232+
bytes = EMPTY_BYTES;
233+
length = 0;
234+
textLength = -1;
235+
} else {
236+
set(utf8, 0, utf8.length);
237+
}
230238
}
231239

232240
/**

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/TestText.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,4 +459,22 @@ public void testUtf8Length() {
459459
2, Text.utf8Length(new String(new char[]{(char)254})));
460460
}
461461

462+
@Test
463+
public void testSetBytes(){
464+
Text a = new Text(new byte[100]);
465+
assertEquals("testSetBytes100 getLength error !",
466+
100, a.getLength());
467+
assertEquals("testSetBytes100 getBytes.length error !",
468+
100, a.getBytes().length);
469+
assertEquals("testSetBytes100 getTextLength error !",
470+
100, a.getTextLength());
471+
472+
a.set(new byte[0]);
473+
assertEquals("testSetBytes0 getLength error !",
474+
0, a.getLength());
475+
assertEquals("testSetBytes0 getBytes.length error !",
476+
0, a.getBytes().length);
477+
assertEquals("testSetBytes0 getTextLength error !",
478+
0, a.getTextLength());
479+
}
462480
}

0 commit comments

Comments
 (0)