File tree Expand file tree Collapse file tree 2 files changed +28
-2
lines changed
hadoop-common-project/hadoop-common/src
main/java/org/apache/hadoop/io
test/java/org/apache/hadoop/io Expand file tree Collapse file tree 2 files changed +28
-2
lines changed Original file line number Diff line number Diff line change @@ -223,10 +223,18 @@ public void set(String string) {
223
223
}
224
224
225
225
/**
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.
227
229
*/
228
230
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
+ }
230
238
}
231
239
232
240
/**
Original file line number Diff line number Diff line change @@ -459,4 +459,22 @@ public void testUtf8Length() {
459
459
2 , Text .utf8Length (new String (new char []{(char )254 })));
460
460
}
461
461
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
+ }
462
480
}
You can’t perform that action at this time.
0 commit comments