Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Classes | Public Member Functions | Static Public Attributes | Protected Member Functions | Static Protected Member Functions | List of all members
io.flutter.plugin.common.StandardMessageCodec Class Reference
Inheritance diagram for io.flutter.plugin.common.StandardMessageCodec:
io.flutter.plugin.common.MessageCodec< Object >

Classes

class  ExposedByteArrayOutputStream
 

Public Member Functions

ByteBuffer encodeMessage (@Nullable Object message)
 
Object decodeMessage (@Nullable ByteBuffer message)
 
- Public Member Functions inherited from io.flutter.plugin.common.MessageCodec< Object >
ByteBuffer encodeMessage ( @Nullable T message)
 
T decodeMessage ( @Nullable ByteBuffer message)
 

Static Public Attributes

static final StandardMessageCodec INSTANCE = new StandardMessageCodec()
 

Protected Member Functions

void writeValue (@NonNull ByteArrayOutputStream stream, @Nullable Object value)
 
final Object readValue (@NonNull ByteBuffer buffer)
 
Object readValueOfType (byte type, @NonNull ByteBuffer buffer)
 

Static Protected Member Functions

static final void writeSize (@NonNull ByteArrayOutputStream stream, int value)
 
static final void writeChar (@NonNull ByteArrayOutputStream stream, int value)
 
static final void writeInt (@NonNull ByteArrayOutputStream stream, int value)
 
static final void writeLong (@NonNull ByteArrayOutputStream stream, long value)
 
static final void writeFloat (@NonNull ByteArrayOutputStream stream, float value)
 
static final void writeDouble (@NonNull ByteArrayOutputStream stream, double value)
 
static final void writeBytes ( @NonNull ByteArrayOutputStream stream, @NonNull byte[] bytes)
 
static final void writeAlignment (@NonNull ByteArrayOutputStream stream, int alignment)
 
static final int readSize (@NonNull ByteBuffer buffer)
 
static final byte[] readBytes (@NonNull ByteBuffer buffer)
 
static final void readAlignment (@NonNull ByteBuffer buffer, int alignment)
 

Detailed Description

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:

On the Dart side, these values are represented as follows:

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.

Definition at line 65 of file StandardMessageCodec.java.

Member Function Documentation

◆ decodeMessage()

Object io.flutter.plugin.common.StandardMessageCodec.decodeMessage ( @Nullable ByteBuffer  message)
inline

Definition at line 84 of file StandardMessageCodec.java.

84 {
85 if (message == null) {
86 return null;
87 }
88 message.order(ByteOrder.nativeOrder());
89 final Object value = readValue(message);
90 if (message.hasRemaining()) {
91 throw new IllegalArgumentException("Message corrupted");
92 }
93 return value;
94 }
final Object readValue(@NonNull ByteBuffer buffer)
uint8_t value
Win32Message message

◆ encodeMessage()

ByteBuffer io.flutter.plugin.common.StandardMessageCodec.encodeMessage ( @Nullable Object  message)
inline

Definition at line 71 of file StandardMessageCodec.java.

71 {
72 if (message == null) {
73 return null;
74 }
75 final ExposedByteArrayOutputStream stream = new ExposedByteArrayOutputStream();
76 writeValue(stream, message);
77 final ByteBuffer buffer = ByteBuffer.allocateDirect(stream.size());
78 buffer.put(stream.buffer(), 0, stream.size());
79 return buffer;
80 }
void writeValue(@NonNull ByteArrayOutputStream stream, @Nullable Object value)
static const uint8_t buffer[]

◆ readAlignment()

static final void io.flutter.plugin.common.StandardMessageCodec.readAlignment ( @NonNull ByteBuffer  buffer,
int  alignment 
)
inlinestaticprotected

Reads alignment padding bytes as written by writeAlignment.

Definition at line 326 of file StandardMessageCodec.java.

326 {
327 final int mod = buffer.position() % alignment;
328 if (mod != 0) {
329 buffer.position(buffer.position() + alignment - mod);
330 }
331 }

◆ readBytes()

static final byte[] io.flutter.plugin.common.StandardMessageCodec.readBytes ( @NonNull ByteBuffer  buffer)
inlinestaticprotected

Reads a byte array as written by writeBytes.

Definition at line 318 of file StandardMessageCodec.java.

318 {
319 final int length = readSize(buffer);
320 final byte[] bytes = new byte[length];
321 buffer.get(bytes);
322 return bytes;
323 }
static final int readSize(@NonNull ByteBuffer buffer)
size_t length

◆ readSize()

