Bläddra i källkod

HADOOP-302. Fix a bug in Text.toString(). Contributed by Hairong.

git-svn-id: https://svn.apache.org/repos/asf/lucene/hadoop/trunk@427234 13f79535-47bb-0310-9956-ffa450edef68
Doug Cutting 19 år sedan
förälder
incheckning
4db4f4fc00
1 ändrade filer med 8 tillägg och 3 borttagningar
  1. 8 3
      src/java/org/apache/hadoop/io/Text.java

+ 8 - 3
src/java/org/apache/hadoop/io/Text.java

@@ -208,7 +208,7 @@ public class Text implements WritableComparable {
    */
   public String toString() {
     try {
-      return decode(bytes);
+      return decode(bytes, 0, length);
     } catch (CharacterCodingException e) { 
       //bytes is supposed to contain valid utf8, therefore, 
       // this should never happen
@@ -308,6 +308,11 @@ public class Text implements WritableComparable {
     return decode(ByteBuffer.wrap(utf8), false);
   }
   
+  public static String decode(byte[] utf8, int start, int length) 
+      throws CharacterCodingException {
+      return decode(ByteBuffer.wrap(utf8, start, length), false);
+  }
+  
   /**
    * Converts the provided byte array to a String using the
    * UTF-8 encoding. If <code>replace</code> is true, then
@@ -315,9 +320,9 @@ public class Text implements WritableComparable {
    * substitution character, which is U+FFFD. Otherwise the
    * method throws a MalformedInputException.
    */
-  public static String decode(byte[] utf8, boolean replace) 
+  public static String decode(byte[] utf8, int start, int length, boolean replace) 
     throws CharacterCodingException {
-    return decode(ByteBuffer.wrap(utf8), replace);
+    return decode(ByteBuffer.wrap(utf8, start, length), replace);
   }
   
   private static String decode(ByteBuffer utf8, boolean replace)