class Message


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

Summary

Constants

const Int

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

const Int

The message is a String.

Public functions

java-static Message

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

java-static Message

Creates a new message with a String payload.

ByteArray<Byte>

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

String

Returns the encapsulated String data.

Int

Returns the type of the data stored in the message.

String

Produce a string representation of the message for debugging purposes.

Constants

TYPE_ARRAY_BUFFER

Added in 1.1.0-alpha02
const val TYPE_ARRAY_BUFFER = 1: Int

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

TYPE_STRING

Added in 1.1.0-alpha02
const val TYPE_STRING = 0: Int

The message is a String.

Public functions

createArrayBufferMessage

Added in 1.1.0-alpha02
java-static fun createArrayBufferMessage(bytes: ByteArray): Message

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
bytes: ByteArray

the contents of the ArrayBuffer.

Returns
Message

a new Message instance with the ArrayBuffer payload.

createStringMessage

Added in 1.1.0-alpha02
java-static fun createStringMessage(string: String): Message

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
string: String

the string payload.

Returns
Message

a new Message instance with the string payload.

getArrayBuffer

Added in 1.1.0-alpha02
fun getArrayBuffer(): ByteArray<Byte>

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
ByteArray<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
fun getString(): String

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
String

the encapsulated String data.

Throws
androidx.javascriptengine.MessageTypeMismatchException

if the message type is not a string.

getType

Added in 1.1.0-alpha02
fun getType(): Int

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

fun toString(): String

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
String

a string representation of the message.