Class StandardMessageCodec
- All Implemented Interfaces:
MessageCodec<Object>
This codec is guaranteed to be compatible with the corresponding StandardMessageCodec on the Dart side. These parts of the Flutter SDK are evolved synchronously.
Supported messages are acyclic values of these forms:
- null
- Booleans
- Bytes, Shorts, Integers, Longs
- BigIntegers (see below)
- Floats, Doubles
- Strings
- byte[], int[], long[], float[], double[]
- Lists of supported values
- Maps with supported keys and values
On the Dart side, these values are represented as follows:
- null: null
- Boolean: bool
- Byte, Short, Integer, Long: int
- Float, Double: double
- String: String
- byte[]: Uint8List
- int[]: Int32List
- long[]: Int64List
- float[]: Float32List
- double[]: Float64List
- List: List
- Map: Map
BigIntegers are represented in Dart as strings with the hexadecimal representation of the integer's value.
To extend the codec, overwrite the writeValue and readValueOfType methods.
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptiondecodeMessage
(ByteBuffer message) Decodes the specified message from binary.encodeMessage
(Object message) Encodes the specified message into binary.protected static final void
readAlignment
(ByteBuffer buffer, int alignment) Reads alignment padding bytes as written by writeAlignment.protected static final byte[]
readBytes
(ByteBuffer buffer) Reads a byte array as written by writeBytes.protected static final int
readSize
(ByteBuffer buffer) Reads an int representing a size as written by writeSize.protected final Object
readValue
(ByteBuffer buffer) Reads a value as written by writeValue.protected Object
readValueOfType
(byte type, ByteBuffer buffer) Reads a value of the specified type.protected static final void
writeAlignment
(ByteArrayOutputStream stream, int alignment) Writes a number of padding bytes to the specified stream to ensure that the next value is aligned to a whole multiple of the specified alignment.protected static final void
writeBytes
(ByteArrayOutputStream stream, byte[] bytes) Writes the length and then the actual bytes of the specified array to the specified stream.protected static final void
writeChar
(ByteArrayOutputStream stream, int value) Writes the least significant two bytes of the specified int to the specified stream.protected static final void
writeDouble
(ByteArrayOutputStream stream, double value) Writes the specified double as 8 bytes to the specified stream.protected static final void
writeFloat
(ByteArrayOutputStream stream, float value) Writes the specified double as 4 bytes to the specified streamprotected static final void
writeInt
(ByteArrayOutputStream stream, int value) Writes the specified int as 4 bytes to the specified stream.protected static final void
writeLong
(ByteArrayOutputStream stream, long value) Writes the specified long as 8 bytes to the specified stream.protected static final void
writeSize
(ByteArrayOutputStream stream, int value) Writes an int representing a size to the specified stream.protected void
writeValue
(ByteArrayOutputStream stream, Object value) Writes a type discriminator byte and then a byte serialization of the specified value to the specified stream.
-
Field Details
-
INSTANCE
-
-
Constructor Details
-
StandardMessageCodec
public StandardMessageCodec()
-
-
Method Details
-
encodeMessage
Description copied from interface:MessageCodec
Encodes the specified message into binary.- Specified by:
encodeMessage
in interfaceMessageCodec<Object>
- Parameters:
message
- the T message, possibly null.- Returns:
- a ByteBuffer containing the encoding between position 0 and the current position, or null, if message is null.
-
decodeMessage
Description copied from interface:MessageCodec
Decodes the specified message from binary.Warning: The ByteBuffer is "direct" and it won't be valid beyond this call. Storing the ByteBuffer and using it later and will lead to a
java.nio.BufferUnderflowException
. If you want to retain the data you'll need to copy it.- Specified by:
decodeMessage
in interfaceMessageCodec<Object>
- Parameters:
message
- theByteBuffer
message, possibly null.- Returns:
- a T value representation of the bytes between the given buffer's current position and its limit, or null, if message is null.
-
writeSize
Writes an int representing a size to the specified stream. Uses an expanding code of 1 to 5 bytes to optimize for small values. -
writeChar
Writes the least significant two bytes of the specified int to the specified stream. -
writeInt
Writes the specified int as 4 bytes to the specified stream. -
writeLong
Writes the specified long as 8 bytes to the specified stream. -
writeFloat
Writes the specified double as 4 bytes to the specified stream -
writeDouble
Writes the specified double as 8 bytes to the specified stream. -
writeBytes
protected static final void writeBytes(@NonNull ByteArrayOutputStream stream, @NonNull byte[] bytes) Writes the length and then the actual bytes of the specified array to the specified stream. -
writeAlignment
Writes a number of padding bytes to the specified stream to ensure that the next value is aligned to a whole multiple of the specified alignment. An example usage with alignment = 8 is to ensure doubles are word-aligned in the stream. -
writeValue
Writes a type discriminator byte and then a byte serialization of the specified value to the specified stream.Subclasses can extend the codec by overriding this method, calling super for values that the extension does not handle.
-
readSize
Reads an int representing a size as written by writeSize. -
readBytes
Reads a byte array as written by writeBytes. -
readAlignment
Reads alignment padding bytes as written by writeAlignment. -
readValue
Reads a value as written by writeValue. -
readValueOfType
Reads a value of the specified type.Subclasses may extend the codec by overriding this method, calling super for types that the extension does not handle.
-