Class ReadWriteForEncodingUtils


  • public class ReadWriteForEncodingUtils
    extends Object
    Utils to read/write stream.
    • Method Detail

      • getIntMaxBitWidth

        public static int getIntMaxBitWidth​(List<Integer> list)
        check all number in a int list and find max bit width.
        Parameters:
        list - input list
        Returns:
        max bit width
      • getLongMaxBitWidth

        public static int getLongMaxBitWidth​(List<Long> list)
        check all number in a long list and find max bit width.
        Parameters:
        list - input list
        Returns:
        max bit width
      • getUnsignedVarInt

        public static byte[] getUnsignedVarInt​(int value)
        transform an int var to byte[] format.
      • readUnsignedVarInt

        public static int readUnsignedVarInt​(InputStream in)
                                      throws IOException
        read an unsigned var int in stream and transform it to int format.
        Parameters:
        in - stream to read an unsigned var int
        Returns:
        integer value
        Throws:
        IOException - exception in IO
      • readUnsignedVarInt

        public static int readUnsignedVarInt​(ByteBuffer buffer)
        read an unsigned var int in stream and transform it to int format.
        Parameters:
        buffer - stream to read an unsigned var int
        Returns:
        integer value
      • readVarInt

        public static int readVarInt​(ByteBuffer buffer)
      • writeUnsignedVarInt

        public static int writeUnsignedVarInt​(int value,
                                              ByteArrayOutputStream out)
        write a value to stream using unsigned var int format. for example, int 123456789 has its binary format 00000111-01011011-11001101-00010101 (if we omit the first 5 0, then it is 111010-1101111-0011010-0010101), function writeUnsignedVarInt will split every seven bits and write them to stream from low bit to high bit like: 1-0010101 1-0011010 1-1101111 0-0111010 1 represents has next byte to write, 0 represents number end.
        Parameters:
        value - value to write into stream
        out - output stream
        Returns:
        the number of bytes that the value consume.
      • writeUnsignedVarInt

        public static int writeUnsignedVarInt​(int value,
                                              ByteBuffer buffer)
        write a value to stream using unsigned var int format. for example, int 123456789 has its binary format 111010-1101111-0011010-0010101, function writeUnsignedVarInt will split every seven bits and write them to stream from low bit to high bit like: 1-0010101 1-0011010 1-1101111 0-0111010 1 represents has next byte to write, 0 represents number end.
        Parameters:
        value - value to write into stream
        buffer - where to store the result. buffer.remaining() needs to >= 32. Notice: (1) this function does not check buffer's remaining(). (2) the position will be updated.
        Returns:
        the number of bytes that the value consume.
        Throws:
        IOException - exception in IO
      • writeVarInt

        public static int writeVarInt​(int value,
                                      ByteBuffer buffer)
      • varIntSize

        public static int varIntSize​(int value)
        Returns the encoding size in bytes of its input value.
        Parameters:
        value - the integer to be measured
        Returns:
        the encoding size in bytes of its input value
      • uVarIntSize

        public static int uVarIntSize​(int value)
        Returns the encoding size in bytes of its input value.
        Parameters:
        value - the unsigned integer to be measured
        Returns:
        the encoding size in bytes of its input value
      • writeIntLittleEndianPaddedOnBitWidth

        public static void writeIntLittleEndianPaddedOnBitWidth​(int value,
                                                                OutputStream out,
                                                                int bitWidth)
                                                         throws IOException
        write integer value using special bit to output stream.
        Parameters:
        value - value to write to stream
        out - output stream
        bitWidth - bit length
        Throws:
        IOException - exception in IO
      • writeLongLittleEndianPaddedOnBitWidth

        public static void writeLongLittleEndianPaddedOnBitWidth​(long value,
                                                                 OutputStream out,
                                                                 int bitWidth)
                                                          throws IOException
        write long value using special bit to output stream.
        Parameters:
        value - value to write to stream
        out - output stream
        bitWidth - bit length
        Throws:
        IOException - exception in IO
      • readIntLittleEndianPaddedOnBitWidth

        public static int readIntLittleEndianPaddedOnBitWidth​(ByteBuffer buffer,
                                                              int bitWidth)
                                                       throws IOException
        read integer value using special bit from input stream.
        Parameters:
        buffer - byte buffer
        bitWidth - bit length
        Returns:
        integer value
        Throws:
        IOException - exception in IO
      • readLongLittleEndianPaddedOnBitWidth

        public static long readLongLittleEndianPaddedOnBitWidth​(ByteBuffer buffer,
                                                                int bitWidth)
                                                         throws IOException
        read long value using special bit from input stream.
        Parameters:
        buffer - byte buffer
        bitWidth - bit length
        Returns:
        long long value
        Throws:
        IOException - exception in IO