Class IntegerCodec
- java.lang.Object
-
- IntegerCodec
-
public abstract class IntegerCodec extends Object
A IntegerCodec knows how to encode / decode / range-check integers using a particular encoding format.
-
-
Field Summary
Fields Modifier and Type Field Description static IntegerCodec
S16BE
A signed 16-bit integer encoded in 2 bytes, MSB-firststatic IntegerCodec
S16BEIE
static IntegerCodec
S16LE
A signed 16-bit integer encoded in 2 bytes, LSB-firststatic IntegerCodec
S16LEIE
static IntegerCodec
S24BE
A signed 24-bit integer encoded in 3 bytes, MSB-firststatic IntegerCodec
S24BEIE
static IntegerCodec
S24LE
A signed 24-bit integer encoded in 3 bytes, LSB-firststatic IntegerCodec
S24LEIE
static IntegerCodec
S32BE
A signed 32-bit integer encoded in 4 bytes, MSB-firststatic IntegerCodec
S32BEIE
static IntegerCodec
S32LE
A signed 32-bit integer encoded in 4 bytes, LSB-firststatic IntegerCodec
S32LEIE
static IntegerCodec
S8
A signed 8-bit integer encoded in 1 bytestatic IntegerCodec
S8IE
static IntegerCodec
SVARBE
A variable-length signed LSB-first integerstatic IntegerCodec
SVARBEIE
static IntegerCodec
SVARLE
A variable-length signed MSB-first integerstatic IntegerCodec
SVARLEIE
static IntegerCodec
U16BE
An unsigned 16-bit integer encoded in 2 bytes, MSB-firststatic IntegerCodec
U16BEIE
An unsigned 16-bit integer encoded in 2 bytes, MSB-first, ignore extra octetsstatic IntegerCodec
U16LE
An unsigned 16-bit integer encoded in 2 bytes, LSB-firststatic IntegerCodec
U16LEIE
static IntegerCodec
U24BE
An unsigned 24-bit integer encoded in 3 bytes, MSB-firststatic IntegerCodec
U24BEIE
static IntegerCodec
U24LE
An unsigned 24-bit integer encoded in 3 bytes, LSB-firststatic IntegerCodec
U24LEIE
static IntegerCodec
U8
An unsigned 8-bit integer encoded in 1 bytestatic IntegerCodec
U8IE
An unsigned 8-bit integer encoded in 1 byte, ignore extra octetsstatic IntegerCodec
UVARBE
A variable-length unsigned LSB-first integerstatic IntegerCodec
UVARBEIE
static IntegerCodec
UVARLE
A variable-length unsigned MSB-first integerstatic IntegerCodec
UVARLEIE
-
Constructor Summary
Constructors Modifier Constructor Description protected
IntegerCodec(int min, int max)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description int
decode(byte[] data)
Decode the given bytearray to an integer value.abstract int
decode(byte[] data, int start, int len)
Decode a value from some part of a bytearrayabstract byte[]
encode(int value)
Encode the given value to a new bytearrayabstract int
encodeTo(int value, byte[] toArray, int start, int len)
Encode the given value to some location in an existing bytearraystatic IntegerCodec
getCodec(int min, int max, int width, boolean bigEndian, boolean signed, boolean ignoreExtraOctets)
Return a IntegerCodec instance for the given parameters.void
rangeCheck(int value)
Check if a given value is in the encodable range for this codec.
-
-
-
Field Detail
-
U8
public static final IntegerCodec U8
An unsigned 8-bit integer encoded in 1 byte
-
U8IE
public static final IntegerCodec U8IE
An unsigned 8-bit integer encoded in 1 byte, ignore extra octets
-
U16BE
public static final IntegerCodec U16BE
An unsigned 16-bit integer encoded in 2 bytes, MSB-first
-
U16BEIE
public static final IntegerCodec U16BEIE
An unsigned 16-bit integer encoded in 2 bytes, MSB-first, ignore extra octets
-
U16LE
public static final IntegerCodec U16LE
An unsigned 16-bit integer encoded in 2 bytes, LSB-first
-
U16LEIE
public static final IntegerCodec U16LEIE
-
U24BE
public static final IntegerCodec U24BE
An unsigned 24-bit integer encoded in 3 bytes, MSB-first
-
U24BEIE
public static final IntegerCodec U24BEIE
-
U24LE
public static final IntegerCodec U24LE
An unsigned 24-bit integer encoded in 3 bytes, LSB-first
-
U24LEIE
public static final IntegerCodec U24LEIE
-
S8
public static final IntegerCodec S8
A signed 8-bit integer encoded in 1 byte
-
S8IE
public static final IntegerCodec S8IE
-
S16BE
public static final IntegerCodec S16BE
A signed 16-bit integer encoded in 2 bytes, MSB-first
-
S16BEIE
public static final IntegerCodec S16BEIE
-
S16LE
public static final IntegerCodec S16LE
A signed 16-bit integer encoded in 2 bytes, LSB-first
-
S16LEIE
public static final IntegerCodec S16LEIE
-
S24BE
public static final IntegerCodec S24BE
A signed 24-bit integer encoded in 3 bytes, MSB-first
-
S24BEIE
public static final IntegerCodec S24BEIE
-
S24LE
public static final IntegerCodec S24LE
A signed 24-bit integer encoded in 3 bytes, LSB-first
-
S24LEIE
public static final IntegerCodec S24LEIE
-
S32BE
public static final IntegerCodec S32BE
A signed 32-bit integer encoded in 4 bytes, MSB-first
-
S32BEIE
public static final IntegerCodec S32BEIE
-
S32LE
public static final IntegerCodec S32LE
A signed 32-bit integer encoded in 4 bytes, LSB-first
-
S32LEIE
public static final IntegerCodec S32LEIE
-
UVARLE
public static final IntegerCodec UVARLE
A variable-length unsigned MSB-first integer
-
UVARLEIE
public static final IntegerCodec UVARLEIE
-
UVARBE
public static final IntegerCodec UVARBE
A variable-length unsigned LSB-first integer
-
UVARBEIE
public static final IntegerCodec UVARBEIE
-
SVARLE
public static final IntegerCodec SVARLE
A variable-length signed MSB-first integer
-
SVARLEIE
public static final IntegerCodec SVARLEIE
-
SVARBE
public static final IntegerCodec SVARBE
A variable-length signed LSB-first integer
-
SVARBEIE
public static final IntegerCodec SVARBEIE
-
-
Method Detail
-
decode
public int decode(byte[] data)
Decode the given bytearray to an integer value.- Parameters:
data
- the bytearray- Returns:
- the integer value
-
encode
public abstract byte[] encode(int value)
Encode the given value to a new bytearray- Parameters:
value
- the value- Returns:
- a new bytearray
-
encodeTo
public abstract int encodeTo(int value, byte[] toArray, int start, int len)
Encode the given value to some location in an existing bytearray- Parameters:
value
- the valuetoArray
- the array to populatestart
- the start index to begin encoding atlen
- the maximum number of bytes to encode to- Returns:
- the number of bytes actually used
-
decode
public abstract int decode(byte[] data, int start, int len)
Decode a value from some part of a bytearray- Parameters:
data
- the bytearraystart
- where to start decodinglen
- how many bytes to decode
-
rangeCheck
public void rangeCheck(int value) throws EncodeException
Check if a given value is in the encodable range for this codec.- Parameters:
value
- the value to check- Throws:
EncodeException
- ifvalue
is out of range
-
getCodec
public static IntegerCodec getCodec(int min, int max, int width, boolean bigEndian, boolean signed, boolean ignoreExtraOctets)
Return a IntegerCodec instance for the given parameters. This may be a shared instance for the more common codecs, or a new instance otherwise.- Parameters:
min
- the minimum value to encodemax
- the maximum value to encodewidth
- the number of bytes to encode, or 0 to use a variable number of bytesbigEndian
- if true, encode MSB-first; if false, encode LSB-firstsigned
- if true, treat the most significant bit as a sign bit; if false, treat the value as unsigned- Returns:
- an appropriate codec instance
- Throws:
IllegalArgumentException
- if min < max, or if min < 0 and !signed, or if width < 0
-
-