Class ByteUtils


  • public class ByteUtils
    extends java.lang.Object
    Utility functions for munging on bytes N.B.: Most functions taken from Voldemort's ByteUtils class.
    • 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 array
      static 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 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 java.lang.String toHexString​(byte[] bytes)
      Translate the given byte array into a hexadecimal string
      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
      static void writeBoolean​(byte[] bytes, java.lang.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
    • Constructor Detail

      • ByteUtils

        public ByteUtils()
    • 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 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,
                                        java.lang.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​(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 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 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.