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 intreadSignedVarInt(byte[] bytes) static intstatic longstatic intreadUnsignedVarInt(byte[] bytes) static intstatic longstatic byte[]writeSignedVarInt(int value) static voidwriteSignedVarInt(int value, DataOutput out) static voidwriteSignedVarLong(long value, DataOutput out) Encodes a value using the variable-length encoding from Google Protocol Buffers.static byte[]writeUnsignedVarInt(int value) static voidwriteUnsignedVarInt(int value, DataOutput out) static voidwriteUnsignedVarLong(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- ifDataOutputthrowsIOException
-
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- ifDataOutputthrowsIOException
-
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- ifDataInputthrowsIOExceptionIllegalArgumentException- 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- ifDataInputthrowsIOExceptionIllegalArgumentException- 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- ifDataInputthrowsIOException- See Also:
-
readUnsignedVarInt
- Throws:
IllegalArgumentException- if variable-length value does not terminate after 5 bytes have been readIOException- ifDataInputthrowsIOException- See Also:
-
readSignedVarInt
public static int readSignedVarInt(byte[] bytes) -
readUnsignedVarInt
public static int readUnsignedVarInt(byte[] bytes)
-