From 9ea11c5fb33c30d1578e21dcf8992642013e7c76 Mon Sep 17 00:00:00 2001 From: Christopher Douglas Date: Wed, 2 Sep 2009 07:07:21 +0000 Subject: [PATCH] HADOOP-6224. Add a method to WritableUtils supported a bounded read of an encoded String. Contributed by Jothi Padmanabhan git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@810384 13f79535-47bb-0310-9956-ffa450edef68 --- CHANGES.txt | 5 +++- .../org/apache/hadoop/io/WritableUtils.java | 25 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/CHANGES.txt b/CHANGES.txt index 97494d6e3b..f5a660da13 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -507,7 +507,10 @@ Trunk (unreleased changes) 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 diff --git a/src/java/org/apache/hadoop/io/WritableUtils.java b/src/java/org/apache/hadoop/io/WritableUtils.java index e49ea9240c..fa6736f1da 100644 --- a/src/java/org/apache/hadoop/io/WritableUtils.java +++ b/src/java/org/apache/hadoop/io/WritableUtils.java @@ -415,4 +415,29 @@ public static byte[] toByteArray(Writable... writables) { } 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); + } }