# Message

Artifact: [androidx.javascriptengine:javascriptengine](https://developer.android.com/jetpack/androidx/releases/javascriptengine) [View Source](https://cs.android.com/search?q=file:androidx/javascriptengine/Message.java+class:androidx.javascriptengine.Message) Added in [1.1.1](https://developer.android.com/jetpack/androidx/releases/javascriptengine#1.1.1)

*** ** * ** ***


```
class Message
```

<br />

*** ** * ** ***

A variant type for data that can be sent and received between two message ports.

Messages can be used with a `https://developer.android.com/reference/kotlin/androidx/javascriptengine/MessagePort` to communicate with a JavaScript isolate. Messages can package either string or binary data and provide methods to inspect the underlying JavaScript type represented.

## Summary

| ### Constants |
|---|---|
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/javascriptengine/Message#TYPE_ARRAY_BUFFER() = 1` The message is a `byte[]` representing a JavaScript ArrayBuffer. |
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/javascriptengine/Message#TYPE_STRING() = 0` The message is a `https://developer.android.com/reference/java/lang/String.html`. |

| ### Public functions |
|---|---|
| `java-static https://developer.android.com/reference/kotlin/androidx/javascriptengine/Message` | `https://developer.android.com/reference/kotlin/androidx/javascriptengine/Message#createArrayBufferMessage(byte[])(bytes: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte/index.html>)` Creates a new message with a JavaScript ArrayBuffer (`byte[]`) payload. |
| `java-static https://developer.android.com/reference/kotlin/androidx/javascriptengine/Message` | `https://developer.android.com/reference/kotlin/androidx/javascriptengine/Message#createStringMessage(java.lang.String)(string: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)` Creates a new message with a `https://developer.android.com/reference/java/lang/String.html` payload. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte/index.html>` | `https://developer.android.com/reference/kotlin/androidx/javascriptengine/Message#getArrayBuffer()()` Returns the encapsulated JavaScript ArrayBuffer data as a `byte[]`. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | `https://developer.android.com/reference/kotlin/androidx/javascriptengine/Message#getString()()` Returns the encapsulated String data. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/javascriptengine/Message#getType()()` Returns the type of the data stored in the message. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | `https://developer.android.com/reference/kotlin/androidx/javascriptengine/Message#toString()()` Produce a string representation of the message for debugging purposes. |

## Constants

### TYPE_ARRAY_BUFFER

Added in [1.1.1](https://developer.android.com/jetpack/androidx/releases/javascriptengine#1.1.1)

```
const val TYPE_ARRAY_BUFFER = 1: Int
```

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

### TYPE_STRING

Added in [1.1.1](https://developer.android.com/jetpack/androidx/releases/javascriptengine#1.1.1)

```
const val TYPE_STRING = 0: Int
```

The message is a `https://developer.android.com/reference/java/lang/String.html`.

## Public functions

### createArrayBufferMessage

Added in [1.1.1](https://developer.android.com/jetpack/androidx/releases/javascriptengine#1.1.1)

```
java-static fun createArrayBufferMessage(bytes: ByteArray<Byte>): 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: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte/index.html>` | the contents of the ArrayBuffer. |

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/javascriptengine/Message` | a new Message instance with the ArrayBuffer payload. |

### createStringMessage

Added in [1.1.1](https://developer.android.com/jetpack/androidx/releases/javascriptengine#1.1.1)

```
java-static fun createStringMessage(string: String): Message
```

Creates a new message with a `https://developer.android.com/reference/java/lang/String.html` payload.

String messages must contain valid Unicode. Invalid Unicode, such as unpaired surrogates may result in message corruption. To send binary data, use `https://developer.android.com/reference/kotlin/androidx/javascriptengine/Message#createArrayBufferMessage(byte[])`.

| Parameters |
|---|---|
| `string: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | the string payload. |

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/javascriptengine/Message` | a new Message instance with the string payload. |

### getArrayBuffer

Added in [1.1.1](https://developer.android.com/jetpack/androidx/releases/javascriptengine#1.1.1)

```
fun getArrayBuffer(): Array<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 `https://developer.android.com/reference/kotlin/androidx/javascriptengine/MessageTypeMismatchException` will be thrown. You should only call this method if `https://developer.android.com/reference/kotlin/androidx/javascriptengine/Message#getType()` returns `https://developer.android.com/reference/kotlin/androidx/javascriptengine/Message#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 |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte/index.html>` | the encapsulated ArrayBuffer data as a `byte[]`. |

| Throws |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/javascriptengine/MessageTypeMismatchException` | if the message type is not an ArrayBuffer. |

### getString

Added in [1.1.1](https://developer.android.com/jetpack/androidx/releases/javascriptengine#1.1.1)

```
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 `https://developer.android.com/reference/kotlin/androidx/javascriptengine/MessageTypeMismatchException` will be thrown. You should only call this method if `https://developer.android.com/reference/kotlin/androidx/javascriptengine/Message#getType()` returns `https://developer.android.com/reference/kotlin/androidx/javascriptengine/Message#TYPE_STRING()`.

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | the encapsulated String data. |

| Throws |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/javascriptengine/MessageTypeMismatchException` | if the message type is not a string. |

### getType

Added in [1.1.1](https://developer.android.com/jetpack/androidx/releases/javascriptengine#1.1.1)

```
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 |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | 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 |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | a string representation of the message. |