Package com.linkedin.venice.utils
Class ByteUtils
- java.lang.Object
-
- com.linkedin.venice.utils.ByteUtils
-
public class ByteUtils extends java.lang.Object
Utility functions for munging on bytes N.B.: Most functions taken from Voldemort's ByteUtils class.
-
-
Field Summary
Fields Modifier and Type Field Description static long
BYTES_PER_GB
static int
BYTES_PER_KB
static int
BYTES_PER_MB
static int
SIZE_OF_BOOLEAN
static int
SIZE_OF_INT
static int
SIZE_OF_LONG
static int
SIZE_OF_SHORT
-
Constructor Summary
Constructors Constructor Description ByteUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static boolean
canUseBackedArray(java.nio.ByteBuffer byteBuffer)
static int
compare(byte[] left, byte[] right)
A comparator for byte arrays.static byte[]
copyByteArray(java.nio.ByteBuffer byteBuffer)
static java.nio.ByteBuffer
enlargeByteBufferForIntHeader(java.nio.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(java.nio.ByteBuffer byteBuffer)
static void
extractByteArray(java.nio.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(java.lang.String hexString)
Translate the given hexidecimal string into a byte arraystatic java.lang.String
generateHumanReadableByteCountString(long bytes)
Convert bytes to "Human-readable" output.static int
getIntHeaderFromByteBuffer(java.nio.ByteBuffer originalBuffer)
Extract an integer header from the ByteBuffer provided.static java.nio.ByteBuffer
prependIntHeaderToByteBuffer(java.nio.ByteBuffer originalBuffer, int header)
static java.nio.ByteBuffer
prependIntHeaderToByteBuffer(java.nio.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 java.lang.String
toHexString(byte[] bytes)
Translate the given byte array into a hexadecimal stringstatic java.lang.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, java.lang.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 Detail
-
BYTES_PER_KB
public static final int BYTES_PER_KB
- See Also:
- Constant Field Values
-
BYTES_PER_MB
public static final int BYTES_PER_MB
- See Also:
- Constant Field Values
-
BYTES_PER_GB
public static final long BYTES_PER_GB
- See Also:
- Constant Field Values
-
SIZE_OF_LONG
public static final int SIZE_OF_LONG
- See Also:
- Constant Field Values
-
SIZE_OF_INT
public static final int SIZE_OF_INT
- See Also:
- Constant Field Values
-
SIZE_OF_SHORT
public static final int SIZE_OF_SHORT
- See Also:
- Constant Field Values
-
SIZE_OF_BOOLEAN
public static final int SIZE_OF_BOOLEAN
- See Also:
- Constant Field Values
-
-
Method Detail
-
toHexString
public static java.lang.String toHexString(byte[] bytes)
Translate the given byte array into a hexadecimal string- Parameters:
bytes
- The bytes to translate- Returns:
- The string
-
toHexString
public static java.lang.String toHexString(byte[] bytes, int start, int len)
Translate the given byte array with specific start position and length into a hexadecimal string
-
fromHexString
public static byte[] fromHexString(java.lang.String hexString)
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
public static void writeBoolean(byte[] bytes, java.lang.Boolean value, int offset)
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
public static boolean canUseBackedArray(java.nio.ByteBuffer byteBuffer)
-
extractByteArray
public static byte[] extractByteArray(java.nio.ByteBuffer byteBuffer)
-
copyByteArray
public static byte[] copyByteArray(java.nio.ByteBuffer byteBuffer)
-
extractByteArray
public static void extractByteArray(java.nio.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.- 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
public static java.lang.String generateHumanReadableByteCountString(long bytes)
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
public static java.nio.ByteBuffer enlargeByteBufferForIntHeader(java.nio.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'- Parameters:
byteBuffer
- The ByteBuffer whose size needs to be expanded.- Returns:
-
getIntHeaderFromByteBuffer
public static int getIntHeaderFromByteBuffer(java.nio.ByteBuffer originalBuffer)
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
public static java.nio.ByteBuffer prependIntHeaderToByteBuffer(java.nio.ByteBuffer originalBuffer, int header)
-
prependIntHeaderToByteBuffer
public static java.nio.ByteBuffer prependIntHeaderToByteBuffer(java.nio.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 {@param reuseOriginalBuffer} is true, then return object is the same as original buffer.
-
-