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 5 package io.flutter.plugin.common; 6 7 import java.nio.ByteBuffer; 8 9 /** 10 * A codec for method calls and enveloped results. 11 * 12 * Method calls are encoded as binary messages with enough structure that the codec can 13 * extract a method name String and an arguments Object. These data items are used to populate a 14 * {@link MethodCall}. 15 * 16 * All operations throw {@link IllegalArgumentException}, if conversion fails. 17 */ 18 public interface MethodCodec { 19 /** 20 * Encodes a message call into binary. 21 * 22 * @param methodCall a {@link MethodCall}. 23 * @return a {@link ByteBuffer} containing the encoding between position 0 and 24 * the current position. 25 */ encodeMethodCall(MethodCall methodCall)26 ByteBuffer encodeMethodCall(MethodCall methodCall); 27 28 /** 29 * Decodes a message call from binary. 30 * 31 * @param methodCall the binary encoding of the method call as a {@link ByteBuffer}. 32 * @return a {@link MethodCall} representation of the bytes between the given buffer's current 33 * position and its limit. 34 */ decodeMethodCall(ByteBuffer methodCall)35 MethodCall decodeMethodCall(ByteBuffer methodCall); 36 37 /** 38 * Encodes a successful result into a binary envelope message. 39 * 40 * @param result The result value, possibly null. 41 * @return a {@link ByteBuffer} containing the encoding between position 0 and 42 * the current position. 43 */ encodeSuccessEnvelope(Object result)44 ByteBuffer encodeSuccessEnvelope(Object result); 45 46 /** 47 * Encodes an error result into a binary envelope message. 48 * 49 * @param errorCode An error code String. 50 * @param errorMessage An error message String, possibly null. 51 * @param errorDetails Error details, possibly null. 52 * @return a {@link ByteBuffer} containing the encoding between position 0 and 53 * the current position. 54 */ encodeErrorEnvelope(String errorCode, String errorMessage, Object errorDetails)55 ByteBuffer encodeErrorEnvelope(String errorCode, String errorMessage, Object errorDetails); 56 57 /** 58 * Decodes a result envelope from binary. 59 * 60 * @param envelope the binary encoding of a result envelope as a {@link ByteBuffer}. 61 * @return the enveloped result Object. 62 * @throws FlutterException if the envelope was an error envelope. 63 */ decodeEnvelope(ByteBuffer envelope)64 Object decodeEnvelope(ByteBuffer envelope); 65 } 66