Class VarIntTool

java.lang.Object
io.datarouter.bytes.VarIntTool

public class VarIntTool extends Object
Encodes a positive long value to a variable number of bytes. The leftmost bit (the sign bit) of each byte indicates if there are more values. A "1" bit (negative java byte value) indicates there is at least one more value byte. The leftmost byte is the least significant byte, each of which is negative until the last byte which is positive. Note the bitwise ordering of the byte arrays does not match Integer/Long ordering. Each byte contains 1 metadata/continuation bit and 7 data bits: - Values 0 to 127 are encoded in 1 byte. - Values 128 to 16_383 are encoded in 2 bytes. - Values 16_384 to 2_097_151 are encoded in 3 bytes. - Values 2_097_152 to 268_435_455 are encoded in 4 bytes. - Etc. - Long.MAX_VALUE is encoded in 9 bytes. The primary use case is for encoding many values that usually fit in 1 or sometimes 2 bytes. This can save 75% of space versus encoding ints to 4 bytes while still allowing big values. Calling length(value) calculates the encoded length quickly, faster than encoding the value to obtain the length. Encoding can be written to an OutputStream without heap allocations. Decoding can be read from an InputStream without heap allocations.
  • Field Details

    • ERROR_MESSAGE_INVALID_OFFSET

      public static final String ERROR_MESSAGE_INVALID_OFFSET
      See Also:
    • ERROR_MESSAGE_INCOMPLETE_VALUE

      public static final String ERROR_MESSAGE_INCOMPLETE_VALUE
      See Also:
    • ERROR_MESSAGE_INTEGER_OVERFLOW

      public static final String ERROR_MESSAGE_INTEGER_OVERFLOW
      See Also:
    • ERROR_MESSAGE_IO

      public static final String ERROR_MESSAGE_IO
      See Also:
    • ERROR_MESSAGE_MAX_SIZE_EXCEEDED

      public static final String ERROR_MESSAGE_MAX_SIZE_EXCEEDED
      See Also:
    • ERROR_MESSAGE_NEGATIVE_VALUE

      public static final String ERROR_MESSAGE_NEGATIVE_VALUE
      See Also:
    • ERROR_MESSAGE_UNEXPECTED_END_OF_INPUT_STREAM

      public static final String ERROR_MESSAGE_UNEXPECTED_END_OF_INPUT_STREAM
      See Also:
    • INTEGER_MAX_ENCODED_VALUE

      public static final byte[] INTEGER_MAX_ENCODED_VALUE
    • LONG_MAX_ENCODED_VALUE

      public static final byte[] LONG_MAX_ENCODED_VALUE
  • Constructor Details

    • VarIntTool

      public VarIntTool()
  • Method Details

    • length

      public static int length(long value)
    • encode

      public static byte[] encode(long value)
    • encode

      public static int encode(byte[] bytes, int offset, long value)
    • encode

      public static int encode(OutputStream outputStream, long value)
    • writeBytes

      @Deprecated public static void writeBytes(long value, OutputStream os)
      Deprecated.
      inline me
    • decodeLong

      public static long decodeLong(byte[] bytes, int offset)
    • decodeLong

      public static long decodeLong(byte[] bytes)
    • decodeLong

      public static long decodeLong(InputStream inputStream)
    • decodeLongOrNull

      public static Long decodeLongOrNull(InputStream inputStream)
    • fromInputStream

      public static Optional<Long> fromInputStream(InputStream is)
      Convenience methods for either: - returning a value - returning Optional.empty() if the end of the InputStream was found
    • fromInputStreamInt

      public static Optional<Integer> fromInputStreamInt(InputStream is)
    • decodeInt

      public static int decodeInt(byte[] bytes, int offset)
    • decodeInt

      public static int decodeInt(byte[] bytes)
    • decodeInt

      public static int decodeInt(InputStream inputStream)
    • decodeIntOrNull

      public static Integer decodeIntOrNull(InputStream inputStream)