hadoop-common-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cdoug...@apache.org
Subject svn commit: r810384 - in /hadoop/common/trunk: CHANGES.txt src/java/org/apache/hadoop/io/WritableUtils.java
Date Wed, 02 Sep 2009 07:07:21 GMT
Author: cdouglas
Date: Wed Sep  2 07:07:21 2009
New Revision: 810384

URL: http://svn.apache.org/viewvc?rev=810384&view=rev
Log:
HADOOP-6224. Add a method to WritableUtils supported a bounded read of an
encoded String. Contributed by Jothi Padmanabhan

Modified:
    hadoop/common/trunk/CHANGES.txt
    hadoop/common/trunk/src/java/org/apache/hadoop/io/WritableUtils.java

Modified: hadoop/common/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/CHANGES.txt?rev=810384&r1=810383&r2=810384&view=diff
==============================================================================
--- hadoop/common/trunk/CHANGES.txt (original)
+++ hadoop/common/trunk/CHANGES.txt Wed Sep  2 07:07:21 2009
@@ -507,7 +507,10 @@
 
     HADOOP-6184. Provide an API to dump Configuration in a JSON format.
     (V.V.Chaitanya Krishna via yhemanth)
- 
+
+    HADOOP-6224. Add a method to WritableUtils performing a bounded read of an
+    encoded String. (Jothi Padmanabhan via cdouglas)
+
   OPTIMIZATIONS
 
     HADOOP-5595. NameNode does not need to run a replicator to choose a

Modified: hadoop/common/trunk/src/java/org/apache/hadoop/io/WritableUtils.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/src/java/org/apache/hadoop/io/WritableUtils.java?rev=810384&r1=810383&r2=810384&view=diff
==============================================================================
--- hadoop/common/trunk/src/java/org/apache/hadoop/io/WritableUtils.java (original)
+++ hadoop/common/trunk/src/java/org/apache/hadoop/io/WritableUtils.java Wed Sep  2 07:07:21
2009
@@ -415,4 +415,29 @@
     }
     return out.getData();
   }
+
+  /**
+   * 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 the encoded string
+   * @return the bytes as a string
+   * @throws IOException if reading from the DataInput fails
+   * @throws IllegalArgumentException if the encoded byte size for string 
+             is negative or larger than maxSize. Only the vint is read.
+   */
+  public static String readStringSafely(DataInput in,
+                                        int maxLength
+                                        ) throws IOException, 
+                                                 IllegalArgumentException {
+    int length = readVInt(in);
+    if (length < 0 || length > maxLength) {
+      throw new IllegalArgumentException("Encoded byte size for String was " + length + 
+                                         ", which is outside of 0.." +
+                                         maxLength + " range.");
+    }
+    byte [] bytes = new byte[length];
+    in.readFully(bytes, 0, length);
+    return Text.decode(bytes);
+  }
 }



Mime
View raw message