static final int io.flutter.plugin.common.StandardMessageCodec.readSize ( @NonNull ByteBuffer  buffer)
inlinestaticprotected

Reads an int representing a size as written by writeSize.

Definition at line 302 of file StandardMessageCodec.java.

302 {
303 if (!buffer.hasRemaining()) {
304 throw new IllegalArgumentException("Message corrupted");
305 }
306 final int value = buffer.get() & 0xff;
307 if (value < 254) {
308 return value;
309 } else if (value == 254) {
310 return buffer.getChar();
311 } else {
312 return buffer.getInt();
313 }
314 }

◆ readValue()

final Object io.flutter.plugin.common.StandardMessageCodec.readValue ( @NonNull ByteBuffer  buffer)
inlineprotected

Reads a value as written by writeValue.

Definition at line 335 of file StandardMessageCodec.java.

335 {
336 if (!buffer.hasRemaining()) {
337 throw new IllegalArgumentException("Message corrupted");
338 }
339 final byte type = buffer.get();
340 return readValueOfType(type, buffer);
341 }
Object readValueOfType(byte type, @NonNull ByteBuffer buffer)

◆ readValueOfType()

Object io.flutter.plugin.common.StandardMessageCodec.readValueOfType ( byte  type,
@NonNull ByteBuffer  buffer 
)
inlineprotected

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.

Definition at line 350 of file StandardMessageCodec.java.

350 {
351 final Object result;
352 switch (type) {
353 case NULL:
354 result = null;
355 break;
356 case TRUE:
357 result = true;
358 break;
359 case FALSE:
360 result = false;
361 break;
362 case INT:
363 result = buffer.getInt();
364 break;
365 case LONG:
366 result = buffer.getLong();
367 break;
368 case BIGINT:
369 {
370 final byte[] hex = readBytes(buffer);
371 result = new BigInteger(new String(hex, UTF8), 16);
372 break;
373 }
374 case DOUBLE:
376 result = buffer.getDouble();
377 break;
378 case STRING:
379 {
380 final byte[] bytes = readBytes(buffer);
381 result = new String(bytes, UTF8);
382 break;
383 }
384 case BYTE_ARRAY:
385 {
387 break;
388 }
389 case INT_ARRAY:
390 {
391 final int length = readSize(buffer);
392 final int[] array = new int[length];
394 buffer.asIntBuffer().get(array);
395 result = array;
396 buffer.position(buffer.position() + 4 * length);
397 break;
398 }
399 case LONG_ARRAY:
400 {
401 final int length = readSize(buffer);
402 final long[] array = new long[length];
404 buffer.asLongBuffer().get(array);
405 result = array;
406 buffer.position(buffer.position() + 8 * length);
407 break;
408 }
409 case DOUBLE_ARRAY:
410 {
411 final int length = readSize(buffer);
412 final double[] array = new double[length];
414 buffer.asDoubleBuffer().get(array);
415 result = array;
416 buffer.position(buffer.position() + 8 * length);
417 break;
418 }
419 case LIST:
420 {
421 final int size = readSize(buffer);
422 final List<Object> list = new ArrayList<>(size);
423 for (int i = 0; i < size; i++) {
424 list.add(readValue(buffer));
425 }
426 result = list;
427 break;
428 }
429 case MAP:
430 {
431 final int size = readSize(buffer);
432 final Map<Object, Object> map = new HashMap<>();
433 for (int i = 0; i < size; i++) {
435 }
436 result = map;
437 break;
438 }
439 case FLOAT_ARRAY:
440 {
441 final int length = readSize(buffer);
442 final float[] array = new float[length];
444 buffer.asFloatBuffer().get(array);
445 result = array;
446 buffer.position(buffer.position() + 4 * length);
447 break;
448 }
449 default:
450 throw new IllegalArgumentException("Message corrupted");
451 }
452 return result;
453 }
void add(sk_sp< SkIDChangeListener > listener) SK_EXCLUDES(fMutex)
static final void readAlignment(@NonNull ByteBuffer buffer, int alignment)
static final byte[] readBytes(@NonNull ByteBuffer buffer)
GAsyncResult * result
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition switches.h:259
SI auto map(std::index_sequence< I... >, Fn &&fn, const Args &... args) -> skvx::Vec< sizeof...(I), decltype(fn(args[0]...))>
Definition SkVx.h:680
long LONG
int INT

◆ writeAlignment()

static final void io.flutter.plugin.common.StandardMessageCodec.writeAlignment ( @NonNull ByteArrayOutputStream  stream,
int  alignment 
)
inlinestaticprotected

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.

