5package io.flutter.plugin.common;
7import androidx.annotation.NonNull;
8import androidx.annotation.Nullable;
9import io.flutter.BuildConfig;
11import java.io.ByteArrayOutputStream;
12import java.math.BigInteger;
13import java.nio.ByteBuffer;
14import java.nio.ByteOrder;
15import java.nio.charset.Charset;
16import java.util.ArrayList;
17import java.util.HashMap;
20import java.util.Map.Entry;
66 private static final String TAG =
"StandardMessageCodec#";
77 final ByteBuffer
buffer = ByteBuffer.allocateDirect(stream.size());
78 buffer.put(stream.buffer(), 0, stream.size());
88 message.order(ByteOrder.nativeOrder());
91 throw new IllegalArgumentException(
"Message corrupted");
96 private static final boolean LITTLE_ENDIAN = ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN;
97 private static final Charset UTF8 = Charset.forName(
"UTF8");
98 private static final byte NULL = 0;
99 private static final byte TRUE = 1;
100 private static final byte FALSE = 2;
101 private static final byte INT = 3;
102 private static final byte LONG = 4;
103 private static final byte BIGINT = 5;
104 private static final byte DOUBLE = 6;
105 private static final byte STRING = 7;
106 private static final byte BYTE_ARRAY = 8;
107 private static final byte INT_ARRAY = 9;
108 private static final byte LONG_ARRAY = 10;
109 private static final byte DOUBLE_ARRAY = 11;
110 private static final byte LIST = 12;
111 private static final byte MAP = 13;
112 private static final byte FLOAT_ARRAY = 14;
118 protected static final void writeSize(@NonNull ByteArrayOutputStream stream,
int value) {
120 Log.
e(
TAG,
"Attempted to write a negative size.");
124 }
else if (
value <= 0xffff) {
134 protected static final void writeChar(@NonNull ByteArrayOutputStream stream,
int value) {
137 stream.write(
value >>> 8);
139 stream.write(
value >>> 8);
145 protected static final void writeInt(@NonNull ByteArrayOutputStream stream,
int value) {
148 stream.write(
value >>> 8);
149 stream.write(
value >>> 16);
150 stream.write(
value >>> 24);
152 stream.write(
value >>> 24);
153 stream.write(
value >>> 16);
154 stream.write(
value >>> 8);
160 protected static final void writeLong(@NonNull ByteArrayOutputStream stream,
long value) {
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));
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);
183 protected static final void writeFloat(@NonNull ByteArrayOutputStream stream,
float value) {
188 protected static final void writeDouble(@NonNull ByteArrayOutputStream stream,
double value) {
194 @NonNull ByteArrayOutputStream stream, @NonNull
byte[] bytes) {
196 stream.write(bytes, 0, bytes.length);
204 protected static final void writeAlignment(@NonNull ByteArrayOutputStream stream,
int alignment) {
205 final int mod = stream.size() % alignment;
207 for (
int i = 0; i < alignment - mod; i++) {
220 protected void writeValue(@NonNull ByteArrayOutputStream stream, @Nullable Object value) {
223 }
else if (
value instanceof Boolean) {
225 }
else if (
value instanceof Number) {
226 if (
value instanceof Integer ||
value instanceof Short ||
value instanceof Byte) {
229 }
else if (
value instanceof Long) {
232 }
else if (
value instanceof Float ||
value instanceof Double) {
233 stream.write(DOUBLE);
236 }
else if (
value instanceof BigInteger) {
237 stream.write(BIGINT);
240 throw new IllegalArgumentException(
"Unsupported Number type: " +
value.getClass());
242 }
else if (
value instanceof CharSequence) {
243 stream.write(STRING);
245 }
else if (
value instanceof
byte[]) {
246 stream.write(BYTE_ARRAY);
248 }
else if (
value instanceof
int[]) {
249 stream.write(INT_ARRAY);
250 final int[] array = (
int[])
value;
253 for (
final int n : array) {
256 }
else if (
value instanceof
long[]) {
257 stream.write(LONG_ARRAY);
258 final long[] array = (
long[])
value;
261 for (
final long n : array) {
264 }
else if (
value instanceof
double[]) {
265 stream.write(DOUBLE_ARRAY);
266 final double[] array = (
double[])
value;
269 for (
final double d : array) {
276 for (
final Object o : list) {
279 }
else if (
value instanceof Map) {
281 final Map<?, ?> map = (Map)
value;
283 for (
final Entry<?, ?> entry : map.entrySet()) {
287 }
else if (
value instanceof
float[]) {
288 stream.write(FLOAT_ARRAY);
289 final float[] array = (
float[])
value;
292 for (
final float f : array) {
296 throw new IllegalArgumentException(
297 "Unsupported value: '" +
value +
"' of type '" +
value.getClass() +
"'");
303 if (!
buffer.hasRemaining()) {
304 throw new IllegalArgumentException(
"Message corrupted");
309 }
else if (
value == 254) {
320 final byte[] bytes =
new byte[
length];
327 final int mod =
buffer.position() % alignment;
336 if (!
buffer.hasRemaining()) {
337 throw new IllegalArgumentException(
"Message corrupted");
371 result =
new BigInteger(
new String(hex, UTF8), 16);
381 result =
new String(bytes, UTF8);
392 final int[] array =
new int[
length];
394 buffer.asIntBuffer().get(array);
402 final long[] array =
new long[
length];
404 buffer.asLongBuffer().get(array);
412 final double[] array =
new double[
length];
414 buffer.asDoubleBuffer().get(array);
423 for (
int i = 0; i < size; i++) {
432 final Map<Object, Object> map =
new HashMap<>();
433 for (
int i = 0; i < size; i++) {
442 final float[] array =
new float[
length];
444 buffer.asFloatBuffer().get(array);
450 throw new IllegalArgumentException(
"Message corrupted");
void add(sk_sp< SkIDChangeListener > listener) SK_EXCLUDES(fMutex)
static final boolean DEBUG
static void e(@NonNull String tag, @NonNull String message)
ByteBuffer encodeMessage(@Nullable Object message)
Object readValueOfType(byte type, @NonNull ByteBuffer buffer)
final Object readValue(@NonNull ByteBuffer buffer)
static final void writeInt(@NonNull ByteArrayOutputStream stream, int value)
static final void writeSize(@NonNull ByteArrayOutputStream stream, int value)
static final void writeChar(@NonNull ByteArrayOutputStream stream, int value)
static final void writeFloat(@NonNull ByteArrayOutputStream stream, float value)
static final void writeAlignment(@NonNull ByteArrayOutputStream stream, int alignment)
void writeValue(@NonNull ByteArrayOutputStream stream, @Nullable Object value)
static final void readAlignment(@NonNull ByteBuffer buffer, int alignment)
static final StandardMessageCodec INSTANCE
static final int readSize(@NonNull ByteBuffer buffer)
static final void writeLong(@NonNull ByteArrayOutputStream stream, long value)
static final void writeBytes( @NonNull ByteArrayOutputStream stream, @NonNull byte[] bytes)
Object decodeMessage(@Nullable ByteBuffer message)
static final void writeDouble(@NonNull ByteArrayOutputStream stream, double value)
static final byte[] readBytes(@NonNull ByteBuffer buffer)
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE auto & d
static const uint8_t buffer[]