Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 9cb34ba

Browse files
committed
moved to direct buffers
1 parent cb3ee34 commit 9cb34ba

File tree

5 files changed

+19
-21
lines changed

5 files changed

+19
-21
lines changed

shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ public void setPlatformMessageHandler(@Nullable PlatformMessageHandler platformM
814814
@SuppressWarnings("unused")
815815
@VisibleForTesting
816816
public void handlePlatformMessage(
817-
@NonNull final String channel, byte[] message, final int replyId) {
817+
@NonNull final String channel, ByteBuffer message, final int replyId) {
818818
if (platformMessageHandler != null) {
819819
platformMessageHandler.handleMessageFromDart(channel, message, replyId);
820820
}
@@ -825,7 +825,7 @@ public void handlePlatformMessage(
825825
// Called by native to respond to a platform message that we sent.
826826
// TODO(mattcarroll): determine if reply is nonull or nullable
827827
@SuppressWarnings("unused")
828-
private void handlePlatformMessageResponse(int replyId, byte[] reply) {
828+
private void handlePlatformMessageResponse(int replyId, ByteBuffer reply) {
829829
if (platformMessageHandler != null) {
830830
platformMessageHandler.handlePlatformMessageResponse(replyId, reply);
831831
}

shell/platform/android/io/flutter/embedding/engine/dart/DartMessenger.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,13 @@ public void send(
7575

7676
@Override
7777
public void handleMessageFromDart(
78-
@NonNull final String channel, @Nullable byte[] message, final int replyId) {
78+
@NonNull final String channel, @Nullable ByteBuffer message, final int replyId) {
7979
Log.v(TAG, "Received message from Dart over channel '" + channel + "'");
8080
BinaryMessenger.BinaryMessageHandler handler = messageHandlers.get(channel);
8181
if (handler != null) {
8282
try {
8383
Log.v(TAG, "Deferring to registered handler to process message.");
84-
final ByteBuffer buffer = (message == null ? null : ByteBuffer.wrap(message));
85-
handler.onMessage(buffer, new Reply(flutterJNI, replyId));
84+
handler.onMessage(message, new Reply(flutterJNI, replyId));
8685
} catch (Exception ex) {
8786
Log.e(TAG, "Uncaught exception in binary message listener", ex);
8887
flutterJNI.invokePlatformMessageEmptyResponseCallback(replyId);
@@ -96,13 +95,13 @@ public void handleMessageFromDart(
9695
}
9796

9897
@Override
99-
public void handlePlatformMessageResponse(int replyId, @Nullable byte[] reply) {
98+
public void handlePlatformMessageResponse(int replyId, @Nullable ByteBuffer reply) {
10099
Log.v(TAG, "Received message reply from Dart.");
101100
BinaryMessenger.BinaryReply callback = pendingReplies.remove(replyId);
102101
if (callback != null) {
103102
try {
104103
Log.v(TAG, "Invoking registered callback for reply from Dart.");
105-
callback.reply(reply == null ? null : ByteBuffer.wrap(reply));
104+
callback.reply(reply);
106105
} catch (Exception ex) {
107106
Log.e(TAG, "Uncaught exception in binary message reply handler", ex);
108107
} catch (Error err) {

shell/platform/android/io/flutter/embedding/engine/dart/PlatformMessageHandler.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66

77
import androidx.annotation.NonNull;
88
import androidx.annotation.Nullable;
9+
import java.nio.ByteBuffer;
910

1011
/** Handler that receives messages from Dart code. */
1112
public interface PlatformMessageHandler {
1213
void handleMessageFromDart(
13-
@NonNull final String channel, @Nullable byte[] message, final int replyId);
14+
@NonNull final String channel, @Nullable ByteBuffer message, final int replyId);
1415

15-
void handlePlatformMessageResponse(int replyId, @Nullable byte[] reply);
16+
void handlePlatformMessageResponse(int replyId, @Nullable ByteBuffer reply);
1617
}

shell/platform/android/platform_view_android_jni_impl.cc

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -828,15 +828,15 @@ bool RegisterApi(JNIEnv* env) {
828828

829829
g_handle_platform_message_method =
830830
env->GetMethodID(g_flutter_jni_class->obj(), "handlePlatformMessage",
831-
"(Ljava/lang/String;[BI)V");
831+
"(Ljava/lang/String;Ljava/nio/ByteBuffer;I)V");
832832

833833
if (g_handle_platform_message_method == nullptr) {
834834
FML_LOG(ERROR) << "Could not locate handlePlatformMessage method";
835835
return false;
836836
}
837837

838838
g_handle_platform_message_response_method = env->GetMethodID(
839-
g_flutter_jni_class->obj(), "handlePlatformMessageResponse", "(I[B)V");
839+
g_flutter_jni_class->obj(), "handlePlatformMessageResponse", "(ILjava/nio/ByteBuffer;)V");
840840

841841
if (g_handle_platform_message_response_method == nullptr) {
842842
FML_LOG(ERROR) << "Could not locate handlePlatformMessageResponse method";
@@ -1107,11 +1107,10 @@ void PlatformViewAndroidJNIImpl::FlutterViewHandlePlatformMessage(
11071107
fml::jni::StringToJavaString(env, message->channel());
11081108

11091109
if (message->hasData()) {
1110-
fml::jni::ScopedJavaLocalRef<jbyteArray> message_array(
1111-
env, env->NewByteArray(message->data().GetSize()));
1112-
env->SetByteArrayRegion(
1113-
message_array.obj(), 0, message->data().GetSize(),
1114-
reinterpret_cast<const jbyte*>(message->data().GetMapping()));
1110+
fml::jni::ScopedJavaLocalRef<jobject> message_array(
1111+
env, env->NewDirectByteBuffer(
1112+
const_cast<uint8_t*>(message->data().GetMapping()),
1113+
message->data().GetSize()));
11151114
env->CallVoidMethod(java_object.obj(), g_handle_platform_message_method,
11161115
java_channel.obj(), message_array.obj(), responseId);
11171116
} else {
@@ -1141,10 +1140,9 @@ void PlatformViewAndroidJNIImpl::FlutterViewHandlePlatformMessageResponse(
11411140
nullptr);
11421141
} else {
11431142
// Convert the vector to a Java byte array.
1144-
fml::jni::ScopedJavaLocalRef<jbyteArray> data_array(
1145-
env, env->NewByteArray(data->GetSize()));
1146-
env->SetByteArrayRegion(data_array.obj(), 0, data->GetSize(),
1147-
reinterpret_cast<const jbyte*>(data->GetMapping()));
1143+
fml::jni::ScopedJavaLocalRef<jobject> data_array(
1144+
env, env->NewDirectByteBuffer(const_cast<uint8_t*>(data->GetMapping()),
1145+
data->GetSize()));
11481146

11491147
env->CallVoidMethod(java_object.obj(),
11501148
g_handle_platform_message_response_method, responseId,

shell/platform/android/test/io/flutter/embedding/engine/dart/DartMessengerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void itHandlesErrors() {
4646
.onMessage(any(ByteBuffer.class), any(DartMessenger.Reply.class));
4747

4848
messenger.setMessageHandler("test", throwingHandler);
49-
messenger.handleMessageFromDart("test", new byte[] {}, 0);
49+
messenger.handleMessageFromDart("test", ByteBuffer.allocate(0), 0);
5050
assertNotNull(reportingHandler.latestException);
5151
assertTrue(reportingHandler.latestException instanceof AssertionError);
5252
currentThread.setUncaughtExceptionHandler(savedHandler);

0 commit comments

Comments
 (0)