Definition at line 204 of file StandardMessageCodec.java.

204 {
205 final int mod = stream.size() % alignment;
206 if (mod != 0) {
207 for (int i = 0; i < alignment - mod; i++) {
208 stream.write(0);
209 }
210 }
211 }

◆ writeBytes()

static final void io.flutter.plugin.common.StandardMessageCodec.writeBytes ( @NonNull ByteArrayOutputStream  stream,
@NonNull byte[]  bytes 
)
inlinestaticprotected

Writes the length and then the actual bytes of the specified array to the specified stream.

Definition at line 193 of file StandardMessageCodec.java.

194 {
195 writeSize(stream, bytes.length);
196 stream.write(bytes, 0, bytes.length);
197 }
static final void writeSize(@NonNull ByteArrayOutputStream stream, int value)

◆ writeChar()

static final void io.flutter.plugin.common.StandardMessageCodec.writeChar ( @NonNull ByteArrayOutputStream  stream,
int  value 
)
inlinestaticprotected

Writes the least significant two bytes of the specified int to the specified stream.

Definition at line 134 of file StandardMessageCodec.java.

134 {
135 if (LITTLE_ENDIAN) {
136 stream.write(value);
137 stream.write(value >>> 8);
138 } else {
139 stream.write(value >>> 8);
140 stream.write(value);
141 }
142 }

◆ writeDouble()

static final void io.flutter.plugin.common.StandardMessageCodec.writeDouble ( @NonNull ByteArrayOutputStream  stream,
double  value 
)
inlinestaticprotected

Writes the specified double as 8 bytes to the specified stream.

Definition at line 188 of file StandardMessageCodec.java.

188 {
189 writeLong(stream, Double.doubleToLongBits(value));
190 }
static final void writeLong(@NonNull ByteArrayOutputStream stream, long value)

◆ writeFloat()

static final void io.flutter.plugin.common.StandardMessageCodec.writeFloat ( @NonNull ByteArrayOutputStream  stream,
float  value 
)
inlinestaticprotected

Writes the specified double as 4 bytes to the specified stream

Definition at line 183 of file StandardMessageCodec.java.

183 {
184 writeInt(stream, Float.floatToIntBits(value));
185 }
static final void writeInt(@NonNull ByteArrayOutputStream stream, int value)

◆ writeInt()

static final void io.flutter.plugin.common.StandardMessageCodec.writeInt ( @NonNull ByteArrayOutputStream  stream,
int  value 
)
inlinestaticprotected

Writes the specified int as 4 bytes to the specified stream.

Definition at line 145 of file StandardMessageCodec.java.

145 {
146 if (LITTLE_ENDIAN) {
147 stream.write(value);
148 stream.write(value >>> 8);
149 stream.write(value >>> 16);
150 stream.write(value >>> 24);
151 } else {
152 stream.write(value >>> 24);
153 stream.write(value >>> 16);
154 stream.write(value >>> 8);
155 stream.write(value);
156 }
157 }

◆ writeLong()

static final void io.flutter.plugin.common.StandardMessageCodec.writeLong ( @NonNull ByteArrayOutputStream  stream,
long  value 
)
inlinestaticprotected

Writes the specified long as 8 bytes to the specified stream.

Definition at line 160 of file StandardMessageCodec.java.

160 {
161 if (LITTLE_ENDIAN) {
162 stream.write((byte) value);
163 stream.write((byte) (value >>> 8));
164 stream.write((byte) (value >>> 16));
165 stream.write((byte) (value >>> 24));
166 stream.write((byte) (value >>> 32));
167 stream.write((byte) (value >>> 40));
168 stream.write((byte) (value >>> 48));
169 stream.write((byte) (value >>> 56));
170 } else {
171 stream.write((byte) (value >>> 56));
172 stream.write((byte) (value >>> 48));
173 stream.write((byte) (value >>> 40));
174 stream.write((byte) (value >>> 32));
175 stream.write((byte) (value >>> 24));
176 stream.write((byte) (value >>> 16));
177 stream.write((byte) (value >>> 8));
178 stream.write((byte) value);
179 }
180 }

◆ writeSize()

static final void io.flutter.plugin.common.StandardMessageCodec.writeSize ( @NonNull ByteArrayOutputStream  stream,
int  value 
)
inlinestaticprotected

Writes an int representing a size to the specified stream. Uses an expanding code of 1 to 5 bytes to optimize for small values.

Definition at line 118 of file StandardMessageCodec.java.

