Class StandardMessageCodec

  • All Implemented Interfaces:

    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.

    • Constructor Detail

      • StandardMessageCodec

        public StandardMessageCodec()
    • Method Detail

      • encodeMessage

        public ByteBuffer encodeMessage​(@Nullable
                                        Object message)
        Description copied from interface: MessageCodec
        Encodes the specified message into binary.
        Specified by:
        encodeMessage in interface MessageCodec<Object>
        message - the T message, possibly null.
        a ByteBuffer containing the encoding between position 0 and the current position, or null, if message is null.
      • decodeMessage

        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 interface MessageCodec<Object>
        message - the ByteBuffer message, possibly null.
        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,
                                               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,
                                  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

        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

        protected final Object readValue​(@NonNull
                                         ByteBuffer buffer)
        Reads a value as written by writeValue.
      • readValueOfType

        protected Object readValueOfType​(byte type,
                                         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.