public class UnsignedLexVarint
extends java.lang.Object
Unsigned varint (32 bit and 64 bit) that is encoded in such a way that the lexicographic comparison of the binary encoding is consistent with the numeric comparison of the numeric values.
For example, consider the integers 130, 257, and 16385. Numerically, 130 < 257 < 16385
. Consider their binary
encodings using protobuf-style varints:
The lexicographical comparison of these encodings will give us 16385 < 257 < 130
.
This happens because the first bit of each byte of a protobuf-style varint says whether there are more bytes in the binary representation (1 means 'more bytes', 0 means 'no more bytes'). Also, protobuf-style varints switch the order of bytes of the encoded integer (that is, least- significant bytes go first).
To make binary encodings lexicographically comparable, we must place all of the 'more bytes' prefixes at the start of the encoded representation, and encode bytes with most-significant bytes first.
For unsigned 64-bit integers, values in the range [0, 2^7)
use 1 byte; [2^7, 2^14)
use 2 bytes,
[2^14, 2^21)
use 3 bytes, and so on. Negative 64-bit unsigned integers use 9 bytes. Negative 32-bit unsigned
integeers use 5 bytes.
Constructor and Description |
---|
UnsignedLexVarint() |
Modifier and Type | Method and Description |
---|---|
static int |
encodedLength(int value) |
static int |
encodedLength(long value) |
static void |
main(java.lang.String[] args) |
static int |
readLexVarUInt32(java.nio.ByteBuffer buf)
Reads a lexvar uint32 from the specified buf, advancing the buf's position
|
static int |
readLexVarUInt32(java.nio.ByteBuffer buf,
int position)
Reads a lexvar uint32 from the specified buf without advancing the buf's position
|
static long |
readLexVarUInt64(java.nio.ByteBuffer buf)
Reads a varint that is lexicographically comparable with other varints and advance the buf's position
|
static long |
readLexVarUInt64(java.nio.ByteBuffer buf,
int position)
Reads a varint that is lexicographically comparable with other varints without advancing the buf's position
|
static long |
readReverseLexVarUInt64(java.nio.ByteBuffer buf)
Reads a varint that is reverse lexicographically comparable with other varints and advance the buf's position
|
static long |
readReverseLexVarUInt64(java.nio.ByteBuffer buf,
int position)
Reads a varint that is reverse lexicographically comparable with other varints without advancing the buf's position
|
static int |
writeLexVarUInt32(java.nio.ByteBuffer buf,
int value) |
static int |
writeLexVarUInt64(java.nio.ByteBuffer buf,
long value) |
static int |
writeReverseLexVarUInt32(java.nio.ByteBuffer buf,
int value) |
static int |
writeReverseLexVarUInt64(java.nio.ByteBuffer buf,
long value) |
static byte[] |
writeVarUInt32(int value)
Returns the byte representation of the provided 32 bit unsigned integer encoded as a lexicographically comparable
varint
|
static byte[] |
writeVarUInt64(long value)
Returns the byte representation of the provided 64 bit unsigned integer encoded as a lexicographically comparable
varint
|
public static void main(java.lang.String[] args)
public static int encodedLength(int value)
value
- Any integer valuepublic static int encodedLength(long value)
value
- Any long valuepublic static int writeLexVarUInt32(java.nio.ByteBuffer buf, int value)
public static int writeReverseLexVarUInt32(java.nio.ByteBuffer buf, int value)
public static int writeLexVarUInt64(java.nio.ByteBuffer buf, long value)
public static int writeReverseLexVarUInt64(java.nio.ByteBuffer buf, long value)
public static int readLexVarUInt32(java.nio.ByteBuffer buf) throws AtomLayerException
AtomLayerException
public static int readLexVarUInt32(java.nio.ByteBuffer buf, int position) throws AtomLayerException
AtomLayerException
public static long readLexVarUInt64(java.nio.ByteBuffer buf) throws AtomLayerException
AtomLayerException
public static long readReverseLexVarUInt64(java.nio.ByteBuffer buf) throws AtomLayerException
AtomLayerException
public static long readLexVarUInt64(java.nio.ByteBuffer buf, int position) throws AtomLayerException
AtomLayerException
public static long readReverseLexVarUInt64(java.nio.ByteBuffer buf, int position) throws AtomLayerException
AtomLayerException
public static byte[] writeVarUInt32(int value)
public static byte[] writeVarUInt64(long value)