Class StandardMessageCodec
- java.lang.Object
-
- io.flutter.plugin.common.StandardMessageCodec
-
- All Implemented Interfaces:
MessageCodec<Object>
public class StandardMessageCodec extends Object implements MessageCodec<Object>
MessageCodec using the Flutter standard binary encoding.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
Fields Modifier and Type Field Description static StandardMessageCodec
INSTANCE
-
Constructor Summary
Constructors Constructor Description StandardMessageCodec()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Object
decodeMessage(ByteBuffer message)
Decodes the specified message from binary.ByteBuffer
encodeMessage(Object message)
Encodes the specified message into binary.protected static void
readAlignment(ByteBuffer buffer, int alignment)
Reads alignment padding bytes as written by writeAlignment.protected static byte[]
readBytes(ByteBuffer buffer)
Reads a byte array as written by writeBytes.protected static int
readSize(ByteBuffer buffer)
Reads an int representing a size as written by writeSize.protected 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 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 void
writeBytes(ByteArrayOutputStream stream, byte[] bytes)
Writes the length and then the actual bytes of the specified array to the specified stream.protected static void
writeChar(ByteArrayOutputStream stream, int value)
Writes the least significant two bytes of the specified int to the specified stream.protected static void
writeDouble(ByteArrayOutputStream stream, double value)
Writes the specified double as 8 bytes to the specified stream.protected static void
writeFloat(ByteArrayOutputStream stream, float value)
Writes the specified double as 4 bytes to the specified streamprotected static void
writeInt(ByteArrayOutputStream stream, int value)
Writes the specified int as 4 bytes to the specified stream.protected static void
writeLong(ByteArrayOutputStream stream, long value)
Writes the specified long as 8 bytes to the specified stream.protected static 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 Detail
-
INSTANCE
public static final StandardMessageCodec INSTANCE
-
-
Method Detail
-
encodeMessage
@Nullable public ByteBuffer encodeMessage(@Nullable Object message)
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
@Nullable public Object decodeMessage(@Nullable ByteBuffer message)
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
protected static final void writeSize(@NonNull ByteArrayOutputStream stream, int value)
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
protected static final void writeChar(@NonNull ByteArrayOutputStream stream, int value)
Writes the least significant two bytes of the specified int to the specified stream.
-
writeInt
protected static final void writeInt(@NonNull ByteArrayOutputStream stream, int value)
Writes the specified int as 4 bytes to the specified stream.
-
writeLong
protected static final void writeLong(@NonNull ByteArrayOutputStream stream, long value)
Writes the specified long as 8 bytes to the specified stream.
-
writeFloat
protected static final void writeFloat(@NonNull ByteArrayOutputStream stream, float value)
Writes the specified double as 4 bytes to the specified stream
-
writeDouble
protected static final void writeDouble(@NonNull ByteArrayOutputStream stream, double value)
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
protected static final void writeAlignment(@NonNull 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. An example usage with alignment = 8 is to ensure doubles are word-aligned in the stream.
-
writeValue
protected void writeValue(@NonNull ByteArrayOutputStream stream, @Nullable Object value)
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
protected static final int readSize(@NonNull ByteBuffer buffer)
Reads an int representing a size as written by writeSize.
-
readBytes
@NonNull protected static final byte[] readBytes(@NonNull ByteBuffer buffer)
Reads a byte array as written by writeBytes.
-
readAlignment
protected static final void readAlignment(@NonNull ByteBuffer buffer, int alignment)
Reads alignment padding bytes as written by writeAlignment.
-
readValue
@NonNull protected final Object readValue(@NonNull ByteBuffer buffer)
Reads a value as written by writeValue.
-
readValueOfType
@Nullable protected Object readValueOfType(byte type, @NonNull ByteBuffer buffer)
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.
-
-