Class EndianUtil

java.lang.Object
org.djutils.serialization.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 Summary

    Fields 
    Modifier and Type Field Description
    static EndianUtil BIG_ENDIAN
    Directly usable bigEndian EndianUtil.
    static EndianUtil LITTLE_ENDIAN
    Directly usable littleEndian EndianUtil.
  • Method Summary

    Modifier and Type Method Description
    static EndianUtil bigEndian()
    Construct an EndianUtil object that uses bigEndian encoding.
    char decodeChar​(byte[] message, int pointer)
    Decode a char (16 bits).
    double decodeDouble​(byte[] message, int pointer)
    Decode a double.
    float decodeFloat​(byte[] message, int pointer)
    Decode a float.
    int decodeInt​(byte[] message, int pointer)
    Decode a int.
    long decodeLong​(byte[] message, int pointer)
    Decode a long.
    short decodeShort​(byte[] message, int pointer)
    Decode a short.
    String decodeUTF16String​(byte[] message, int pointer)
    Decode a String including the length int from the message byte array.
    String decodeUTF8String​(byte[] message, int pointer)
    Decode a String including the length int from the message byte array.
    int encodeChar​(char v, byte[] message, int pointer)
    Encode a char (16 bits) into a message buffer.
    int encodeDouble​(double v, byte[] message, int pointer)
    Encode a double into a message buffer.
    void encodeFloat​(float v, byte[] message, int pointer)
    Encode a float into a message buffer.
    void encodeInt​(int v, byte[] message, int pointer)
    Encode a int into a message buffer.
    int encodeLong​(long v, byte[] message, int pointer)
    Encode a long into a message buffer.
    int encodeShort​(short v, byte[] message, int pointer)
    Encode a short into a message buffer.
    boolean isBigEndian()
    Report if this EndianUtil is bigEndian.
    static boolean isPlatformBigEndian()
    Report whether this platform is bigEndian, or littleEndian.
    static EndianUtil littleEndian()
    Construct an EndianUtil object that uses littleEndian encoding.
    String toString()  

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

  • Method Details

    • 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 - the ZeroMQ byte array to decode
      pointer - the first byte to consider
      Returns:
      the short value
    • decodeInt

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

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

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

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

      public char decodeChar​(byte[] message, int pointer)
      Decode a char (16 bits).
      Parameters:
      message - the ZeroMQ byte array to decode
      pointer - 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 - the message byte array
      pointer - 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 - the message byte array
      pointer - 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 - the variable to encode
      message - the message buffer to encode the variable into
      pointer - 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 - the variable to encode
      message - the message buffer to encode the variable into
      pointer - 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 - the variable to encode
      message - the message buffer to encode the variable into
      pointer - the pointer to start writing
    • encodeLong

      public int encodeLong​(long v, byte[] message, int pointer)
      Encode a long into a message buffer.
      Parameters:
      v - the variable to encode
      message - the message buffer to encode the variable into
      pointer - 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 - the variable to encode
      message - the message buffer to encode the variable into
      pointer - the pointer to start writing
    • encodeDouble

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

      public String toString()
      Overrides:
      toString in class Object