Package io.permazen.util
Class UnsignedIntEncoder
java.lang.Object
io.permazen.util.UnsignedIntEncoder
Encodes unsigned (i.e., non-negative)
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 + 0xfb |
0xfc 0xWW 0xXX |
0xWWXX + 0xfb |
0xfd 0xWW 0xXX 0xYY |
0xWWXXYY + 0xfb |
0xfe 0xWW 0xXX 0xYY 0xZZ |
0xWWXXYYZZ + 0xfb |
0xff |
Illegal |
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
Maximum possible length of an encoded value.static final int
Minimum value that triggers a multi-byte encoding. -
Method Summary
Modifier and TypeMethodDescriptionstatic 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
encode
(int value, byte[] buf, int off) Encode the given value and write the encoded bytes into the given buffer.static int
encodeLength
(int value) Determine the length (in bytes) of the encoded value.static void
Test routine.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 givenInputStream
.static int
read
(ByteBuffer buf) Read and decode a value from the givenByteBuffer
.static void
skip
(ByteReader reader) Skip a value from the input.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 givenOutputStream
.static void
write
(ByteBuffer buf, int value) Encode the given value and write it to the givenByteBuffer
.
-
Field Details
-
MAX_ENCODED_LENGTH
public static final int MAX_ENCODED_LENGTHMaximum possible length of an encoded value.- See Also:
-
MIN_MULTI_BYTE_VALUE
public static final int MIN_MULTI_BYTE_VALUEMinimum value that triggers a multi-byte encoding.- See Also:
-
-
Method Details
-
encode
public static byte[] encode(int value) Encode the given value.- Parameters:
value
- value to encode- Returns:
- encoded value
- Throws:
IllegalArgumentException
- ifvalue
is negative
-
decode
public static int decode(byte[] data) Decode the given value.- Parameters:
data
- encoded value- Returns:
- decoded value
- Throws:
IllegalArgumentException
- ifbytes
contains an invalid encoding, or extra trailing garbage
-
write
Encode the given value to the output.- Parameters:
writer
- destination for the encoded valuevalue
- value to encode- Throws:
IllegalArgumentException
- ifvalue
is negative
-
write
Encode the given value and write it to the givenOutputStream
.- Parameters:
out
- destination for the encoded valuevalue
- value to encode- Throws:
IOException
- if an I/O error occursNullPointerException
- ifout
is null
-
write
Encode the given value and write it to the givenByteBuffer
.- Parameters:
buf
- destination for the encoded valuevalue
- value to encode- Throws:
BufferOverflowException
- ifbuf
overflowsReadOnlyBufferException
- ifbuf
is read-onlyNullPointerException
- ifbuf
is null
-
read
Read and decode a value from the input.- Parameters:
reader
- input holding an encoded value- Returns:
- the decoded value, always non-negative
- Throws:
IllegalArgumentException
- if the encoded value is truncatedIllegalArgumentException
- if an invalid encoding is encountered
-
read
Read and decode a value from the givenInputStream
.- Parameters:
input
- input source for the encoded value- Returns:
- the decoded value
- Throws:
IOException
- if an I/O error occursEOFException
- if an unexpected EOF is encounteredIllegalArgumentException
- if an invalid encoding is encounteredNullPointerException
- ifinput
is null
-
read
Read and decode a value from the givenByteBuffer
.- Parameters:
buf
- input source for the encoded value- Returns:
- the decoded value
- Throws:
BufferUnderflowException
- ifbuf
underflowsIllegalArgumentException
- if an invalid encoding is encounteredNullPointerException
- ifbuf
is null
-
skip
Skip a value from the input.- Parameters:
reader
- input holding an encoded value
-
decodeLength
public static int decodeLength(int first) Determine the length (in bytes) of an encoded value based on the first byte.- Parameters:
first
- first byte of encoded value (in lower eight bits; other bits are ignored)- Returns:
- the length of the encoded value (including
first
) - Throws:
IllegalArgumentException
- if the lower eight bits offirst
equal0xff
-
encodeLength
public static int encodeLength(int value) Determine the length (in bytes) of the encoded value.- Parameters:
value
- value to encode- Returns:
- the length of the encoded value, a value between one and
MAX_ENCODED_LENGTH
- Throws:
IllegalArgumentException
- ifvalue
is negative
-
encode
public static int encode(int value, byte[] buf, int off) Encode the given value and write the encoded bytes into the given buffer.- Parameters:
value
- value to encodebuf
- output bufferoff
- starting offset into output buffer- Returns:
- the number of encoded bytes written
- Throws:
IllegalArgumentException
- ifvalue
is negativeNullPointerException
- ifbuf
is nullArrayIndexOutOfBoundsException
- ifoff
is negative or the encoded value exceeds the given buffer
-
main
Test routine.- Parameters:
args
- command line arguments
-