Class EncodedInts


  • @GwtCompatible
    public class EncodedInts
    extends Object
    Utilities for encoding and decoding integers.
    • Constructor Detail

      • EncodedInts

        public EncodedInts()
    • Method Detail

      • readVarint64

        public static long readVarint64​(InputStream input)
                                 throws IOException
        Reads a variable-encoded signed long.

        Note that if you frequently read/write negative numbers, you should consider zigzag-encoding your values before storing them as varints. See encodeZigZag32(int) and decodeZigZag32(int).

        Throws:
        IOException - if input.read() throws an IOException or returns -1 (EOF), or if the variable-encoded signed long is malformed.
      • encodeZigZag32

        public static int encodeZigZag32​(int n)
        Encode a ZigZag-encoded 32-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 32-bit integer.
        Returns:
        An unsigned 32-bit integer, stored in a signed int because Java has no explicit unsigned support.
      • 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.
      • decodeZigZag32

        public static int decodeZigZag32​(int n)
        Decode a ZigZag-encoded 32-bit signed 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 32-bit integer, stored in a signed int because Java has no explicit unsigned support.
        Returns:
        A signed 32-bit integer.
      • decodeZigZag64

        public static long decodeZigZag64​(long n)
        Decode a ZigZag-encoded 64-bit signed 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 64-bit integer, stored in a signed long because Java has no explicit unsigned support.
        Returns:
        A signed 64-bit integer.
      • interleaveBits

        public static long interleaveBits​(int val1,
                                          int val2)
        Returns the interleaving of bits of val1 and val2, where the LSB of val1 is the LSB of the result, and the MSB of val2 is the MSB of the result.
      • deinterleaveBits1

        public static int deinterleaveBits1​(long bits)
        Returns the first int de-interleaved from the result of interleaveBits(int, int).
      • deinterleaveBits2

        public static int deinterleaveBits2​(long bits)
        Returns the second int de-interleaved from the result of interleaveBits(int, int).
      • interleaveBitPairs

        public static long interleaveBitPairs​(int val1,
                                              int val2)
        Like interleaveBits(int, int) but interleaves bit pairs rather than individual bits. This format is faster to decode than the fully interleaved format, and produces the same results for our use case.

        This code is about 10% faster than interleaveBits(int, int).

      • deinterleaveBitPairs1

        public static int deinterleaveBitPairs1​(long pairs)
        Returns the first int de-interleaved from the result of interleaveBitPairs(int, int).
      • deinterleaveBitPairs2

        public static int deinterleaveBitPairs2​(long pairs)
        Returns the second int de-interleaved from the result of interleaveBitPairs(int, int).