hadoop-common-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ji...@apache.org
Subject svn commit: r1579814 - /hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/DataOutputBuffer.java
Date Thu, 20 Mar 2014 23:08:34 GMT
Author: jing9
Date: Thu Mar 20 23:08:32 2014
New Revision: 1579814

URL: http://svn.apache.org/r1579814
Log:
HDFS-6038. Merge r1579813 from trunk.

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

Modified: hadoop/common/branches/branch-2/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/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/DataOutputBuffer.java?rev=1579814&r1=1579813&r2=1579814&view=diff
==============================================================================
--- hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/DataOutputBuffer.java
(original)
+++ hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/DataOutputBuffer.java
Thu Mar 20 23:08:32 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);
+  }
 }



Mime
View raw message