Package org.djutils.serialization
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-2021 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
Nested Classes Modifier and Type Class Description (package private) static class
TypedMessage.MinimalSerializableObject
Minimal implementation of SerializableObject.
-
Field Summary
Fields Modifier and Type Field Description (package private) static Map<Byte,Serializer<?>>
PRIMITIVE_DATA_DECODERS
All the converters that decode into primitive data when possible, keyed by prefix.protected static Charset
UTF16
the UTF-16 charset, big endian variant.protected static Charset
UTF8
the UTF-8 charset.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description (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 Detail
-
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 Detail
-
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
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 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 SerializationException
Decode 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
-
-