Flutter Engine
The Flutter Engine
BinaryCodec.java
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5package io.flutter.plugin.common;
6
7import androidx.annotation.Nullable;
8import java.nio.ByteBuffer;
9
10/**
11 * A {@link MessageCodec} using unencoded binary messages, represented as {@link ByteBuffer}s.
12 *
13 * <p>This codec is guaranteed to be compatible with the corresponding <a
14 * href="https://api.flutter.dev/flutter/services/BinaryCodec-class.html">BinaryCodec</a> on the
15 * Dart side. These parts of the Flutter SDK are evolved synchronously.
16 *
17 * <p>On the Dart side, messages are represented using {@code ByteData}.
18 */
19public final class BinaryCodec implements MessageCodec<ByteBuffer> {
20 // This codec must match the Dart codec of the same name in package flutter/services.
21 public static final BinaryCodec INSTANCE = new BinaryCodec();
22 /**
23 * A BinaryCodec that returns direct ByteBuffers from `decodeMessage` for better performance.
24 *
25 * @see BinaryCodec#BinaryCodec(boolean)
26 */
27 public static final BinaryCodec INSTANCE_DIRECT = new BinaryCodec(true);
28
29 private final boolean returnsDirectByteBufferFromDecoding;
30
31 private BinaryCodec() {
32 this.returnsDirectByteBufferFromDecoding = false;
33 }
34
35 /**
36 * A constructor for BinaryCodec.
37 *
38 * @param returnsDirectByteBufferFromDecoding `true` means that the Codec will return direct
39 * ByteBuffers from `decodeMessage`. Direct ByteBuffers will have better performance but will
40 * be invalid beyond the scope of the `decodeMessage` call. `false` means Flutter will copy
41 * the encoded message to Java's memory, so the ByteBuffer will be valid beyond the
42 * decodeMessage call, at the cost of a copy.
43 */
44 private BinaryCodec(boolean returnsDirectByteBufferFromDecoding) {
45 this.returnsDirectByteBufferFromDecoding = returnsDirectByteBufferFromDecoding;
46 }
47
48 @Override
49 public ByteBuffer encodeMessage(@Nullable ByteBuffer message) {
50 return message;
51 }
52
53 @Override
54 public ByteBuffer decodeMessage(@Nullable ByteBuffer message) {
55 if (message == null) {
56 return message;
57 } else if (returnsDirectByteBufferFromDecoding) {
58 return message;
59 } else {
60 ByteBuffer result = ByteBuffer.allocate(message.capacity());
61 result.put(message);
62 result.rewind();
63 return result;
64 }
65 }
66}
ByteBuffer decodeMessage(@Nullable ByteBuffer message)
static final BinaryCodec INSTANCE
ByteBuffer encodeMessage(@Nullable ByteBuffer message)
static final BinaryCodec INSTANCE_DIRECT
GAsyncResult * result
Win32Message message