Package org.djutils.serialization
Class EndianUtil
java.lang.Object
org.djutils.serialization.EndianUtil
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
Modifier and TypeFieldDescriptionstatic final EndianUtil
Directly usable bigEndian EndianUtil.static final EndianUtil
Directly usable littleEndian EndianUtil. -
Method Summary
Modifier and TypeMethodDescriptionstatic EndianUtil
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.decodeUTF16String
(byte[] message, int pointer) Decode a String including the length int from the message byte array.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
Report if this EndianUtil is bigEndian.static boolean
Report whether this platform is bigEndian, or littleEndian.static EndianUtil
Construct an EndianUtil object that uses littleEndian encoding.toString()
-
Field Details
-
BIG_ENDIAN
Directly usable bigEndian EndianUtil. -
LITTLE_ENDIAN
Directly usable littleEndian EndianUtil.
-
-
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
Construct an EndianUtil object that uses bigEndian encoding.- Returns:
- EndianUtil that uses bigEndian encoding
-
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 decodepointer
- 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 decodepointer
- 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 decodepointer
- 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 decodepointer
- 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 decodepointer
- 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 decodepointer
- int; the first byte to consider- Returns:
- the short value
-
decodeUTF8String
Decode a String including the length int from the message byte array.- Parameters:
message
- byte[]; the message byte arraypointer
- 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
Decode a String including the length int from the message byte array.- Parameters:
message
- byte[]; the message byte arraypointer
- 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 encodemessage
- byte[]; the message buffer to encode the variable intopointer
- 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 encodemessage
- byte[]; the message buffer to encode the variable intopointer
- 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 encodemessage
- byte[]; the message buffer to encode the variable intopointer
- 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 encodemessage
- byte[]; the message buffer to encode the variable intopointer
- 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 encodemessage
- byte[]; the message buffer to encode the variable intopointer
- 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 encodemessage
- byte[]; the message buffer to encode the variable intopointer
- int; the pointer to start writing- Returns:
- the new pointer after writing
-
toString
-