Return-Path: Delivered-To: apmail-hadoop-common-commits-archive@www.apache.org Received: (qmail 72535 invoked from network); 31 Aug 2009 07:57:02 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 31 Aug 2009 07:57:02 -0000 Received: (qmail 17209 invoked by uid 500); 31 Aug 2009 07:57:02 -0000 Delivered-To: apmail-hadoop-common-commits-archive@hadoop.apache.org Received: (qmail 17118 invoked by uid 500); 31 Aug 2009 07:57:01 -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 17109 invoked by uid 99); 31 Aug 2009 07:57:01 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 31 Aug 2009 07:57:01 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.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; Mon, 31 Aug 2009 07:56:59 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id C4B52238889B; Mon, 31 Aug 2009 07:56:39 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r809491 - in /hadoop/common/trunk: CHANGES.txt src/java/org/apache/hadoop/io/DataOutputBuffer.java src/java/org/apache/hadoop/io/Text.java Date: Mon, 31 Aug 2009 07:56:39 -0000 To: common-commits@hadoop.apache.org From: ddas@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090831075639.C4B52238889B@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: ddas Date: Mon Aug 31 07:56:39 2009 New Revision: 809491 URL: http://svn.apache.org/viewvc?rev=809491&view=rev Log: HADOOP-6224. Adds methods to read strings safely, makes the Buffer class in DataOutputBuffer public, and introduces public constructors there. These changes are required for MAPREDUCE-318. Contributed by Jothi Padmanabhan and Arun Murthy. Modified: hadoop/common/trunk/CHANGES.txt hadoop/common/trunk/src/java/org/apache/hadoop/io/DataOutputBuffer.java hadoop/common/trunk/src/java/org/apache/hadoop/io/Text.java Modified: hadoop/common/trunk/CHANGES.txt URL: http://svn.apache.org/viewvc/hadoop/common/trunk/CHANGES.txt?rev=809491&r1=809490&r2=809491&view=diff ============================================================================== --- hadoop/common/trunk/CHANGES.txt (original) +++ hadoop/common/trunk/CHANGES.txt Mon Aug 31 07:56:39 2009 @@ -507,6 +507,10 @@ HADOOP-6184. Provide an API to dump Configuration in a JSON format. (V.V.Chaitanya Krishna via yhemanth) + + HADOOP-6224. Adds methods to read strings safely, makes the Buffer class + in DataOutputBuffer public, and introduces public constructors there. These changes + are required for MAPREDUCE-318. (Jothi Padmanabhan and Arun Murthy via ddas) OPTIMIZATIONS Modified: hadoop/common/trunk/src/java/org/apache/hadoop/io/DataOutputBuffer.java URL: http://svn.apache.org/viewvc/hadoop/common/trunk/src/java/org/apache/hadoop/io/DataOutputBuffer.java?rev=809491&r1=809490&r2=809491&view=diff ============================================================================== --- hadoop/common/trunk/src/java/org/apache/hadoop/io/DataOutputBuffer.java (original) +++ hadoop/common/trunk/src/java/org/apache/hadoop/io/DataOutputBuffer.java Mon Aug 31 07:56:39 2009 @@ -20,6 +20,8 @@ import java.io.*; +import org.apache.hadoop.io.DataOutputBuffer.Buffer; + /** A reusable {@link DataOutput} implementation that writes to an in-memory * buffer. * @@ -41,7 +43,7 @@ */ public class DataOutputBuffer extends DataOutputStream { - private static class Buffer extends ByteArrayOutputStream { + public static class Buffer extends ByteArrayOutputStream { public byte[] getData() { return buf; } public int getLength() { return count; } @@ -53,6 +55,10 @@ super(size); } + public Buffer(byte[] buf) { + super.buf = buf; + } + public void write(DataInput in, int len) throws IOException { int newcount = count + len; if (newcount > buf.length) { @@ -76,6 +82,10 @@ this(new Buffer(size)); } + public DataOutputBuffer(byte[] buf) { + this(new Buffer(buf)); + } + private DataOutputBuffer(Buffer buffer) { super(buffer); this.buffer = buffer; Modified: hadoop/common/trunk/src/java/org/apache/hadoop/io/Text.java URL: http://svn.apache.org/viewvc/hadoop/common/trunk/src/java/org/apache/hadoop/io/Text.java?rev=809491&r1=809490&r2=809491&view=diff ============================================================================== --- hadoop/common/trunk/src/java/org/apache/hadoop/io/Text.java (original) +++ hadoop/common/trunk/src/java/org/apache/hadoop/io/Text.java Mon Aug 31 07:56:39 2009 @@ -35,6 +35,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.hadoop.io.WritableUtils; /** This class stores text using standard UTF8 encoding. It provides methods * to serialize, deserialize, and compare texts at byte level. The type of @@ -403,6 +404,30 @@ in.readFully(bytes, 0, length); return decode(bytes); } + /** + * Read a string, but check it for sanity. The format consists of a vint + * followed by the given number of bytes. + * @param in the stream to read from + * @param maxLength the largest acceptable length of string + * @return the bytes as a string + * @throws IOException if reading from the DataInput fails + * @throws IllegalArgumentException if the string length is negative or + * larger than maxSize. Only the vint is read. + */ + public static String readStringSafely(DataInput in, + int maxLength + ) throws IOException, + IllegalArgumentException { + int length = WritableUtils.readVInt(in); + if (length < 0 || length > maxLength) { + throw new IllegalArgumentException("String size was " + length + + ", which is outside of 0.." + + maxLength); + } + byte [] bytes = new byte[length]; + in.readFully(bytes, 0, length); + return decode(bytes); + } /** Write a UTF8 encoded string to out */