Add an encoder for CharSequence in StandardMessageCodec (flutter/engine#26526)

https://github.com/flutter/engine/pull/25373 introdued APIs that
return SpannableString, which is a CharSequence subclass that was
not previously supported by StandardMessageCodec

See https://github.com/flutter/flutter/issues/83751
This commit is contained in:
Jason Simmons
2021-06-01 18:42:57 -07:00
committed by GitHub
parent 187c6a3ec2
commit 189fc5d4d4
2 changed files with 16 additions and 2 deletions

View File

@@ -234,9 +234,9 @@ public class StandardMessageCodec implements MessageCodec<Object> {
} else {
throw new IllegalArgumentException("Unsupported Number type: " + value.getClass());
}
} else if (value instanceof String) {
} else if (value instanceof CharSequence) {
stream.write(STRING);
writeBytes(stream, ((String) value).getBytes(UTF8));
writeBytes(stream, value.toString().getBytes(UTF8));
} else if (value instanceof byte[]) {
stream.write(BYTE_ARRAY);
writeBytes(stream, (byte[]) value);

View File

@@ -3,6 +3,7 @@ package io.flutter.plugin.common;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import android.text.SpannableString;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import org.junit.Test;
@@ -108,4 +109,17 @@ public class StandardMessageCodecTest {
float[] values = (float[]) codec.decodeMessage(message);
assertArrayEquals(expectedValues, values, 0.01f);
}
@Test
public void itEncodesCharSequences() {
StandardMessageCodec codec = new StandardMessageCodec();
CharSequence cs = new SpannableString("hello world");
ByteBuffer message = codec.encodeMessage(cs);
message.flip();
String value = (String) codec.decodeMessage(message);
assertEquals(value, "hello world");
}
}