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
This commit is contained in:
Christopher Douglas 2009-09-02 07:07:21 +00:00
parent faf9fe98ae
commit 9ea11c5fb3
2 changed files with 29 additions and 1 deletions

View File

@ -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

View File

@ -415,4 +415,29 @@ public final class WritableUtils {
}
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);
}
}