You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-commits@hadoop.apache.org by ji...@apache.org on 2014/03/21 00:11:03 UTC

svn commit: r1579815 - /hadoop/common/branches/branch-2.4/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/DataOutputBuffer.java

Author: jing9
Date: Thu Mar 20 23:11:01 2014
New Revision: 1579815

URL: http://svn.apache.org/r1579815
Log:
HDFS-6038. Merge r1579814 from branch-2.

Modified:
    hadoop/common/branches/branch-2.4/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/DataOutputBuffer.java

Modified: hadoop/common/branches/branch-2.4/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/DataOutputBuffer.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-2.4/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/DataOutputBuffer.java?rev=1579815&r1=1579814&r2=1579815&view=diff
==============================================================================
--- hadoop/common/branches/branch-2.4/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/DataOutputBuffer.java (original)
+++ hadoop/common/branches/branch-2.4/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/DataOutputBuffer.java Thu Mar 20 23:11:01 2014
@@ -23,6 +23,8 @@ import java.io.*;
 import org.apache.hadoop.classification.InterfaceAudience;
 import org.apache.hadoop.classification.InterfaceStability;
 
+import com.google.common.base.Preconditions;
+
 /** A reusable {@link DataOutput} implementation that writes to an in-memory
  * buffer.
  *
@@ -68,6 +70,18 @@ public class DataOutputBuffer extends Da
       in.readFully(buf, count, len);
       count = newcount;
     }
+
+    /**
+     * Set the count for the current buf.
+     * @param newCount the new count to set
+     * @return the original count
+     */
+    private int setCount(int newCount) {
+      Preconditions.checkArgument(newCount >= 0 && newCount <= buf.length);
+      int oldCount = count;
+      count = newCount;
+      return oldCount;
+    }
   }
 
   private Buffer buffer;
@@ -110,4 +124,21 @@ public class DataOutputBuffer extends Da
   public void writeTo(OutputStream out) throws IOException {
     buffer.writeTo(out);
   }
+
+  /**
+   * Overwrite an integer into the internal buffer. Note that this call can only
+   * be used to overwrite existing data in the buffer, i.e., buffer#count cannot
+   * be increased, and DataOutputStream#written cannot be increased.
+   */
+  public void writeInt(int v, int offset) throws IOException {
+    Preconditions.checkState(offset + 4 <= buffer.getLength());
+    byte[] b = new byte[4];
+    b[0] = (byte) ((v >>> 24) & 0xFF);
+    b[1] = (byte) ((v >>> 16) & 0xFF);
+    b[2] = (byte) ((v >>> 8) & 0xFF);
+    b[3] = (byte) ((v >>> 0) & 0xFF);
+    int oldCount = buffer.setCount(offset);
+    buffer.write(b);
+    buffer.setCount(oldCount);
+  }
 }