Class EndianUtil


  • public final class EndianUtil
    extends Object
    Method to help with Little Endian / Big Endian conversions for the Sim0MQ messages. All Sim0MQ messages are encoded Big Endian over the wire.

    Copyright (c) 2016-2017 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
    BSD-style license. See Sim0MQ License.

    Author:
    Alexander Verbraeck
    • Field Detail

      • BIG_ENDIAN

        public static final EndianUtil BIG_ENDIAN
        Directly usable bigEndian EndianUtil.
      • LITTLE_ENDIAN

        public static final EndianUtil LITTLE_ENDIAN
        Directly usable littleEndian EndianUtil.
    • Method Detail

      • isPlatformBigEndian

        public static boolean isPlatformBigEndian()
        Report whether this platform is bigEndian, or littleEndian.
        Returns:
        boolean; true if this platform is bigEndian; false if this platform is littleEndian
      • bigEndian

        public static EndianUtil bigEndian()
        Construct an EndianUtil object that uses bigEndian encoding.
        Returns:
        EndianUtil that uses bigEndian encoding
      • littleEndian

        public static EndianUtil littleEndian()
        Construct an EndianUtil object that uses littleEndian encoding.
        Returns:
        EndianUtil that uses littleEndian encoding
      • isBigEndian

        public boolean isBigEndian()
        Report if this EndianUtil is bigEndian.
        Returns:
        bigEndian boolean; true if this EndianUtil is bigEndian; false if this EndianUtil is littleEndian
      • decodeShort

        public short decodeShort​(byte[] message,
                                 int pointer)
        Decode a short.
        Parameters:
        message - byte[]; the ZeroMQ byte array to decode
        pointer - int; the first byte to consider
        Returns:
        the short value
      • decodeInt

        public int decodeInt​(byte[] message,
                             int pointer)
        Decode a int.
        Parameters:
        message - byte[]; the ZeroMQ byte array to decode
        pointer - int; the first byte to consider
        Returns:
        the integer value
      • decodeLong

        public long decodeLong​(byte[] message,
                               int pointer)
        Decode a long.
        Parameters:
        message - byte[]; the ZeroMQ byte array to decode
        pointer - int; the first byte to consider
        Returns:
        the long value
      • decodeFloat

        public float decodeFloat​(byte[] message,
                                 int pointer)
        Decode a float.
        Parameters:
        message - byte[]; the ZeroMQ byte array to decode
        pointer - int; the first byte to consider
        Returns:
        the float value
      • decodeDouble

        public double decodeDouble​(byte[] message,
                                   int pointer)
        Decode a double.
        Parameters:
        message - byte[]; the ZeroMQ byte array to decode
        pointer - int; the first byte to consider
        Returns:
        the double value
      • decodeChar

        public char decodeChar​(byte[] message,
                               int pointer)
        Decode a char (16 bits).
        Parameters:
        message - byte[]; the ZeroMQ byte array to decode
        pointer - int; the first byte to consider
        Returns:
        the short value
      • decodeUTF8String

        public String decodeUTF8String​(byte[] message,
                                       int pointer)
                                throws SerializationException
        Decode a String including the length int from the message byte array.
        Parameters:
        message - byte[]; the message byte array
        pointer - int; the start position in the array
        Returns:
        the Java String at position pointer
        Throws:
        SerializationException - when the bytes cannot be parsed as UTF8
      • decodeUTF16String

        public String decodeUTF16String​(byte[] message,
                                        int pointer)
        Decode a String including the length int from the message byte array.
        Parameters:
        message - byte[]; the message byte array
        pointer - int; the start position in the array
        Returns:
        the Java String at position pointer
      • encodeShort

        public int encodeShort​(short v,
                               byte[] message,
                               int pointer)
        Encode a short into a message buffer.
        Parameters:
        v - short; the variable to encode
        message - byte[]; the message buffer to encode the variable into
        pointer - int; the pointer to start writing
        Returns:
        the new pointer after writing
      • encodeChar

        public int encodeChar​(char v,
                              byte[] message,
                              int pointer)
        Encode a char (16 bits) into a message buffer.
        Parameters:
        v - char; the variable to encode
        message - byte[]; the message buffer to encode the variable into
        pointer - int; the pointer to start writing
        Returns:
        the new pointer after writing
      • encodeInt

        public void encodeInt​(int v,
                              byte[] message,
                              int pointer)
        Encode a int into a message buffer.
        Parameters:
        v - int; the variable to encode
        message - byte[]; the message buffer to encode the variable into
        pointer - int; the pointer to start writing
      • encodeLong

        public int encodeLong​(long v,
                              byte[] message,
                              int pointer)
        Encode a long into a message buffer.
        Parameters:
        v - long; the variable to encode
        message - byte[]; the message buffer to encode the variable into
        pointer - int; the pointer to start writing
        Returns:
        the new pointer after writing
      • encodeFloat

        public void encodeFloat​(float v,
                                byte[] message,
                                int pointer)
        Encode a float into a message buffer.
        Parameters:
        v - float; the variable to encode
        message - byte[]; the message buffer to encode the variable into
        pointer - int; the pointer to start writing
      • encodeDouble

        public int encodeDouble​(double v,
                                byte[] message,
                                int pointer)
        Encode a double into a message buffer.
        Parameters:
        v - double; the variable to encode
        message - byte[]; the message buffer to encode the variable into
        pointer - int; the pointer to start writing
        Returns:
        the new pointer after writing