Class IntegerCodec


  • public abstract class IntegerCodec
    extends Object
    A IntegerCodec knows how to encode / decode / range-check integers using a particular encoding format.
    • 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
      • U24BE

        public static final IntegerCodec U24BE
        An unsigned 24-bit integer encoded in 3 bytes, MSB-first
      • U24LE

        public static final IntegerCodec U24LE
        An unsigned 24-bit integer encoded in 3 bytes, LSB-first
      • S8

        public static final IntegerCodec S8
        A signed 8-bit integer encoded in 1 byte
      • S16BE

        public static final IntegerCodec S16BE
        A signed 16-bit integer encoded in 2 bytes, MSB-first
      • S16LE

        public static final IntegerCodec S16LE
        A signed 16-bit integer encoded in 2 bytes, LSB-first
      • S24BE

        public static final IntegerCodec S24BE
        A signed 24-bit integer encoded in 3 bytes, MSB-first
      • S24LE

        public static final IntegerCodec S24LE
        A signed 24-bit integer encoded in 3 bytes, LSB-first
      • S32BE

        public static final IntegerCodec S32BE
        A signed 32-bit integer encoded in 4 bytes, MSB-first
      • S32LE

        public static final IntegerCodec S32LE
        A signed 32-bit integer encoded in 4 bytes, LSB-first
      • UVARLE

        public static final IntegerCodec UVARLE
        A variable-length unsigned MSB-first integer
      • UVARBE

        public static final IntegerCodec UVARBE
        A variable-length unsigned LSB-first integer
      • SVARLE

        public static final IntegerCodec SVARLE
        A variable-length signed MSB-first integer
      • SVARBE

        public static final IntegerCodec SVARBE
        A variable-length signed LSB-first integer
    • Constructor Detail

      • IntegerCodec

        protected IntegerCodec​(int min,
                               int max)
    • 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 value
        toArray - the array to populate
        start - the start index to begin encoding at
        len - 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 bytearray
        start - where to start decoding
        len - 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 - if value 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 encode
        max - the maximum value to encode
        width - the number of bytes to encode, or 0 to use a variable number of bytes
        bigEndian - if true, encode MSB-first; if false, encode LSB-first
        signed - 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