Class ByteUtils

java.lang.Object
com.linkedin.venice.utils.ByteUtils

public class ByteUtils extends 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 final long
     
    static final int
     
    static final int
     
    static final int
     
    static final int
     
    static final int
     
    static final int
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
     
    static int
    compare(byte[] left, byte[] right)
    A comparator for byte arrays.
    static byte[]
     
    static 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[]
     
    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 array
    static String
    Convert bytes to "Human-readable" output.
    static int
    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 offset
    static int
    readInt(byte[] bytes, int offset)
    Read an int from the byte array starting at the given offset
    static long
    readLong(byte[] bytes, int offset)
    Read a long from the byte array starting at the given offset
    static short
    readShort(byte[] bytes, int offset)
    Read a short from the byte array starting at the given offset
    static String
    toHexString(byte[] bytes)
    Translate the given byte array into a hexadecimal string
    static String
    toHexString(byte[] bytes, int start, int len)
    Translate the given byte array with specific start position and length into a hexadecimal string
    static void
    writeBoolean(byte[] bytes, Boolean value, int offset)
    Write a boolean to the byte array starting at the given offset
    static void
    writeInt(byte[] bytes, int value, int offset)
    Write an int to the byte array starting at the given offset
    static void
    writeLong(byte[] bytes, long value, int offset)
    Write a long to the byte array starting at the given offset
    static void
    writeShort(byte[] bytes, short value, int offset)
    Write a short to the byte array starting at the given offset

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • ByteUtils

      public ByteUtils()
  • Method Details

    • toHexString

      public static 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 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(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 array
      value - The long to write
      offset - 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 from
      offset - 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 array
      value - The int to write
      offset - 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 from
      offset - 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 array
      value - The short to write
      offset - 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 from
      offset - The offset to start reading at
      Returns:
      The short read
    • writeBoolean

      public static void writeBoolean(byte[] bytes, Boolean value, int offset)
      Write a boolean to the byte array starting at the given offset
      Parameters:
      bytes - The byte array
      value - The boolean to write
      offset - 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 from
      offset - 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(ByteBuffer byteBuffer)
    • extractByteArray

      public static byte[] extractByteArray(ByteBuffer byteBuffer)
    • copyByteArray

      public static byte[] copyByteArray(ByteBuffer byteBuffer)
    • extractByteArray

      public 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.
      Parameters:
      src - The src buffer to copy data from
      destination - The destination array to copy data into
      offset - The position in destination where to start copying to
      length - The number of bytes to copy
    • generateHumanReadableByteCountString

      public static 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 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'
      Parameters:
      byteBuffer - The ByteBuffer whose size needs to be expanded.
      Returns:
    • getIntHeaderFromByteBuffer

      public static int getIntHeaderFromByteBuffer(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 ByteBuffer prependIntHeaderToByteBuffer(ByteBuffer originalBuffer, int header)
    • 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.