瀏覽代碼

HADOOP-982. Add some setters and a toString() method to BytesWritable. Contributed by Owen.

git-svn-id: https://svn.apache.org/repos/asf/lucene/hadoop/trunk@504680 13f79535-47bb-0310-9956-ffa450edef68
Doug Cutting 18 年之前
父節點
當前提交
253ed92e7b
共有 2 個文件被更改,包括 44 次插入1 次删除
  1. 3 0
      CHANGES.txt
  2. 41 1
      src/java/org/apache/hadoop/io/BytesWritable.java

+ 3 - 0
CHANGES.txt

@@ -6,6 +6,9 @@ Trunk (unreleased changes)
  1. HADOOP-975.  Separate stdout and stderr from tasks.
     (Arun C Murthy via cutting)
 
+ 2. HADOOP-982.  Add some setters and a toString() method to
+    BytesWritable.  (omalley via cutting)
+
 
 Branch 0.11 - unreleased
 

+ 41 - 1
src/java/org/apache/hadoop/io/BytesWritable.java

@@ -104,7 +104,27 @@ public class BytesWritable implements WritableComparable {
       bytes = new_data;
     }
   }
-  
+
+  /**
+   * Set the BytesWritable to the contents of the given newData.
+   * @param newData the value to set this BytesWritable to.
+   */
+  public void set(BytesWritable newData) {
+    set(newData.bytes, 0, newData.size);
+  }
+
+  /**
+   * Set the value to a copy of the given byte range
+   * @param newData the new values to copy in
+   * @param offset the offset in newData to start at
+   * @param length the number of bytes to copy
+   */
+  public void set(byte[] newData, int offset, int length) {
+    setSize(0);
+    setSize(length);
+    System.arraycopy(newData, 0, bytes, 0, size);
+  }
+
   // inherit javadoc
   public void readFields(DataInput in) throws IOException {
     setSize(0); // clear the old data
@@ -144,6 +164,26 @@ public class BytesWritable implements WritableComparable {
     return false;
   }
   
+  /**
+   * Generate the stream of bytes as hex pairs separated by ' '.
+   */
+  public String toString() { 
+    StringBuffer sb = new StringBuffer(3*size);
+    for (int idx = 0; idx < size; idx++) {
+      // if not the first, put a blank separator in
+      if (idx != 0) {
+        sb.append(' ');
+      }
+      String num = Integer.toHexString((int) bytes[idx]);
+      // if it is only one digit, add a leading 0.
+      if (num.length() < 2) {
+        sb.append('0');
+      }
+      sb.append(num);
+    }
+    return sb.toString();
+  }
+
   /** A Comparator optimized for BytesWritable. */ 
   public static class Comparator extends WritableComparator {
     public Comparator() {