public final class Message


Represents a message that can be sent or received between two message ports.

Summary

Constants

static final int

The message is a byte[] representing a JavaScript ArrayBuffer.

static final int

The message is a String.

Public methods

static @NonNull Message

Creates a new message with a JavaScript ArrayBuffer (byte[]) payload.

static @NonNull Message

Creates a new message with a String payload.

@NonNull byte[]

Returns the encapsulated JavaScript ArrayBuffer data as a byte[].

@NonNull String

Returns the encapsulated String data.

int

Returns the type of the data stored in the message.

@NonNull String

Produce a string representation of the message for debugging purposes.

Constants

TYPE_ARRAY_BUFFER

Added in 1.1.0-alpha02
public static final int TYPE_ARRAY_BUFFER = 1

The message is a byte[] representing a JavaScript ArrayBuffer.

TYPE_STRING

Added in 1.1.0-alpha02
public static final int TYPE_STRING = 0

The message is a String.

Public methods

createArrayBufferMessage

Added in 1.1.0-alpha02
public static @NonNull Message createArrayBufferMessage(@NonNull byte[] bytes)

Creates a new message with a JavaScript ArrayBuffer (byte[]) payload.

This method does not create a copy of the byte array; the message object will only hold a reference to the original byte array. Data is only copied during message posting. As such, you should generally avoid modifying the original supplied array after creating the message.

Parameters
@NonNull byte[] bytes

the contents of the ArrayBuffer.

Returns
@NonNull Message

a new Message instance with the ArrayBuffer payload.

createStringMessage

Added in 1.1.0-alpha02
public static @NonNull Message createStringMessage(@NonNull String string)

Creates a new message with a String payload.

String messages must contain valid Unicode. Invalid Unicode, such as unpaired surrogates may result in message corruption. To send binary data, use createArrayBufferMessage.

Parameters
@NonNull String string

the string payload.

Returns
@NonNull Message

a new Message instance with the string payload.

getArrayBuffer

Added in 1.1.0-alpha02
public @NonNull byte[] getArrayBuffer()

Returns the encapsulated JavaScript ArrayBuffer data as a byte[].

No implicit conversions are performed. If the data type of the message is not an ArrayBuffer, a MessageTypeMismatchException will be thrown. You should only call this method if getType returns TYPE_ARRAY_BUFFER.

This method only obtains a reference to the backing array and does not create a copy. Modifications to the returned byte array will modify the content of this message instance.

Returns
@NonNull byte[]

the encapsulated ArrayBuffer data as a byte[].

Throws
androidx.javascriptengine.MessageTypeMismatchException

if the message type is not an ArrayBuffer.

getString

Added in 1.1.0-alpha02
public @NonNull String getString()

Returns the encapsulated String data.

No implicit conversions are performed. If the data type of the message is not a String, a MessageTypeMismatchException will be thrown. You should only call this method if getType returns TYPE_STRING.

Returns
@NonNull String

the encapsulated String data.

Throws
androidx.javascriptengine.MessageTypeMismatchException

if the message type is not a string.

getType

Added in 1.1.0-alpha02
public int getType()

Returns the type of the data stored in the message.

Note on Forward Compatibility:

Support for new types may be added over time. An application should generally be prepared to accept messages of previously undefined types, particularly when it processes external scripts that may be updated separately from the application. This may necessitate else blocks or default case labels to handle unknown types, or simply ignoring messages of unmatched types if appropriate.

As such, you should avoid assuming the data type unless you have positively confirmed it specifically, else you may encounter a MessageTypeMismatchException due to using an inappropriate getter.

Returns
int

the data type of the message.

toString

public @NonNull String toString()

Produce a string representation of the message for debugging purposes.

The output of toString may not be stable across API versions. Do not attempt to parse its output or rely on any property about it in main application logic.

Returns
@NonNull String

a string representation of the message.