Class TypedMessage

java.lang.Object
org.djutils.serialization.TypedMessage

public final class TypedMessage extends Object
Message conversions. These take into account the endianness for coding the different values. Java is by default big-endian.

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

Author:
Alexander Verbraeck, Peter Knoppers
  • Field Details

    • PRIMITIVE_DATA_DECODERS

      static final Map<Byte,Serializer<?>> PRIMITIVE_DATA_DECODERS
      All the converters that decode into primitive data when possible, keyed by prefix.
    • UTF8

      protected static final Charset UTF8
      the UTF-8 charset.
    • UTF16

      protected static final Charset UTF16
      the UTF-16 charset, big endian variant.
  • Method Details

    • encodeUTF8

      public static byte[] encodeUTF8(EndianUtil endianUtil, Object... content) throws SerializationException
      Encode the object array into a byte[] message. Use UTF8 for the characters and for the String.
      Parameters:
      endianUtil - EndianUtil; encoder to use for multi-byte values
      content - Object...; the objects to encode
      Returns:
      the zeroMQ message to send as a byte array
      Throws:
      SerializationException - on unknown data type
    • encodeUTF16

      public static byte[] encodeUTF16(EndianUtil endianUtil, Object... content) throws SerializationException
      Encode the object array into a byte[] message. Use UTF16 for the characters and for the String.
      Parameters:
      endianUtil - EndianUtil; encoder for multi-byte values
      content - Object...; the objects to encode
      Returns:
      the zeroMQ message to send as a byte array
      Throws:
      SerializationException - on unknown data type
    • findEncoder

      static Serializer<?> findEncoder(boolean utf8, Object object) throws SerializationException
      Find the serializer for one object.
      Parameters:
      utf8 - boolean; if true; use UTF8 encoding for characters and Strings; if false; use UTF16 encoding for characters and Strings
      object - Object; the object for which the serializer must be returned
      Returns:
      Serializer; the serializer needed for object
      Throws:
      SerializationException - when there is no known serializer for object
    • buildEncoderList

      static Serializer<?>[] buildEncoderList(boolean utf8, Object... content) throws SerializationException
      Build the list of serializers corresponding to the data in an Object array.
      Parameters:
      utf8 - boolean; if true; use UTF8 encoding for characters and Strings; if false; use UTF16 encoding for characters and Strings
      content - Object...; the objects for which the serializers must be returned
      Returns:
      Serializer[]; array filled with the serializers needed for the objects in the Object array
      Throws:
      SerializationException - when an object in content cannot be serialized
    • decodeToPrimitiveDataTypes

      public static Object[] decodeToPrimitiveDataTypes(byte[] buffer, EndianUtil endianUtil) throws SerializationException
      Decode the message into an object array, constructing Java Primitive data arrays and matrices where possible.
      Parameters:
      buffer - byte[]; the byte array to decode
      endianUtil - EndianUtil; decoder for multi-byte values
      Returns:
      an array of objects of the right type
      Throws:
      SerializationException - on unknown data type
    • decodeToObjectDataTypes

      public static Object[] decodeToObjectDataTypes(byte[] buffer, EndianUtil endianUtil) throws SerializationException
      Decode the message into an object array, constructing Java Object arrays and matrices where possible.
      Parameters:
      buffer - byte[]; the byte array to decode
      endianUtil - EndianUtil; decoder for multi-byte values
      Returns:
      an array of objects of the right type
      Throws:
      SerializationException - on unknown data type
    • decode

      public static Object[] decode(byte[] buffer, Map<Byte,Serializer<?>> decoderMap, EndianUtil endianUtil) throws SerializationException
      Decode the message into an object array.
      Parameters:
      buffer - byte[]; the byte array to decode
      decoderMap - Map<Byte, Serializer<?>>; the map with decoders to use
      endianUtil - EndianUtil; decoder for multi-byte values
      Returns:
      an array of objects of the right type
      Throws:
      SerializationException - on unknown data type
    • getUnit

      public static <U extends org.djunits.unit.Unit<U>> U getUnit(byte[] buffer, Pointer pointer, EndianUtil endianUtil)
      Retrieve and decode a DJUNITS unit.
      Type Parameters:
      U - the unit type
      Parameters:
      buffer - byte[]; the encoded data
      pointer - Pointer; position in the encoded data where the unit is to be decoded from
      endianUtil - EndianUtil; decoder for multi-byte values
      Returns:
      U; decoded Unit