diff --git a/shell/platform/android/io/flutter/plugin/common/FlutterMessageChannel.java b/shell/platform/android/io/flutter/plugin/common/FlutterMessageChannel.java index 001ab35cf61..b8d8f53fb3d 100644 --- a/shell/platform/android/io/flutter/plugin/common/FlutterMessageChannel.java +++ b/shell/platform/android/io/flutter/plugin/common/FlutterMessageChannel.java @@ -10,8 +10,6 @@ import io.flutter.view.FlutterView.BinaryMessageReplyCallback; import io.flutter.view.FlutterView.BinaryMessageResponse; import io.flutter.view.FlutterView.OnBinaryMessageListenerAsync; import java.nio.ByteBuffer; -import java.util.Objects; -import java.util.concurrent.atomic.AtomicBoolean; /** * A named channel for communicating with the Flutter application using semi-structured messages. @@ -43,9 +41,12 @@ public final class FlutterMessageChannel { * @param codec a {@link MessageCodec}. */ public FlutterMessageChannel(FlutterView view, String name, MessageCodec codec) { - this.view = Objects.requireNonNull(view); - this.name = Objects.requireNonNull(name); - this.codec = Objects.requireNonNull(codec); + assert view != null; + assert name != null; + assert codec != null; + this.view = view; + this.name = name; + this.codec = codec; } /** diff --git a/shell/platform/android/io/flutter/plugin/common/FlutterMethodChannel.java b/shell/platform/android/io/flutter/plugin/common/FlutterMethodChannel.java index 1a980233c44..5cadc0f2ef6 100644 --- a/shell/platform/android/io/flutter/plugin/common/FlutterMethodChannel.java +++ b/shell/platform/android/io/flutter/plugin/common/FlutterMethodChannel.java @@ -9,7 +9,6 @@ import io.flutter.view.FlutterView; import io.flutter.view.FlutterView.BinaryMessageResponse; import io.flutter.view.FlutterView.OnBinaryMessageListenerAsync; import java.nio.ByteBuffer; -import java.util.Objects; import java.util.concurrent.atomic.AtomicBoolean; /** @@ -53,9 +52,12 @@ public final class FlutterMethodChannel { * @param codec a {@link MessageCodec}. */ public FlutterMethodChannel(FlutterView view, String name, MethodCodec codec) { - this.view = Objects.requireNonNull(view); - this.name = Objects.requireNonNull(name); - this.codec = Objects.requireNonNull(codec); + assert view != null; + assert name != null; + assert codec != null; + this.view = view; + this.name = name; + this.codec = codec; } /** diff --git a/shell/platform/android/io/flutter/plugin/common/MethodCall.java b/shell/platform/android/io/flutter/plugin/common/MethodCall.java index 81843832988..3643bb5e455 100644 --- a/shell/platform/android/io/flutter/plugin/common/MethodCall.java +++ b/shell/platform/android/io/flutter/plugin/common/MethodCall.java @@ -27,7 +27,8 @@ public final class MethodCall { * @param arguments the arguments, a value supported by the channel's message codec. */ public MethodCall(String method, Object arguments) { - this.method = Objects.requireNonNull(method); + assert method != null; + this.method = method; this.arguments = arguments; } } diff --git a/shell/platform/android/io/flutter/plugin/common/StandardMessageCodec.java b/shell/platform/android/io/flutter/plugin/common/StandardMessageCodec.java index 99079ba84da..038dd90b4df 100644 --- a/shell/platform/android/io/flutter/plugin/common/StandardMessageCodec.java +++ b/shell/platform/android/io/flutter/plugin/common/StandardMessageCodec.java @@ -7,7 +7,7 @@ package io.flutter.plugin.common; import java.io.ByteArrayOutputStream; import java.math.BigInteger; import java.nio.ByteBuffer; -import java.nio.charset.StandardCharsets; +import java.nio.charset.Charset; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -65,6 +65,7 @@ public final class StandardMessageCodec implements MessageCodec { return value; } + private static final Charset UTF8 = Charset.forName("UTF8"); private static final byte NULL = 0; private static final byte TRUE = 1; private static final byte FALSE = 2; @@ -84,7 +85,7 @@ public final class StandardMessageCodec implements MessageCodec { assert 0 <= value; if (value < 254) { stream.write(value); - } else if (value < 0xffff) { + } else if (value <= 0xffff) { stream.write(254); stream.write(value >>> 8); stream.write(value); @@ -150,13 +151,13 @@ public final class StandardMessageCodec implements MessageCodec { } else if (value instanceof BigInteger) { stream.write(BIGINT); writeBytes(stream, - ((BigInteger) value).toString(16).getBytes(StandardCharsets.UTF_8)); + ((BigInteger) value).toString(16).getBytes(UTF8)); } else { throw new IllegalArgumentException("Unsupported Number type: " + value.getClass()); } } else if (value instanceof String) { stream.write(STRING); - writeBytes(stream, ((String) value).getBytes(StandardCharsets.UTF_8)); + writeBytes(stream, ((String) value).getBytes(UTF8)); } else if (value instanceof byte[]) { stream.write(BYTE_ARRAY); writeBytes(stream, (byte[]) value); @@ -259,7 +260,7 @@ public final class StandardMessageCodec implements MessageCodec { break; case BIGINT: { final byte[] hex = readBytes(buffer); - result = new BigInteger(new String(hex, StandardCharsets.UTF_8), 16); + result = new BigInteger(new String(hex, UTF8), 16); break; } case DOUBLE: @@ -267,7 +268,7 @@ public final class StandardMessageCodec implements MessageCodec { break; case STRING: { final byte[] bytes = readBytes(buffer); - result = new String(bytes, StandardCharsets.UTF_8); + result = new String(bytes, UTF8); break; } case BYTE_ARRAY: { diff --git a/shell/platform/android/io/flutter/plugin/common/StringMessageCodec.java b/shell/platform/android/io/flutter/plugin/common/StringMessageCodec.java index f47e39c2a22..0ae415c8a3b 100644 --- a/shell/platform/android/io/flutter/plugin/common/StringMessageCodec.java +++ b/shell/platform/android/io/flutter/plugin/common/StringMessageCodec.java @@ -5,13 +5,14 @@ package io.flutter.plugin.common; import java.nio.ByteBuffer; -import java.nio.charset.StandardCharsets; +import java.nio.charset.Charset; /** * A {@link MessageCodec} using UTF-8 encoded String messages. */ public final class StringMessageCodec implements MessageCodec { // This codec must match the Dart codec of the same name in package flutter/services. + private static final Charset UTF8 = Charset.forName("UTF8"); public static final StringMessageCodec INSTANCE = new StringMessageCodec(); private StringMessageCodec() { @@ -23,7 +24,7 @@ public final class StringMessageCodec implements MessageCodec { return null; } // TODO(mravn): Avoid the extra copy below. - final byte[] bytes = message.getBytes(StandardCharsets.UTF_8); + final byte[] bytes = message.getBytes(UTF8); final ByteBuffer buffer = ByteBuffer.allocateDirect(bytes.length); buffer.put(bytes); return buffer; @@ -46,6 +47,6 @@ public final class StringMessageCodec implements MessageCodec { message.get(bytes); offset = 0; } - return new String(bytes, offset, length, StandardCharsets.UTF_8); + return new String(bytes, offset, length, UTF8); } }