> Documentation index: https://developer.android.com/reference/llms.txt

# SharedArrayBuffer

Artifact: [androidx.webkit:webkit](https://developer.android.com/jetpack/androidx/releases/webkit.md.txt) [View Source](https://cs.android.com/search?q=file:androidx/webkit/SharedArrayBuffer.java+class:androidx.webkit.SharedArrayBuffer) Added in [1.19.0-alpha03](https://developer.android.com/jetpack/androidx/releases/webkit.md.txt#1.19.0-alpha03)

*** ** * ** ***


```
class SharedArrayBuffer
```

<br />

*** ** * ** ***

The Java representation of a JavaScript [SharedArrayBuffer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer).

A `SharedArrayBuffer` is a region of memory that is shared between the application and the web content running in WebView, which means that writes performed on one side are visible on the other side without copying the data or posting another message.

Use `allocate` to allocate a new buffer, and send it to web content by posting a `WebMessageCompat` carrying it, or by replying with `postMessage`. Web content that is not [cross-origin isolated](https://developer.mozilla.org/en-US/docs/Web/API/Window/crossOriginIsolated) cannot use a `SharedArrayBuffer`; see `setCrossOriginIsolatedAllowlist`.

Shared memory provides no notification when its contents change. An application that needs to observe writes made by web content has to read the buffer again, or has to be told by web content through a separate message.

Any web content that holds this buffer can write arbitrary data into it at any time. Treat the contents as untrusted input.

| See also |
|---|---|
| `SharedMemory` |   |

## Summary

| ### Public functions |
|---|---|
| `java-static SharedArrayBuffer` | `@AnyThread @RequiresFeature(name = WebViewFeature.WEB_MESSAGE_SHARED_ARRAY_BUFFER, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported") allocate(name: String?, size: @IntRange(from = 1) Int)` Allocates a new `SharedArrayBuffer` of `size` bytes. |
| `@IntRange(from = 1) Int` | `getSize()` Returns the size of the shared memory region in bytes. |
| `ByteBuffer` | `mapAndCreateByteBuffer()` Returns a direct `ByteBuffer` that gives access to the shared memory region. |

## Public functions

### allocate

Added in [1.19.0-alpha03](https://developer.android.com/jetpack/androidx/releases/webkit.md.txt#1.19.0-alpha03)

```
@AnyThread
@RequiresFeature(name = WebViewFeature.WEB_MESSAGE_SHARED_ARRAY_BUFFER, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
java-static fun allocate(name: String?, size: @IntRange(from = 1) Int): SharedArrayBuffer
```

Allocates a new `SharedArrayBuffer` of `size` bytes.

The memory is allocated as an `android.os.SharedMemory` region in the application's process, and is accounted against the application.

| Parameters |
|---|---|
| `name: String?` | an optional name for the underlying shared memory region, which is only used for debugging purposes and does not need to be unique. See `create`. |
| `size: @IntRange(from = 1) Int` | the size of the buffer in bytes. |

| Returns |
|---|---|
| `SharedArrayBuffer` | the newly allocated `SharedArrayBuffer`. |

| Throws |
|---|---|
| `android.system.ErrnoException` | if the shared memory region could not be allocated. |
| `java.lang.IllegalArgumentException` | if `size` is outside of the supported range. |
| `java.lang.UnsupportedOperationException` | if the `WEB_MESSAGE_SHARED_ARRAY_BUFFER` feature is not supported. This should be checked before use with `isFeatureSupported`. |

### getSize

Added in [1.19.0-alpha03](https://developer.android.com/jetpack/androidx/releases/webkit.md.txt#1.19.0-alpha03)

```
fun getSize(): @IntRange(from = 1) Int
```

Returns the size of the shared memory region in bytes.

### mapAndCreateByteBuffer

Added in [1.19.0-alpha03](https://developer.android.com/jetpack/androidx/releases/webkit.md.txt#1.19.0-alpha03)

```
fun mapAndCreateByteBuffer(): ByteBuffer
```

Returns a direct `ByteBuffer` that gives access to the shared memory region.

The region is mapped into the application's address space the first time this method is called. Every call returns a new `ByteBuffer` over that same mapping, with its own independent position, limit and mark. All of them, as well as the `SharedArrayBuffer` held by web content, observe each other's writes.

The returned buffer has a capacity of `getSize` bytes, and its byte order is `nativeOrder`, which matches the byte order of JavaScript typed arrays such as `Int32Array`. Calling `duplicate` or `slice` on the returned buffer resets the byte order to `BIG_ENDIAN`.

| Returns |
|---|---|
| `ByteBuffer` | a direct `ByteBuffer` backed by the shared memory region. |

| Throws |
|---|---|
| `android.system.ErrnoException` | if the shared memory region could not be mapped. |