|
@@ -35,6 +35,7 @@ import java.util.Arrays;
|
|
|
|
|
|
import org.apache.commons.logging.Log;
|
|
import org.apache.commons.logging.Log;
|
|
import org.apache.commons.logging.LogFactory;
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
+import org.apache.hadoop.io.WritableUtils;
|
|
|
|
|
|
/** This class stores text using standard UTF8 encoding. It provides methods
|
|
/** This class stores text using standard UTF8 encoding. It provides methods
|
|
* to serialize, deserialize, and compare texts at byte level. The type of
|
|
* to serialize, deserialize, and compare texts at byte level. The type of
|
|
@@ -403,6 +404,30 @@ public class Text extends BinaryComparable
|
|
in.readFully(bytes, 0, length);
|
|
in.readFully(bytes, 0, length);
|
|
return decode(bytes);
|
|
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
|
|
/** Write a UTF8 encoded string to out
|
|
*/
|
|
*/
|