public final class LongEncoder extends Object
long
values to/from binary, preserving sort order, and such that the length of the
encoding is optimized for values near zero.
Encoded values are guaranteed to not start with 0x00
or 0xff
.
Modifier and Type | Field and Description |
---|---|
static int |
MAX_ENCODED_LENGTH
Maximum possible length of an encoded value.
|
static int |
MAX_SINGLE_BYTE_ENCODED
Maximum value for the first byte of a single byte encoded value.
|
static int |
MAX_SINGLE_BYTE_VALUE
Maximum value that can be encoded as a single byte.
|
static int |
MIN_SINGLE_BYTE_ENCODED
Minimum value for the first byte of a single byte encoded value.
|
static int |
MIN_SINGLE_BYTE_VALUE
Minimum value that can be encoded as a single byte.
|
static int |
NEGATIVE_ADJUST
Adjustment applied to multi-byte encoded negative values before encoding.
|
static int |
POSITIVE_ADJUST
Adjustment applied to multi-byte encoded positive values before encoding.
|
static int |
ZERO_ADJUST
Adjustment applied to single byte encoded values before encoding.
|
Modifier and Type | Method and Description |
---|---|
static long |
decode(byte[] data)
Decode the given value.
|
static int |
decodeLength(int first)
Determine the length (in bytes) of an encoded value based on the first byte.
|
static byte[] |
encode(long value)
Encode the given value.
|
static int |
encodeLength(long value)
Determine the length (in bytes) of the encoded value.
|
static void |
main(String[] args)
Test routine.
|
static long |
read(ByteBuffer buf)
Read and decode a value from the given
ByteBuffer . |
static long |
read(ByteReader reader)
Read and decode a value from the input.
|
static long |
read(InputStream input)
Read and decode a value from the given
InputStream . |
static void |
skip(ByteReader reader)
Skip a value from the input.
|
static void |
write(ByteBuffer buf,
long value)
Encode the given value and write it to the given
ByteBuffer . |
static void |
write(ByteWriter writer,
long value)
Encode the given value to the output.
|
static void |
write(OutputStream out,
long value)
Encode the given value and write it to the given
OutputStream . |
public static final int MAX_ENCODED_LENGTH
public static final int MIN_SINGLE_BYTE_ENCODED
public static final int MAX_SINGLE_BYTE_ENCODED
public static final int ZERO_ADJUST
public static final int MIN_SINGLE_BYTE_VALUE
public static final int MAX_SINGLE_BYTE_VALUE
public static final int NEGATIVE_ADJUST
public static final int POSITIVE_ADJUST
public static byte[] encode(long value)
value
- value to encodepublic static long decode(byte[] data)
data
- encoded valueIllegalArgumentException
- if bytes
contains an invalid encoding, or extra trailing garbagepublic static void write(ByteWriter writer, long value)
writer
- destination for the encoded valuevalue
- value to encodeNullPointerException
- if writer
is nullpublic static void write(OutputStream out, long value) throws IOException
OutputStream
.out
- destination for the encoded valuevalue
- value to encodeIOException
- if an I/O error occursNullPointerException
- if out
is nullpublic static void write(ByteBuffer buf, long value)
ByteBuffer
.buf
- destination for the encoded valuevalue
- value to encodeBufferOverflowException
- if buf
overflowsReadOnlyBufferException
- if buf
is read-onlyNullPointerException
- if buf
is nullpublic static long read(ByteReader reader)
reader
- input holding an encoded valueIllegalArgumentException
- if an invalid encoding is encounteredIllegalArgumentException
- if the encoded value is truncatedNullPointerException
- if reader
is nullpublic static long read(InputStream input) throws IOException
InputStream
.input
- input source for the encoded valueIOException
- if an I/O error occursEOFException
- if an unexpected EOF is encounteredIllegalArgumentException
- if an invalid encoding is encounteredNullPointerException
- if input
is nullpublic static long read(ByteBuffer buf)
ByteBuffer
.buf
- input source for the encoded valueBufferUnderflowException
- if buf
underflowsIllegalArgumentException
- if an invalid encoding is encounteredNullPointerException
- if buf
is nullpublic static void skip(ByteReader reader)
reader
- input holding an encoded valueIllegalArgumentException
- if the first byte is 0xff
public static int decodeLength(int first)
first
- first byte of encoded value (in lower eight bits; other bits are ignored)first
)IllegalArgumentException
- if the first byte is 0xff
public static int encodeLength(long value)
value
- value to encodeMAX_ENCODED_LENGTH
public static void main(String[] args)
args
- command line argumentsCopyright © 2022. All rights reserved.