Class Varint
java.lang.Object
com.hiddenswitch.spellsource.util.Varint
Encodes signed and unsigned values using a common variable-length scheme, found for example in
Google's Protocol Buffers. It uses fewer bytes to encode smaller values, but will use slightly more bytes to
encode large values.
Signed values are further encoded using so-called zig-zag encoding in order to make them "compatible" with variable-length encoding.
-
Method Summary
Modifier and TypeMethodDescriptionstatic int
readSignedVarInt
(byte[] bytes) static int
static long
static int
readUnsignedVarInt
(byte[] bytes) static int
static long
static byte[]
writeSignedVarInt
(int value) static void
writeSignedVarInt
(int value, DataOutput out) static void
writeSignedVarLong
(long value, DataOutput out) Encodes a value using the variable-length encoding from Google Protocol Buffers.static byte[]
writeUnsignedVarInt
(int value) static void
writeUnsignedVarInt
(int value, DataOutput out) static void
writeUnsignedVarLong
(long value, DataOutput out) Encodes a value using the variable-length encoding from Google Protocol Buffers.
-
Method Details
-
writeSignedVarLong
Encodes a value using the variable-length encoding from Google Protocol Buffers. It uses zig-zag encoding to efficiently encode signed values. If values are known to be nonnegative,writeUnsignedVarLong(long, DataOutput)
should be used.- Parameters:
value
- value to encodeout
- to write bytes to- Throws:
IOException
- ifDataOutput
throwsIOException
-
writeUnsignedVarLong
Encodes a value using the variable-length encoding from Google Protocol Buffers. Zig-zag is not used, so input must not be negative. If values can be negative, usewriteSignedVarLong(long, DataOutput)
instead. This method treats negative input as like a large unsigned value.- Parameters:
value
- value to encodeout
- to write bytes to- Throws:
IOException
- ifDataOutput
throwsIOException
-
writeSignedVarInt
- Throws:
IOException
- See Also:
-
writeUnsignedVarInt
- Throws:
IOException
- See Also:
-
writeSignedVarInt
public static byte[] writeSignedVarInt(int value) -
writeUnsignedVarInt
public static byte[] writeUnsignedVarInt(int value) -
readSignedVarLong
- Parameters:
in
- to read bytes from- Returns:
- decode value
- Throws:
IOException
- ifDataInput
throwsIOException
IllegalArgumentException
- if variable-length value does not terminate after 9 bytes have been read- See Also:
-
readUnsignedVarLong
- Parameters:
in
- to read bytes from- Returns:
- decode value
- Throws:
IOException
- ifDataInput
throwsIOException
IllegalArgumentException
- if variable-length value does not terminate after 9 bytes have been read- See Also:
-
readSignedVarInt
- Throws:
IllegalArgumentException
- if variable-length value does not terminate after 5 bytes have been readIOException
- ifDataInput
throwsIOException
- See Also:
-
readUnsignedVarInt
- Throws:
IllegalArgumentException
- if variable-length value does not terminate after 5 bytes have been readIOException
- ifDataInput
throwsIOException
- See Also:
-
readSignedVarInt
public static int readSignedVarInt(byte[] bytes) -
readUnsignedVarInt
public static int readUnsignedVarInt(byte[] bytes)
-