Package io.datarouter.bytes.varint
Class VarIntTool
java.lang.Object
io.datarouter.bytes.varint.VarIntTool
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 Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic intdecodeInt(byte[] bytes) static intdecodeInt(byte[] bytes, int offset) static intdecodeInt(InputStream inputStream) static IntegerdecodeIntOrNull(InputStream inputStream) static longdecodeLong(byte[] bytes) static longdecodeLong(byte[] bytes, int offset) static longdecodeLong(InputStream inputStream) static LongdecodeLongOrNull(InputStream inputStream) static intencode(byte[] bytes, int offset, long value) static byte[]encode(long value) static intencode(OutputStream outputStream, long value) Convenience methods for either: - returning a value - returning Optional.empty() if the end of the InputStream was foundstatic intlength(long value) static voidwriteBytes(long value, OutputStream os) Deprecated.inline me
-
Field Details
-
ERROR_MESSAGE_INVALID_OFFSET
- See Also:
-
ERROR_MESSAGE_INCOMPLETE_VALUE
- See Also:
-
ERROR_MESSAGE_INTEGER_OVERFLOW
- See Also:
-
ERROR_MESSAGE_IO
- See Also:
-
ERROR_MESSAGE_MAX_SIZE_EXCEEDED
- See Also:
-
ERROR_MESSAGE_NEGATIVE_VALUE
- See Also:
-
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
-
writeBytes
Deprecated.inline me -
decodeLong
public static long decodeLong(byte[] bytes, int offset) -
decodeLong
public static long decodeLong(byte[] bytes) -
decodeLong
-
decodeLongOrNull
-
fromInputStream
Convenience methods for either: - returning a value - returning Optional.empty() if the end of the InputStream was found -
fromInputStreamInt
-
decodeInt
public static int decodeInt(byte[] bytes, int offset) -
decodeInt
public static int decodeInt(byte[] bytes) -
decodeInt
-
decodeIntOrNull
-