Class BytesUtils


  • public class BytesUtils
    extends Object
    utility methods to deal with bytes
    • Constructor Detail

      • BytesUtils

        public BytesUtils()
    • Method Detail

      • getWidthFromMaxInt

        public static int getWidthFromMaxInt​(int bound)
        give the number of bits needed to encode an int given the max value
        Parameters:
        bound - max int that we want to encode
        Returns:
        the number of bits required
      • readIntLittleEndian

        public static int readIntLittleEndian​(ByteBuffer in,
                                              int offset)
                                       throws IOException
        reads an int in little endian at the given position
        Parameters:
        in - a byte buffer
        offset - an offset into the byte buffer
        Returns:
        the integer at position offset read using little endian byte order
        Throws:
        IOException - if there is an exception reading from the byte buffer
      • readIntLittleEndian

        public static int readIntLittleEndian​(byte[] in,
                                              int offset)
                                       throws IOException
        reads an int in little endian at the given position
        Parameters:
        in - a byte array
        offset - an offset into the byte array
        Returns:
        the integer at position offset read using little endian byte order
        Throws:
        IOException - if there is an exception reading from the byte array
      • readIntLittleEndianPaddedOnBitWidth

        public static int readIntLittleEndianPaddedOnBitWidth​(InputStream in,
                                                              int bitWidth)
                                                       throws IOException
        Throws:
        IOException
      • writeIntLittleEndianPaddedOnBitWidth

        public static void writeIntLittleEndianPaddedOnBitWidth​(OutputStream out,
                                                                int v,
                                                                int bitWidth)
                                                         throws IOException
        Write a little endian int to out, using the the number of bytes required by bit width
        Parameters:
        out - an output stream
        v - an int value
        bitWidth - bit width for padding
        Throws:
        IOException - if there is an exception while writing
      • readZigZagVarInt

        public static int readZigZagVarInt​(InputStream in)
                                    throws IOException
        uses a trick mentioned in https://developers.google.com/protocol-buffers/docs/encoding to read zigZag encoded data
        Parameters:
        in - an input stream
        Returns:
        the value of a zig-zag varint read from the current position in the stream
        Throws:
        IOException - if there is an exception while reading
      • readZigZagVarLong

        public static long readZigZagVarLong​(InputStream in)
                                      throws IOException
        uses a trick mentioned in https://developers.google.com/protocol-buffers/docs/encoding to read zigZag encoded data TODO: the implementation is compatible with readZigZagVarInt. Is there a need for different functions?
        Parameters:
        in - an input stream
        Returns:
        the value of a zig-zag var-long read from the current position in the stream
        Throws:
        IOException - if there is an exception while reading
      • paddedByteCountFromBits

        public static int paddedByteCountFromBits​(int bitLength)
        Parameters:
        bitLength - a count of bits
        Returns:
        the corresponding byte count padded to the next byte
      • intToBytes

        public static byte[] intToBytes​(int value)
      • bytesToInt

        public static int bytesToInt​(byte[] bytes)
      • longToBytes

        public static byte[] longToBytes​(long value)
      • bytesToLong

        public static long bytesToLong​(byte[] bytes)
      • booleanToBytes

        public static byte[] booleanToBytes​(boolean value)
      • bytesToBool

        public static boolean bytesToBool​(byte[] bytes)