Class VarInt


  • public class VarInt
    extends java.lang.Object
    Common methods to encode and decode varints and varlongs into ByteBuffers and arrays.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int MAX_VARINT_SIZE
      Maximum encoded size of 32-bit positive integers (in bytes)
      static int MAX_VARLONG_SIZE
      maximum encoded size of 64-bit longs, and negative 32-bit ints (in bytes)
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private VarInt()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int getVarInt​(byte[] src, int offset, int[] dst)
      Reads a varint from src, places its values into the first element of dst and returns the offset in to src of the first byte after the varint.
      static int getVarInt​(java.io.InputStream inputStream)
      Reads a varint from the given InputStream and returns the decoded value as an int.
      static int getVarInt​(java.nio.ByteBuffer src)
      Reads a varint from the current position of the given ByteBuffer and returns the decoded value as 32 bit integer.
      static long getVarLong​(java.nio.ByteBuffer src)
      Reads an up to 64 bit long varint from the current position of the given ByteBuffer and returns the decoded value as long.
      static int putVarInt​(int v, byte[] sink, int offset)
      Encodes an integer in a variable-length encoding, 7 bits per byte, into a destination byte[], following the protocol buffer convention.
      static void putVarInt​(int v, java.io.OutputStream outputStream)
      Encodes an integer in a variable-length encoding, 7 bits per byte, and writes it to the given OutputStream.
      static void putVarInt​(int v, java.nio.ByteBuffer sink)
      Encodes an integer in a variable-length encoding, 7 bits per byte, to a ByteBuffer sink.
      static void putVarLong​(long v, java.nio.ByteBuffer sink)
      Encodes a long integer in a variable-length encoding, 7 bits per byte, to a ByteBuffer sink.
      static int varIntSize​(int i)
      Returns the encoding size in bytes of its input value.
      static int varLongSize​(long v)
      Returns the encoding size in bytes of its input value.
      • Methods inherited from class java.lang.Object

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

      • MAX_VARINT_SIZE

        public static final int MAX_VARINT_SIZE
        Maximum encoded size of 32-bit positive integers (in bytes)
        See Also:
        Constant Field Values
      • MAX_VARLONG_SIZE

        public static final int MAX_VARLONG_SIZE
        maximum encoded size of 64-bit longs, and negative 32-bit ints (in bytes)
        See Also:
        Constant Field Values
    • Constructor Detail

      • VarInt

        private VarInt()
    • Method Detail

      • varIntSize

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

        public static int getVarInt​(byte[] src,
                                    int offset,
                                    int[] dst)
        Reads a varint from src, places its values into the first element of dst and returns the offset in to src of the first byte after the varint.
        Parameters:
        src - source buffer to retrieve from
        offset - offset within src
        dst - the resulting int value
        Returns:
        the updated offset after reading the varint
      • putVarInt

        public static int putVarInt​(int v,
                                    byte[] sink,
                                    int offset)
        Encodes an integer in a variable-length encoding, 7 bits per byte, into a destination byte[], following the protocol buffer convention.
        Parameters:
        v - the int value to write to sink
        sink - the sink buffer to write to
        offset - the offset within sink to begin writing
        Returns:
        the updated offset after writing the varint
      • getVarInt

        public static int getVarInt​(java.nio.ByteBuffer src)
        Reads a varint from the current position of the given ByteBuffer and returns the decoded value as 32 bit integer.

        The position of the buffer is advanced to the first byte after the decoded varint.

        Parameters:
        src - the ByteBuffer to get the var int from
        Returns:
        The integer value of the decoded varint
      • putVarInt

        public static void putVarInt​(int v,
                                     java.nio.ByteBuffer sink)
        Encodes an integer in a variable-length encoding, 7 bits per byte, to a ByteBuffer sink.
        Parameters:
        v - the value to encode
        sink - the ByteBuffer to add the encoded value
      • getVarInt

        public static int getVarInt​(java.io.InputStream inputStream)
                             throws java.io.IOException
        Reads a varint from the given InputStream and returns the decoded value as an int.
        Parameters:
        inputStream - the InputStream to read from
        Throws:
        java.io.IOException
      • putVarInt

        public static void putVarInt​(int v,
                                     java.io.OutputStream outputStream)
                              throws java.io.IOException
        Encodes an integer in a variable-length encoding, 7 bits per byte, and writes it to the given OutputStream.
        Parameters:
        v - the value to encode
        outputStream - the OutputStream to write to
        Throws:
        java.io.IOException
      • varLongSize

        public static int varLongSize​(long v)
        Returns the encoding size in bytes of its input value.
        Parameters:
        v - the long to be measured
        Returns:
        the encoding size in bytes of a given long value.
      • getVarLong

        public static long getVarLong​(java.nio.ByteBuffer src)
        Reads an up to 64 bit long varint from the current position of the given ByteBuffer and returns the decoded value as long.

        The position of the buffer is advanced to the first byte after the decoded varint.

        Parameters:
        src - the ByteBuffer to get the var int from
        Returns:
        The integer value of the decoded long varint
      • putVarLong

        public static void putVarLong​(long v,
                                      java.nio.ByteBuffer sink)
        Encodes a long integer in a variable-length encoding, 7 bits per byte, to a ByteBuffer sink.
        Parameters:
        v - the value to encode
        sink - the ByteBuffer to add the encoded value