Class VIntCoding


  • public class VIntCoding
    extends java.lang.Object
    Borrows idea from https://developers.google.com/protocol-buffers/docs/encoding#varints
    • Field Detail

      • encodingBuffer

        protected static final io.netty.util.concurrent.FastThreadLocal<byte[]> encodingBuffer
    • Constructor Detail

      • VIntCoding

        public VIntCoding()
    • Method Detail

      • readUnsignedVInt

        public static long readUnsignedVInt​(java.io.DataInput input)
                                     throws java.io.IOException
        Throws:
        java.io.IOException
      • skipUnsignedVInt

        public static void skipUnsignedVInt​(DataInputPlus input)
                                     throws java.io.IOException
        Throws:
        java.io.IOException
      • getUnsignedVInt32

        public static int getUnsignedVInt32​(java.nio.ByteBuffer input,
                                            int readerIndex)
        Read up to a 32-bit integer back, using the unsigned (no zigzag) encoding. Note this method is the same as readUnsignedVInt(DataInput), except that we do *not* block if there are not enough bytes in the buffer to reconstruct the value.
        Throws:
        VIntCoding.VIntOutOfRangeException - If the vint doesn't fit into a 32-bit integer
      • getVInt32

        public static int getVInt32​(java.nio.ByteBuffer input,
                                    int readerIndex)
      • getVInt

        public static long getVInt​(java.nio.ByteBuffer input,
                                   int readerIndex)
      • getUnsignedVInt

        public static long getUnsignedVInt​(java.nio.ByteBuffer input,
                                           int readerIndex)
      • getUnsignedVInt

        public static long getUnsignedVInt​(java.nio.ByteBuffer input,
                                           int readerIndex,
                                           int readerLimit)
      • getUnsignedVInt32

        public static <V> int getUnsignedVInt32​(V input,
                                                ValueAccessor<V> accessor,
                                                int readerIndex)
      • getVInt32

        public static <V> int getVInt32​(V input,
                                        ValueAccessor<V> accessor,
                                        int readerIndex)
      • getVInt

        public static <V> long getVInt​(V input,
                                       ValueAccessor<V> accessor,
                                       int readerIndex)
      • getUnsignedVInt

        public static <V> long getUnsignedVInt​(V input,
                                               ValueAccessor<V> accessor,
                                               int readerIndex)
      • getUnsignedVInt

        public static <V> long getUnsignedVInt​(V input,
                                               ValueAccessor<V> accessor,
                                               int readerIndex,
                                               int readerLimit)
      • computeUnsignedVIntSize

        public static int computeUnsignedVIntSize​(java.nio.ByteBuffer input,
                                                  int readerIndex)
        Computes size of an unsigned vint that starts at readerIndex of the provided ByteBuf.
        Returns:
        -1 if there are not enough bytes in the input to calculate the size; else, the vint unsigned value size in bytes.
      • computeUnsignedVIntSize

        public static int computeUnsignedVIntSize​(java.nio.ByteBuffer input,
                                                  int readerIndex,
                                                  int readerLimit)
      • readVInt

        public static long readVInt​(java.io.DataInput input)
                             throws java.io.IOException
        Throws:
        java.io.IOException
      • readVInt32

        public static int readVInt32​(java.io.DataInput input)
                              throws java.io.IOException
        Read up to a signed 32-bit integer back. Assumes the vint was written using writeVInt32(int, DataOutputPlus) or similar that zigzag encodes the integer.
        Throws:
        VIntCoding.VIntOutOfRangeException - If the vint doesn't fit into a 32-bit integer
        java.io.IOException
      • readUnsignedVInt32

        public static int readUnsignedVInt32​(java.io.DataInput input)
                                      throws java.io.IOException
        Read up to a 32-bit integer. This method assumes the original integer was written using writeUnsignedVInt32(int, DataOutputPlus) or similar that doesn't zigzag encodes the vint.
        Throws:
        VIntCoding.VIntOutOfRangeException - If the vint doesn't fit into a 32-bit integer
        java.io.IOException
      • firstByteValueMask

        public static int firstByteValueMask​(int extraBytesToRead)
      • encodeExtraBytesToRead

        public static int encodeExtraBytesToRead​(int extraBytesToRead)
      • numberOfExtraBytesToRead

        public static int numberOfExtraBytesToRead​(int firstByte)
      • writeUnsignedVInt

        @Deprecated(since="5.0")
        public static void writeUnsignedVInt​(int value,
                                             DataOutputPlus output)
                                      throws java.io.IOException
        Deprecated.
        See CASSANDRA-18099
        Throws:
        java.io.IOException
      • writeUnsignedVInt

        @Inline
        public static void writeUnsignedVInt​(long value,
                                             DataOutputPlus output)
                                      throws java.io.IOException
        Throws:
        java.io.IOException
      • writeUnsignedVInt32

        public static void writeUnsignedVInt32​(int value,
                                               DataOutputPlus output)
                                        throws java.io.IOException
        Throws:
        java.io.IOException
      • writeUnsignedVInt

        @Deprecated(since="5.0")
        public static void writeUnsignedVInt​(int value,
                                             java.nio.ByteBuffer output)
                                      throws java.io.IOException
        Deprecated.
        See CASSANDRA-18099
        Throws:
        java.io.IOException
      • writeUnsignedVInt

        @Inline
        public static void writeUnsignedVInt​(long value,
                                             java.nio.ByteBuffer output)
      • writeVInt

        @Inline
        public static <V> int writeVInt​(long value,
                                        V output,
                                        int offset,
                                        ValueAccessor<V> accessor)
      • writeVInt32

        @Inline
        public static <V> int writeVInt32​(int value,
                                          V output,
                                          int offset,
                                          ValueAccessor<V> accessor)
      • writeUnsignedVInt32

        @Inline
        public static <V> int writeUnsignedVInt32​(int value,
                                                  V output,
                                                  int offset,
                                                  ValueAccessor<V> accessor)
      • writeUnsignedVInt

        @Inline
        public static <V> int writeUnsignedVInt​(long value,
                                                V output,
                                                int offset,
                                                ValueAccessor<V> accessor)
      • writeUnsignedVInt32

        @Inline
        public static void writeUnsignedVInt32​(int value,
                                               java.nio.ByteBuffer output)
      • writeVInt

        @Deprecated(since="5.0")
        public static void writeVInt​(int value,
                                     DataOutputPlus output)
                              throws java.io.IOException
        Deprecated.
        See CASSANDRA-18099
        Throws:
        java.io.IOException
      • writeVInt

        @Inline
        public static void writeVInt​(long value,
                                     DataOutputPlus output)
                              throws java.io.IOException
        Throws:
        java.io.IOException
      • writeVInt32

        @Inline
        public static void writeVInt32​(int value,
                                       DataOutputPlus output)
                                throws java.io.IOException
        Throws:
        java.io.IOException
      • writeVInt

        @Deprecated(since="5.0")
        public static void writeVInt​(int value,
                                     java.nio.ByteBuffer output)
        Deprecated.
        See CASSANDRA-18099
      • writeVInt

        @Inline
        public static void writeVInt​(long value,
                                     java.nio.ByteBuffer output)
      • writeVInt32

        @Inline
        public static void writeVInt32​(int value,
                                       java.nio.ByteBuffer output)
      • decodeZigZag64

        public static long decodeZigZag64​(long n)
        Decode a ZigZag-encoded 64-bit value. ZigZag encodes signed integers into values that can be efficiently encoded with varint. (Otherwise, negative values must be sign-extended to 64 bits to be varint encoded, thus always taking 10 bytes on the wire.)
        Parameters:
        n - An unsigned 64-bit integer, stored in a signed int because Java has no explicit unsigned support.
        Returns:
        A signed 64-bit integer.
      • encodeZigZag64

        public static long encodeZigZag64​(long n)
        Encode a ZigZag-encoded 64-bit value. ZigZag encodes signed integers into values that can be efficiently encoded with varint. (Otherwise, negative values must be sign-extended to 64 bits to be varint encoded, thus always taking 10 bytes on the wire.)
        Parameters:
        n - A signed 64-bit integer.
        Returns:
        An unsigned 64-bit integer, stored in a signed int because Java has no explicit unsigned support.
      • computeVIntSize

        public static int computeVIntSize​(long param)
        Compute the number of bytes that would be needed to encode a varint.
      • computeUnsignedVIntSize

        public static int computeUnsignedVIntSize​(long value)
        Compute the number of bytes that would be needed to encode an unsigned varint.
      • checkedCast

        public static int checkedCast​(long value)