# 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)

*** ** * ** ***


```
public final 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/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 |
|---|---|
| `static final int` | `https://developer.android.com/reference/androidx/javascriptengine/Message#TYPE_ARRAY_BUFFER() = 1` The message is a `byte[]` representing a JavaScript ArrayBuffer. |
| `static final int` | `https://developer.android.com/reference/androidx/javascriptengine/Message#TYPE_STRING() = 0` The message is a `https://developer.android.com/reference/java/lang/String.html`. |

| ### Public methods |
|---|---|
| `static @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/javascriptengine/Message` | `https://developer.android.com/reference/androidx/javascriptengine/Message#createArrayBufferMessage(byte[])(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html byte[] bytes)` Creates a new message with a JavaScript ArrayBuffer (`byte[]`) payload. |
| `static @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/javascriptengine/Message` | `https://developer.android.com/reference/androidx/javascriptengine/Message#createStringMessage(java.lang.String)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html string)` Creates a new message with a `https://developer.android.com/reference/java/lang/String.html` payload. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html byte[]` | `https://developer.android.com/reference/androidx/javascriptengine/Message#getArrayBuffer()()` Returns the encapsulated JavaScript ArrayBuffer data as a `byte[]`. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/javascriptengine/Message#getString()()` Returns the encapsulated String data. |
| `int` | `https://developer.android.com/reference/androidx/javascriptengine/Message#getType()()` Returns the type of the data stored in the message. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/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)

```
public static final int TYPE_ARRAY_BUFFER = 1
```

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)

```
public static final int TYPE_STRING = 0
```

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

## Public methods

### createArrayBufferMessage

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

```
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 |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html byte[] bytes` | the contents of the ArrayBuffer. |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/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)

```
public static @NonNull Message createStringMessage(@NonNull String string)
```

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/androidx/javascriptengine/Message#createArrayBufferMessage(byte[])`.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html string` | the string payload. |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/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)

```
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 `https://developer.android.com/reference/androidx/javascriptengine/MessageTypeMismatchException` will be thrown. You should only call this method if `https://developer.android.com/reference/androidx/javascriptengine/Message#getType()` returns `https://developer.android.com/reference/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://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html byte[]` | the encapsulated ArrayBuffer data as a `byte[]`. |

| Throws |
|---|---|
| `https://developer.android.com/reference/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)

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

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html` | the encapsulated String data. |

| Throws |
|---|---|
| `https://developer.android.com/reference/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)

```
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 |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html` | a string representation of the message. |