Package org.apache.hadoop.io
Class WritableUtils
java.lang.Object
org.apache.hadoop.io.WritableUtils
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends Writable>
Tclone(T orig, Configuration conf) Make a copy of a writable object using serialization to a buffer.static voidDeprecated.use ReflectionUtils.cloneInto instead.static intdecodeVIntSize(byte value) Parse the first byte of a vint/vlong to determine the number of bytesstatic voiddisplayByteArray(byte[] record) static intgetVIntSize(long i) Get the encoded length if an integer is stored in a variable-length format.static booleanisNegativeVInt(byte value) Given the first byte of a vint/vlong, determine the signstatic byte[]static Stringstatic String[]static <T extends Enum<T>>
TRead an Enum value from DataInput, Enums are read and written using String values.static StringreadString(DataInput in) static String[]static StringreadStringSafely(DataInput in, int maxLength) Read a string, but check it for sanity.static intReads a zero-compressed encoded integer from input stream and returns it.static intreadVIntInRange(DataInput stream, int lower, int upper) Reads an integer from the input stream and returns it.static longReads a zero-compressed encoded long from input stream and returns it.static voidstatic voidSkip len number of bytes in input streaminstatic byte[]toByteArray(Writable... writables) Convert writables to a byte array.static intwriteCompressedByteArray(DataOutput out, byte[] bytes) static intwriteCompressedString(DataOutput out, String s) static voidwriteCompressedStringArray(DataOutput out, String[] s) static voidwriteEnum(DataOutput out, Enum<?> enumVal) writes String value of enum to DataOutput.static voidwriteString(DataOutput out, String s) static voidwriteStringArray(DataOutput out, String[] s) static voidwriteVInt(DataOutput stream, int i) Serializes an integer to a binary stream with zero-compressed encoding.static voidwriteVLong(DataOutput stream, long i) Serializes a long to a binary stream with zero-compressed encoding.
-
Constructor Details
-
WritableUtils
public WritableUtils()
-
-
Method Details
-
readCompressedByteArray
- Throws:
IOException
-
skipCompressedByteArray
- Throws:
IOException
-
writeCompressedByteArray
- Throws:
IOException
-
readCompressedString
- Throws:
IOException
-
writeCompressedString
- Throws:
IOException
-
writeString
- Throws:
IOException
-
readString
- Throws:
IOException
-
writeStringArray
- Throws:
IOException
-
writeCompressedStringArray
- Throws:
IOException
-
readStringArray
- Throws:
IOException
-
readCompressedStringArray
- Throws:
IOException
-
displayByteArray
public static void displayByteArray(byte[] record) -
clone
Make a copy of a writable object using serialization to a buffer.- Type Parameters:
T- Generics Type T.- Parameters:
orig- The object to copyconf- input Configuration.- Returns:
- The copied object
-
cloneInto
Deprecated.use ReflectionUtils.cloneInto instead.Make a copy of the writable object using serialization to a buffer.- Parameters:
dst- the object to copy fromsrc- the object to copy into, which is destroyed- Throws:
IOException- raised on errors performing I/O.
-
writeVInt
Serializes an integer to a binary stream with zero-compressed encoding. For -112 <= i <= 127, only one byte is used with the actual value. For other values of i, the first byte value indicates whether the integer is positive or negative, and the number of bytes that follow. If the first byte value v is between -113 and -116, the following integer is positive, with number of bytes that follow are -(v+112). If the first byte value v is between -121 and -124, the following integer is negative, with number of bytes that follow are -(v+120). Bytes are stored in the high-non-zero-byte-first order.- Parameters:
stream- Binary output streami- Integer to be serialized- Throws:
IOException- raised on errors performing I/O.
-
writeVLong
Serializes a long to a binary stream with zero-compressed encoding. For -112 <= i <= 127, only one byte is used with the actual value. For other values of i, the first byte value indicates whether the long is positive or negative, and the number of bytes that follow. If the first byte value v is between -113 and -120, the following long is positive, with number of bytes that follow are -(v+112). If the first byte value v is between -121 and -128, the following long is negative, with number of bytes that follow are -(v+120). Bytes are stored in the high-non-zero-byte-first order.- Parameters:
stream- Binary output streami- Long to be serialized- Throws:
IOException- raised on errors performing I/O.
-
readVLong
Reads a zero-compressed encoded long from input stream and returns it.- Parameters:
stream- Binary input stream- Returns:
- deserialized long from stream.
- Throws:
IOException- raised on errors performing I/O.
-
readVInt
Reads a zero-compressed encoded integer from input stream and returns it.- Parameters:
stream- Binary input stream- Returns:
- deserialized integer from stream.
- Throws:
IOException- raised on errors performing I/O.
-
readVIntInRange
Reads an integer from the input stream and returns it. This function validates that the integer is between [lower, upper], inclusive.- Parameters:
stream- Binary input streamlower- input lower.upper- input upper.- Returns:
- deserialized integer from stream.
- Throws:
IOException- raised on errors performing I/O.
-
isNegativeVInt
public static boolean isNegativeVInt(byte value) Given the first byte of a vint/vlong, determine the sign- Parameters:
value- the first byte- Returns:
- is the value negative
-
decodeVIntSize
public static int decodeVIntSize(byte value) Parse the first byte of a vint/vlong to determine the number of bytes- Parameters:
value- the first byte of the vint/vlong- Returns:
- the total number of bytes (1 to 9)
-
getVIntSize
public static int getVIntSize(long i) Get the encoded length if an integer is stored in a variable-length format.- Parameters:
i- input i.- Returns:
- the encoded length
-
readEnum
Read an Enum value from DataInput, Enums are read and written using String values.- Type Parameters:
T- Enum type- Parameters:
in- DataInput to read fromenumType- Class type of Enum- Returns:
- Enum represented by String read from DataInput
- Throws:
IOException- raised on errors performing I/O.
-
writeEnum
writes String value of enum to DataOutput.- Parameters:
out- Dataoutput streamenumVal- enum value- Throws:
IOException- raised on errors performing I/O.
-
skipFully
Skip len number of bytes in input streamin- Parameters:
in- input streamlen- number of bytes to skip- Throws:
IOException- when skipped less number of bytes
-
toByteArray
Convert writables to a byte array.- Parameters:
writables- input writables.- Returns:
- ByteArray.
-
readStringSafely
public static String readStringSafely(DataInput in, int maxLength) throws IOException, IllegalArgumentException Read a string, but check it for sanity. The format consists of a vint followed by the given number of bytes.- Parameters:
in- the stream to read frommaxLength- the largest acceptable length of the encoded string- Returns:
- the bytes as a string
- Throws:
IOException- if reading from the DataInput failsIllegalArgumentException- if the encoded byte size for string is negative or larger than maxSize. Only the vint is read.
-