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