Package com.linkedin.venice.utils
Class ByteUtils
java.lang.Object
com.linkedin.venice.utils.ByteUtils
Utility functions for munging on bytes
N.B.: Most functions taken from Voldemort's ByteUtils class.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final long
static final int
static final int
static final int
static final int
static final int
static final int
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
canUseBackedArray
(ByteBuffer byteBuffer) static int
compare
(byte[] left, byte[] right) A comparator for byte arrays.static byte[]
copyByteArray
(ByteBuffer byteBuffer) static ByteBuffer
enlargeByteBufferForIntHeader
(ByteBuffer byteBuffer) This function is to simulate the deserialization logic in com.linkedin.venice.serialization.avro.OptimizedKafkaValueSerializer to leave some room at the beginning of byte buffer of 'byteBuffer'static byte[]
extractByteArray
(ByteBuffer byteBuffer) static void
extractByteArray
(ByteBuffer src, byte[] destination, int offset, int length) Extract the data in the ByteBuffer into the destination array by copying "length" bytes from the source buffer into the given array, starting at the current position of the source buffer and at the given offset in the destination array.static byte[]
fromHexString
(String hexString) Translate the given hexidecimal string into a byte arraystatic String
generateHumanReadableByteCountString
(long bytes) Convert bytes to "Human-readable" output.static int
getIntHeaderFromByteBuffer
(ByteBuffer originalBuffer) Extract an integer header from the ByteBuffer provided.static ByteBuffer
prependIntHeaderToByteBuffer
(ByteBuffer originalBuffer, int header) static ByteBuffer
prependIntHeaderToByteBuffer
(ByteBuffer originalBuffer, int header, boolean reuseOriginalBuffer) This function will return a ByteBuffer that has the integer prepended as a header from the current position.static boolean
readBoolean
(byte[] bytes, int offset) Read a boolean from the byte array starting at the given offsetstatic int
readInt
(byte[] bytes, int offset) Read an int from the byte array starting at the given offsetstatic long
readLong
(byte[] bytes, int offset) Read a long from the byte array starting at the given offsetstatic short
readShort
(byte[] bytes, int offset) Read a short from the byte array starting at the given offsetstatic String
toHexString
(byte[] bytes) Translate the given byte array into a hexadecimal stringstatic String
toHexString
(byte[] bytes, int start, int len) Translate the given byte array with specific start position and length into a hexadecimal stringstatic void
writeBoolean
(byte[] bytes, Boolean value, int offset) Write a boolean to the byte array starting at the given offsetstatic void
writeInt
(byte[] bytes, int value, int offset) Write an int to the byte array starting at the given offsetstatic void
writeLong
(byte[] bytes, long value, int offset) Write a long to the byte array starting at the given offsetstatic void
writeShort
(byte[] bytes, short value, int offset) Write a short to the byte array starting at the given offset
-
Field Details
-
BYTES_PER_KB
public static final int BYTES_PER_KB- See Also:
-
BYTES_PER_MB
public static final int BYTES_PER_MB- See Also:
-
BYTES_PER_GB
public static final long BYTES_PER_GB- See Also:
-
SIZE_OF_LONG
public static final int SIZE_OF_LONG- See Also:
-
SIZE_OF_INT
public static final int SIZE_OF_INT- See Also:
-
SIZE_OF_SHORT
public static final int SIZE_OF_SHORT- See Also:
-
SIZE_OF_BOOLEAN
public static final int SIZE_OF_BOOLEAN- See Also:
-
-
Constructor Details
-
ByteUtils
public ByteUtils()
-
-
Method Details
-
toHexString
Translate the given byte array into a hexadecimal string- Parameters:
bytes
- The bytes to translate- Returns:
- The string
-
toHexString
Translate the given byte array with specific start position and length into a hexadecimal string -
fromHexString
Translate the given hexidecimal string into a byte array- Parameters:
hexString
- The hex string to translate- Returns:
- The bytes
- Throws:
org.apache.commons.codec.DecoderException
-
writeLong
public static void writeLong(byte[] bytes, long value, int offset) Write a long to the byte array starting at the given offset- Parameters:
bytes
- The byte arrayvalue
- The long to writeoffset
- The offset to begin writing at
-
readLong
public static long readLong(byte[] bytes, int offset) Read a long from the byte array starting at the given offset- Parameters:
bytes
- The byte array to read fromoffset
- The offset to start reading at- Returns:
- The long read
-
writeInt
public static void writeInt(byte[] bytes, int value, int offset) Write an int to the byte array starting at the given offset- Parameters:
bytes
- The byte arrayvalue
- The int to writeoffset
- The offset to begin writing at
-
readInt
public static int readInt(byte[] bytes, int offset) Read an int from the byte array starting at the given offset- Parameters:
bytes
- The byte array to read fromoffset
- The offset to start reading at- Returns:
- The int read
-
writeShort
public static void writeShort(byte[] bytes, short value, int offset) Write a short to the byte array starting at the given offset- Parameters:
bytes
- The byte arrayvalue
- The short to writeoffset
- The offset to begin writing at
-
readShort
public static short readShort(byte[] bytes, int offset) Read a short from the byte array starting at the given offset- Parameters:
bytes
- The byte array to read fromoffset
- The offset to start reading at- Returns:
- The short read
-
writeBoolean
Write a boolean to the byte array starting at the given offset- Parameters:
bytes
- The byte arrayvalue
- The boolean to writeoffset
- The offset to begin writing at
-
readBoolean
public static boolean readBoolean(byte[] bytes, int offset) Read a boolean from the byte array starting at the given offset- Parameters:
bytes
- The byte array to read fromoffset
- The offset to start reading at- Returns:
- The boolean read
-
compare
public static int compare(byte[] left, byte[] right) A comparator for byte arrays. Taken from: https://stackoverflow.com/a/5108711/791758 (and originally coming for Apache HBase) -
canUseBackedArray
-
extractByteArray
-
copyByteArray
-
extractByteArray
Extract the data in the ByteBuffer into the destination array by copying "length" bytes from the source buffer into the given array, starting at the current position of the source buffer and at the given offset in the destination array.- Parameters:
src
- The src buffer to copy data fromdestination
- The destination array to copy data intooffset
- The position in destination where to start copying tolength
- The number of bytes to copy
-
generateHumanReadableByteCountString
Convert bytes to "Human-readable" output. Uses unit suffixes: B, KiB, MiB, GiB, TiB and PiB. Negative values are handled for completeness and it is the responsibility of the caller to validate the inputs.- Parameters:
bytes
- the value to be converted to "Human-readable" output- Returns:
- "Human-readable" output of the input byte count
-
enlargeByteBufferForIntHeader
This function is to simulate the deserialization logic in com.linkedin.venice.serialization.avro.OptimizedKafkaValueSerializer to leave some room at the beginning of byte buffer of 'byteBuffer'- Parameters:
byteBuffer
- The ByteBuffer whose size needs to be expanded.- Returns:
-
getIntHeaderFromByteBuffer
Extract an integer header from the ByteBuffer provided. The header is extracted from the bytes immediately preceding the current position.- Parameters:
originalBuffer
- The buffer which contains the header.- Returns:
- The integer header that is extracted from the ByteBuffer provided.
-
prependIntHeaderToByteBuffer
-
prependIntHeaderToByteBuffer
public static ByteBuffer prependIntHeaderToByteBuffer(ByteBuffer originalBuffer, int header, boolean reuseOriginalBuffer) This function will return a ByteBuffer that has the integer prepended as a header from the current position. The position of the buffer will be the same as that of the input.- Parameters:
originalBuffer
- The buffer to which the header should be prepended.header
- The header value to prepend to the buffer provided.reuseOriginalBuffer
- If the original ByteBuffer should be reused.- Returns:
- The ByteBuffer that has the header prepended. If is true, then return object is the same as original buffer.
-