public final class UnsignedIntEncoder extends Object
int values to/from self-delimited binary, preserving sort order, and such
that the length of the encoding is optimized for values near zero and encoded values never begin with 0xff.
The encoding uses a simple prefixing format:
| Encoded Bytes | Value |
|---|---|
0x00 ... 0xfa |
Same |
0xfb 0xWW |
0xWW |
0xfc 0xWW 0xXX |
0xWWXX |
0xfd 0xWW 0xXX 0xYY |
0xWWXXYY |
0xfe 0xWW 0xXX 0xYY 0xZZ |
0xWWXXYYZZ |
0xff |
Illegal |
| Modifier and Type | Field and Description |
|---|---|
static int |
MAX_ENCODED_LENGTH
Maximum possible length of an encoded value.
|
static int |
MIN_MULTI_BYTE_VALUE
Minimum value that triggers a multi-byte encoding.
|
| Modifier and Type | Method and Description |
|---|---|
static int |
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(int value)
Encode the given value.
|
static int |
encodeLength(int value)
Determine the length (in bytes) of the encoded value.
|
static void |
main(String[] args)
Test routine.
|
static int |
read(ByteBuffer buf)
Read and decode a value from the given
ByteBuffer. |
static int |
read(ByteReader reader)
Read and decode a value from the input.
|
static int |
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,
int value)
Encode the given value and write it to the given
ByteBuffer. |
static void |
write(ByteWriter writer,
int value)
Encode the given value to the output.
|
static void |
write(OutputStream out,
int value)
Encode the given value and write it to the given
OutputStream. |
public static final int MAX_ENCODED_LENGTH
public static final int MIN_MULTI_BYTE_VALUE
public static byte[] encode(int value)
value - value to encodeIllegalArgumentException - if value is negativepublic static int decode(byte[] data)
data - encoded valueIllegalArgumentException - if bytes contains an invalid encoding, or extra trailing garbagepublic static void write(ByteWriter writer, int value)
writer - destination for the encoded valuevalue - value to encodeIllegalArgumentException - if value is negativepublic static void write(OutputStream out, int 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, int value)
ByteBuffer.buf - destination for the encoded valuevalue - value to encodeBufferOverflowException - if buf overflowsReadOnlyBufferException - if buf is read-onlyNullPointerException - if buf is nullpublic static int read(ByteReader reader)
reader - input holding an encoded valueIllegalArgumentException - if the encoded value is truncatedIllegalArgumentException - if an invalid encoding is encounteredpublic static int 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 int 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 valuepublic static int decodeLength(int first)
first - first byte of encoded value (in lower eight bits; other bits are ignored)first)IllegalArgumentException - if the lower eight bits of first equal 0xffpublic static int encodeLength(int value)
value - value to encodeMAX_ENCODED_LENGTHIllegalArgumentException - if value is negativepublic static void main(String[] args)
args - command line argumentsCopyright © 2016. All rights reserved.