118 {
119 if (BuildConfig.DEBUG && 0 > value) {
120 Log.e(TAG, "Attempted to write a negative size.");
121 }
122 if (value < 254) {
123 stream.write(value);
124 } else if (value <= 0xffff) {
125 stream.write(254);
126 writeChar(stream, value);
127 } else {
128 stream.write(255);
129 writeInt(stream, value);
130 }
131 }
static final void writeChar(@NonNull ByteArrayOutputStream stream, int value)
void Log(const char *format,...) SK_PRINTF_LIKE(1

◆ writeValue()

void io.flutter.plugin.common.StandardMessageCodec.writeValue ( @NonNull ByteArrayOutputStream  stream,
@Nullable Object  value 
)
inlineprotected

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.

Definition at line 220 of file StandardMessageCodec.java.

220 {
221 if (value == null || value.equals(null)) {
222 stream.write(NULL);
223 } else if (value instanceof Boolean) {
224 stream.write(((Boolean) value).booleanValue() ? TRUE : FALSE);
225 } else if (value instanceof Number) {
226 if (value instanceof Integer || value instanceof Short || value instanceof Byte) {
227 stream.write(INT);
228 writeInt(stream, ((Number) value).intValue());
229 } else if (value instanceof Long) {
230 stream.write(LONG);
231 writeLong(stream, (long) value);
232 } else if (value instanceof Float || value instanceof Double) {
233 stream.write(DOUBLE);
234 writeAlignment(stream, 8);
235 writeDouble(stream, ((Number) value).doubleValue());
236 } else if (value instanceof BigInteger) {
237 stream.write(BIGINT);
238 writeBytes(stream, ((BigInteger) value).toString(16).getBytes(UTF8));
239 } else {
240 throw new IllegalArgumentException("Unsupported Number type: " + value.getClass());
241 }
242 } else if (value instanceof CharSequence) {
243 stream.write(STRING);
244 writeBytes(stream, value.toString().getBytes(UTF8));
245 } else if (value instanceof byte[]) {
246 stream.write(BYTE_ARRAY);
247 writeBytes(stream, (byte[]) value);
248 } else if (value instanceof int[]) {
249 stream.write(INT_ARRAY);
250 final int[] array = (int[]) value;
251 writeSize(stream, array.length);
252 writeAlignment(stream, 4);
253 for (final int n : array) {
254 writeInt(stream, n);
255 }
256 } else if (value instanceof long[]) {
257 stream.write(LONG_ARRAY);
258 final long[] array = (long[]) value;
259 writeSize(stream, array.length);
260 writeAlignment(stream, 8);
261 for (final long n : array) {
262 writeLong(stream, n);
263 }
264 } else if (value instanceof double[]) {
265 stream.write(DOUBLE_ARRAY);
266 final double[] array = (double[]) value;
267 writeSize(stream, array.length);
268 writeAlignment(stream, 8);
269 for (final double d : array) {
270 writeDouble(stream, d);
271 }
272 } else if (value instanceof List) {
273 stream.write(LIST);
274 final List<?> list = (List) value;
275 writeSize(stream, list.size());
276 for (final Object o : list) {
277 writeValue(stream, o);
278 }
279 } else if (value instanceof Map) {
280 stream.write(MAP);
281 final Map<?, ?> map = (Map) value;
282 writeSize(stream, map.size());
283 for (final Entry<?, ?> entry : map.entrySet()) {
284 writeValue(stream, entry.getKey());
285 writeValue(stream, entry.getValue());
286 }
287 } else if (value instanceof float[]) {
288 stream.write(FLOAT_ARRAY);
289 final float[] array = (float[]) value;
290 writeSize(stream, array.length);
291 writeAlignment(stream, 4);
292 for (final float f : array) {
293 writeFloat(stream, f);
294 }
295 } else {
296 throw new IllegalArgumentException(
297 "Unsupported value: '" + value + "' of type '" + value.getClass() + "'");
298 }
299 }
static final void writeFloat(@NonNull ByteArrayOutputStream stream, float value)
static final void writeAlignment(@NonNull ByteArrayOutputStream stream, int alignment)
static final void writeBytes( @NonNull ByteArrayOutputStream stream, @NonNull byte[] bytes)
static final void writeDouble(@NonNull ByteArrayOutputStream stream, double value)
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE auto & d
Definition main.cc:19

Member Data Documentation

◆ INSTANCE

final StandardMessageCodec io.flutter.plugin.common.StandardMessageCodec.INSTANCE = new StandardMessageCodec()
static

Definition at line 67 of file StandardMessageCodec.java.


The documentation for this class was generated from the following file: