Package org.djutils.serialization
Class TypedMessage
java.lang.Object
org.djutils.serialization.TypedMessage
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
-
Nested Class Summary
Modifier and TypeClassDescription(package private) static class
Minimal implementation of SerializableObject. -
Field Summary
Modifier and TypeFieldDescription(package private) static final Map<Byte,
Serializer<?>> All the converters that decode into primitive data when possible, keyed by prefix.protected static final Charset
the UTF-16 charset, big endian variant.protected static final Charset
the UTF-8 charset. -
Method Summary
Modifier and TypeMethodDescription(package private) static Serializer<?>[]
buildEncoderList
(boolean utf8, Object... content) Build the list of serializers corresponding to the data in an Object array.static Object[]
decode
(byte[] buffer, Map<Byte, Serializer<?>> decoderMap, EndianUtil endianUtil) Decode the message into an object array.static Object[]
decodeToObjectDataTypes
(byte[] buffer, EndianUtil endianUtil) Decode the message into an object array, constructing Java Object arrays and matrices where possible.static Object[]
decodeToPrimitiveDataTypes
(byte[] buffer, EndianUtil endianUtil) Decode the message into an object array, constructing Java Primitive data arrays and matrices where possible.static byte[]
encodeUTF16
(EndianUtil endianUtil, Object... content) Encode the object array into a byte[] message.static byte[]
encodeUTF8
(EndianUtil endianUtil, Object... content) Encode the object array into a byte[] message.(package private) static Serializer<?>
findEncoder
(boolean utf8, Object object) Find the serializer for one object.static <U extends org.djunits.unit.Unit<U>>
UgetUnit
(byte[] buffer, Pointer pointer, EndianUtil endianUtil) Retrieve and decode a DJUNITS unit.
-
Field Details
-
PRIMITIVE_DATA_DECODERS
All the converters that decode into primitive data when possible, keyed by prefix. -
UTF8
the UTF-8 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 valuescontent
- 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 valuescontent
- Object...; the objects to encode- Returns:
- the zeroMQ message to send as a byte array
- Throws:
SerializationException
- on unknown data type
-
findEncoder
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 Stringsobject
- 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 forobject
-
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 Stringscontent
- 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 incontent
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 decodeendianUtil
- 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 decodeendianUtil
- 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 SerializationExceptionDecode the message into an object array.- Parameters:
buffer
- byte[]; the byte array to decodedecoderMap
- Map<Byte, Serializer<?>>; the map with decoders to useendianUtil
- 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 datapointer
- Pointer; position in the encoded data where the unit is to be decoded fromendianUtil
- EndianUtil; decoder for multi-byte values- Returns:
- U; decoded Unit
-