public class CodecM32 extends Object
The M32 coding scheme provides a way of encoding standard integer values into a sequence of between 1 and 5 bytes, depending on the magnitude of the input value. The scheme is advantageous integer distributions with substantially more one-byte values than multi-byte values. In fact, for values greater than 126 or less than -126, the resulting storage will be at least 2 bytes. And values greater than 254 or less than -254, require at least 3 bytes. So for those integers that could be represented by the two-byte "short" data type, this method is actually disadvantageous.
The M32 coding scheme resembles the variable-length integer coding scheme used in SQLLite, except that it supports both positive and negative integer values.
Modifier and Type | Field and Description |
---|---|
static int |
MAX_BYTES_PER_VALUE
The maximum number of bytes that would be needed to encode
large-magnitude symbols
|
Constructor and Description |
---|
CodecM32(byte[] buffer,
int offset,
int length)
Construct an instance using the specified buffer for storing data.
|
CodecM32(int symbolCount)
Construct an instance with a buffer large enough to hold the specified
number of integer values.
|
Modifier and Type | Method and Description |
---|---|
int |
decode()
Decodes the next value in the buffer, advancing the buffer position as
appropriate.
|
void |
encode(int value)
Encodes the specified value and appends it to the buffer.
|
int |
getEncodedLength()
Gets the the length of the encoded data stored in the buffer
|
byte[] |
getEncoding()
Get a safe copy of the content of the buffer.
|
int |
remaining()
Gets the number of bytes remaining in the buffer.
|
void |
rewind()
Reset the buffer position to its beginning position
|
public static final int MAX_BYTES_PER_VALUE
public CodecM32(int symbolCount)
symbolCount
- the number of values to be stored in the buffer.public CodecM32(byte[] buffer, int offset, int length)
buffer
- a valid byte array dimensioned to at least offset+lengthoffset
- the starting position for storing data in the buffer.length
- the limit of the number bytes to be stored in the buffer.public void rewind()
public byte[] getEncoding()
public int getEncodedLength()
public void encode(int value)
Because this method is written to be used in tight loops with intense processing, there is no bounds checking performed on the internal buffer. Application developers must take responsibility for their own safety of implementation issues.
value
- any integer value to be encoded (a signed integer that can
be stored with 6 bytes)public int decode()
Because this method is written to be used in tight loops with intense processing, there is no bounds checking performed on the internal buffer.
public int remaining()
Copyright © 2022. All rights reserved.