Class ValueBuffer


  • @NonNullByDefault
    public class ValueBuffer
    extends Object
    ByteBuffer-like interface for working with different types of data stored in byte arrays
    Author:
    Sami Salonen - Initial contribution
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      byte[] array()
      Returns underlying bytes
      ValueBuffer get​(byte[] dst)
      Starting from current position, read dst.length number of bytes and copy the data to dst
      float getFloat32()
      Extract single-precision 32-bit IEEE 754 floating point at current position, and advance position.
      float getFloat32Swap()
      Extract single-precision 32-bit IEEE 754 floating point at current position, and advance position.
      short getSInt16()
      Extract signed 16-bit integer at current position, and advance position.
      int getSInt32()
      Extract signed 32-bit integer at current position, and advance position.
      int getSInt32Swap()
      Extract signed 32-bit integer at current position, and advance position.
      long getSInt64()
      Extract signed 64-bit integer at current position, and advance position.
      long getSInt64Swap()
      Extract signed 64-bit integer at current position, and advance position.
      byte getSInt8()
      Extract signed 8-bit integer at current position, and advance position.
      int getUInt16()
      Extract unsigned 16-bit integer at current position, and advance position.
      long getUInt32()
      Extract unsigned 32-bit integer at current position, and advance position.
      long getUInt32Swap()
      Extract unsigned 32-bit integer at current position, and advance position.
      BigInteger getUInt64()
      Extract unsigned 64-bit integer at current position, and advance position.
      BigInteger getUInt64Swap()
      Extract unsigned 64-bit integer at current position, and advance position.
      short getUInt8()
      Extract unsigned 8-bit integer at current position, and advance position.
      boolean hasRemaining()
      Tells whether there are any bytes left between current position and the end
      ValueBuffer mark()
      Sets this buffer's mark at its position.
      int position()
      Returns this buffer's position.
      ValueBuffer position​(int byteIndex)
      Sets this buffer's position.
      int remaining()
      Returns the number of bytes between the current position and the end.
      ValueBuffer reset()
      Resets this buffer's position to the previously-marked position.
      static ValueBuffer wrap​(byte[] array)
      Wrap given bytes and create a new instance of ValueBuffer The instance will have position of 0.
      static ValueBuffer wrap​(ModbusRegisterArray array)
      Wrap modbus registers and create a new instance of ValueBuffer The instance will have position of 0.
    • Method Detail

      • wrap

        public static ValueBuffer wrap​(ModbusRegisterArray array)
        Wrap modbus registers and create a new instance of ValueBuffer The instance will have position of 0.
        Parameters:
        array - set of registers
        Returns:
        new instance of ValueBuffer referencing bytes represented by modbus register array
      • wrap

        public static ValueBuffer wrap​(byte[] array)
        Wrap given bytes and create a new instance of ValueBuffer The instance will have position of 0.
        Parameters:
        array - set of bytes to wrap
        Returns:
        new instance of ValueBuffer referencing bytes
      • position

        public int position()
        Returns this buffer's position.
        Returns:
        The position of this buffer
      • position

        public ValueBuffer position​(int byteIndex)
        Sets this buffer's position. If the mark is defined and larger than the new position then it is discarded.
        Returns:
        this buffer
      • mark

        public ValueBuffer mark()
        Sets this buffer's mark at its position.
        Returns:
        this buffer
      • reset

        public ValueBuffer reset()
                          throws InvalidMarkException
        Resets this buffer's position to the previously-marked position. Invoking this method neither changes nor discards the mark's value.
        Returns:
        this buffer
        Throws:
        InvalidMarkException - If the mark has not been set
      • remaining

        public int remaining()
        Returns the number of bytes between the current position and the end.
        Returns:
        The number of bytes remaining in this buffer
      • array

        public byte[] array()
        Returns underlying bytes
        Returns:
        reference to underlying bytes
      • hasRemaining

        public boolean hasRemaining()
        Tells whether there are any bytes left between current position and the end
        Returns:
        true if, and only if, there is at least one byte remaining in this buffer
      • get

        public ValueBuffer get​(byte[] dst)
        Starting from current position, read dst.length number of bytes and copy the data to dst
        Parameters:
        dst - copied bytes
        Returns:
        this buffer
        Throws:
        BufferOverflowException - If there is insufficient space in this buffer for the remaining bytes in the source buffer
      • getSInt8

        public byte getSInt8()
        Extract signed 8-bit integer at current position, and advance position.
        Returns:
        signed 8-bit integer (byte)
        Throws:
        IllegalArgumentException - when there are not enough bytes in this ValueBuffer
        See Also:
        ModbusBitUtilities.extractSInt8
      • getUInt8

        public short getUInt8()
        Extract unsigned 8-bit integer at current position, and advance position.
        Returns:
        unsigned 8-bit integer
        Throws:
        IllegalArgumentException - when there are not enough bytes in this ValueBuffer
        See Also:
        ModbusBitUtilities.extractUInt8
      • getSInt16

        public short getSInt16()
        Extract signed 16-bit integer at current position, and advance position.
        Returns:
        signed 16-bit integer (short)
        Throws:
        IllegalArgumentException - when there are not enough bytes in this ValueBuffer
        See Also:
        ModbusBitUtilities.extractSInt16
      • getUInt16

        public int getUInt16()
        Extract unsigned 16-bit integer at current position, and advance position.
        Returns:
        unsigned 16-bit integer
        Throws:
        IllegalArgumentException - when there are not enough bytes in this ValueBuffer
        See Also:
        ModbusBitUtilities.extractUInt16
      • getSInt32

        public int getSInt32()
        Extract signed 32-bit integer at current position, and advance position.
        Returns:
        signed 32-bit integer
        Throws:
        IllegalArgumentException - when there are not enough bytes in this ValueBuffer
        See Also:
        ModbusBitUtilities.extractSInt32
      • getUInt32

        public long getUInt32()
        Extract unsigned 32-bit integer at current position, and advance position.
        Returns:
        unsigned 32-bit integer
        Throws:
        IllegalArgumentException - when there are not enough bytes in this ValueBuffer
        See Also:
        ModbusBitUtilities.extractUInt32
      • getSInt32Swap

        public int getSInt32Swap()
        Extract signed 32-bit integer at current position, and advance position. This is identical with getSInt32, but with registers swapped.
        Returns:
        signed 32-bit integer
        Throws:
        IllegalArgumentException - when there are not enough bytes in this ValueBuffer
        See Also:
        ModbusBitUtilities.extractSInt32Swap
      • getUInt32Swap

        public long getUInt32Swap()
        Extract unsigned 32-bit integer at current position, and advance position. This is identical with getUInt32, but with registers swapped.
        Returns:
        unsigned 32-bit integer
        Throws:
        IllegalArgumentException - when there are not enough bytes in this ValueBuffer
        See Also:
        ModbusBitUtilities.extractUInt32Swap
      • getSInt64

        public long getSInt64()
        Extract signed 64-bit integer at current position, and advance position.
        Returns:
        signed 64-bit integer
        Throws:
        IllegalArgumentException - when there are not enough bytes in this ValueBuffer
        See Also:
        ModbusBitUtilities.extractInt64
      • getUInt64

        public BigInteger getUInt64()
        Extract unsigned 64-bit integer at current position, and advance position.
        Returns:
        unsigned 64-bit integer
        Throws:
        IllegalArgumentException - when there are not enough bytes in this ValueBuffer
        See Also:
        ModbusBitUtilities.extractUInt64
      • getSInt64Swap

        public long getSInt64Swap()
        Extract signed 64-bit integer at current position, and advance position. This is identical with getSInt64, but with registers swapped.
        Returns:
        signed 64-bit integer
        Throws:
        IllegalArgumentException - when there are not enough bytes in this ValueBuffer
        See Also:
        ModbusBitUtilities.extractInt64Swap
      • getUInt64Swap

        public BigInteger getUInt64Swap()
        Extract unsigned 64-bit integer at current position, and advance position. This is identical with getUInt64, but with registers swapped.
        Returns:
        unsigned 64-bit integer
        Throws:
        IllegalArgumentException - when there are not enough bytes in this ValueBuffer
        See Also:
        ModbusBitUtilities.extractUInt64Swap
      • getFloat32

        public float getFloat32()
        Extract single-precision 32-bit IEEE 754 floating point at current position, and advance position. Note that this method can return floating point NaN and floating point infinity.
        Returns:
        single-precision 32-bit IEEE 754 floating point
        Throws:
        IllegalArgumentException - when there are not enough bytes in this ValueBuffer
        See Also:
        ModbusBitUtilities.extractFloat32
      • getFloat32Swap

        public float getFloat32Swap()
        Extract single-precision 32-bit IEEE 754 floating point at current position, and advance position. This is identical with getFloat32, but with registers swapped. Note that this method can return floating point NaN and floating point infinity.
        Returns:
        single-precision 32-bit IEEE 754 floating point
        Throws:
        IllegalArgumentException - when there are not enough bytes in this ValueBuffer
        See Also:
        ModbusBitUtilities.extractFloat32