Class ReadWriteForEncodingUtils


  • public class ReadWriteForEncodingUtils
    extends java.lang.Object
    Utils to read/write stream.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int getIntMaxBitWidth​(java.util.List<java.lang.Integer> list)
      check all number in a int list and find max bit width.
      static int getLongMaxBitWidth​(java.util.List<java.lang.Long> list)
      check all number in a long list and find max bit width.
      static byte[] getUnsignedVarInt​(int value)
      transform an int var to byte[] format.
      static int readIntLittleEndianPaddedOnBitWidth​(java.nio.ByteBuffer buffer, int bitWidth)
      read integer value using special bit from input stream.
      static long readLongLittleEndianPaddedOnBitWidth​(java.nio.ByteBuffer buffer, int bitWidth)
      read long value using special bit from input stream.
      static int readUnsignedVarInt​(java.io.InputStream in)
      read an unsigned var int in stream and transform it to int format.
      static int readUnsignedVarInt​(java.nio.ByteBuffer buffer)
      read an unsigned var int in stream and transform it to int format.
      static int readVarInt​(java.io.InputStream in)  
      static int readVarInt​(java.nio.ByteBuffer buffer)  
      static int uVarIntSize​(int value)
      Returns the encoding size in bytes of its input value.
      static int varIntSize​(int value)
      Returns the encoding size in bytes of its input value.
      static void writeIntLittleEndianPaddedOnBitWidth​(int value, java.io.OutputStream out, int bitWidth)
      write integer value using special bit to output stream.
      static void writeLongLittleEndianPaddedOnBitWidth​(long value, java.io.OutputStream out, int bitWidth)
      write long value using special bit to output stream.
      static int writeUnsignedVarInt​(int value, java.io.ByteArrayOutputStream out)
      write a value to stream using unsigned var int format.
      static int writeUnsignedVarInt​(int value, java.io.OutputStream out)  
      static int writeUnsignedVarInt​(int value, java.nio.ByteBuffer buffer)
      write a value to stream using unsigned var int format.
      static int writeVarInt​(int value, java.io.ByteArrayOutputStream out)  
      static int writeVarInt​(int value, java.io.OutputStream out)  
      static int writeVarInt​(int value, java.nio.ByteBuffer buffer)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • getIntMaxBitWidth

        public static int getIntMaxBitWidth​(java.util.List<java.lang.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​(java.util.List<java.lang.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​(java.io.InputStream in)
                                      throws java.io.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:
        java.io.IOException - exception in IO
      • readVarInt

        public static int readVarInt​(java.io.InputStream in)
                              throws java.io.IOException
        Throws:
        java.io.IOException
      • readUnsignedVarInt

        public static int readUnsignedVarInt​(java.nio.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​(java.nio.ByteBuffer buffer)
      • writeUnsignedVarInt

        public static int writeUnsignedVarInt​(int value,
                                              java.io.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.
      • writeVarInt

        public static int writeVarInt​(int value,
                                      java.io.ByteArrayOutputStream out)
      • writeUnsignedVarInt

        public static int writeUnsignedVarInt​(int value,
                                              java.io.OutputStream out)
                                       throws java.io.IOException
        Throws:
        java.io.IOException
      • writeVarInt

        public static int writeVarInt​(int value,
                                      java.io.OutputStream out)
                               throws java.io.IOException
        Throws:
        java.io.IOException
      • writeUnsignedVarInt

        public static int writeUnsignedVarInt​(int value,
                                              java.nio.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:
        java.io.IOException - exception in IO
      • writeVarInt

        public static int writeVarInt​(int value,
                                      java.nio.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,
                                                                java.io.OutputStream out,
                                                                int bitWidth)
                                                         throws java.io.IOException
        write integer value using special bit to output stream.
        Parameters:
        value - value to write to stream
        out - output stream
        bitWidth - bit length
        Throws:
        java.io.IOException - exception in IO
      • writeLongLittleEndianPaddedOnBitWidth

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

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

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