Return-Path: X-Original-To: apmail-hadoop-common-commits-archive@www.apache.org Delivered-To: apmail-hadoop-common-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 5477310286 for ; Thu, 20 Mar 2014 23:11:27 +0000 (UTC) Received: (qmail 93405 invoked by uid 500); 20 Mar 2014 23:11:25 -0000 Delivered-To: apmail-hadoop-common-commits-archive@hadoop.apache.org Received: (qmail 93335 invoked by uid 500); 20 Mar 2014 23:11:24 -0000 Mailing-List: contact common-commits-help@hadoop.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: common-dev@hadoop.apache.org Delivered-To: mailing list common-commits@hadoop.apache.org Received: (qmail 93324 invoked by uid 99); 20 Mar 2014 23:11:24 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 20 Mar 2014 23:11:24 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 20 Mar 2014 23:11:23 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 1F06623889E0; Thu, 20 Mar 2014 23:11:03 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1579815 - /hadoop/common/branches/branch-2.4/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/DataOutputBuffer.java Date: Thu, 20 Mar 2014 23:11:03 -0000 To: common-commits@hadoop.apache.org From: jing9@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20140320231103.1F06623889E0@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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); + } }