18c339a94Sopenharmony_ci# ArrayBuffer 28c339a94Sopenharmony_ci 38c339a94Sopenharmony_ciClass `Napi::ArrayBuffer` inherits from class [`Napi::Object`][]. 48c339a94Sopenharmony_ci 58c339a94Sopenharmony_ciThe `Napi::ArrayBuffer` class corresponds to the 68c339a94Sopenharmony_ci[JavaScript `ArrayBuffer`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) 78c339a94Sopenharmony_ciclass. 88c339a94Sopenharmony_ci 98c339a94Sopenharmony_ci## Methods 108c339a94Sopenharmony_ci 118c339a94Sopenharmony_ci### New 128c339a94Sopenharmony_ci 138c339a94Sopenharmony_ciAllocates a new `Napi::ArrayBuffer` instance with a given length. 148c339a94Sopenharmony_ci 158c339a94Sopenharmony_ci```cpp 168c339a94Sopenharmony_cistatic Napi::ArrayBuffer Napi::ArrayBuffer::New(napi_env env, size_t byteLength); 178c339a94Sopenharmony_ci``` 188c339a94Sopenharmony_ci 198c339a94Sopenharmony_ci- `[in] env`: The environment in which to create the `Napi::ArrayBuffer` instance. 208c339a94Sopenharmony_ci- `[in] byteLength`: The length to be allocated, in bytes. 218c339a94Sopenharmony_ci 228c339a94Sopenharmony_ciReturns a new `Napi::ArrayBuffer` instance. 238c339a94Sopenharmony_ci 248c339a94Sopenharmony_ci### New 258c339a94Sopenharmony_ci 268c339a94Sopenharmony_ci> When `NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED` is defined, this method is not available. 278c339a94Sopenharmony_ci> See [External Buffer][] for more information. 288c339a94Sopenharmony_ci 298c339a94Sopenharmony_ciWraps the provided external data into a new `Napi::ArrayBuffer` instance. 308c339a94Sopenharmony_ci 318c339a94Sopenharmony_ciThe `Napi::ArrayBuffer` instance does not assume ownership for the data and 328c339a94Sopenharmony_ciexpects it to be valid for the lifetime of the instance. Since the 338c339a94Sopenharmony_ci`Napi::ArrayBuffer` is subject to garbage collection this overload is only 348c339a94Sopenharmony_cisuitable for data which is static and never needs to be freed. 358c339a94Sopenharmony_ciThis factory method will not provide the caller with an opportunity to free the 368c339a94Sopenharmony_cidata when the `Napi::ArrayBuffer` gets garbage-collected. If you need to free 378c339a94Sopenharmony_cithe data retained by the `Napi::ArrayBuffer` object please use other 388c339a94Sopenharmony_civariants of the `Napi::ArrayBuffer::New` factory method that accept 398c339a94Sopenharmony_ci`Napi::Finalizer`, which is a function that will be invoked when the 408c339a94Sopenharmony_ci`Napi::ArrayBuffer` object has been destroyed. 418c339a94Sopenharmony_ci 428c339a94Sopenharmony_ci```cpp 438c339a94Sopenharmony_cistatic Napi::ArrayBuffer Napi::ArrayBuffer::New(napi_env env, void* externalData, size_t byteLength); 448c339a94Sopenharmony_ci``` 458c339a94Sopenharmony_ci 468c339a94Sopenharmony_ci- `[in] env`: The environment in which to create the `Napi::ArrayBuffer` instance. 478c339a94Sopenharmony_ci- `[in] externalData`: The pointer to the external data to wrap. 488c339a94Sopenharmony_ci- `[in] byteLength`: The length of the `externalData`, in bytes. 498c339a94Sopenharmony_ci 508c339a94Sopenharmony_ciReturns a new `Napi::ArrayBuffer` instance. 518c339a94Sopenharmony_ci 528c339a94Sopenharmony_ci### New 538c339a94Sopenharmony_ci 548c339a94Sopenharmony_ci> When `NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED` is defined, this method is not available. 558c339a94Sopenharmony_ci> See [External Buffer][] for more information. 568c339a94Sopenharmony_ci 578c339a94Sopenharmony_ciWraps the provided external data into a new `Napi::ArrayBuffer` instance. 588c339a94Sopenharmony_ci 598c339a94Sopenharmony_ciThe `Napi::ArrayBuffer` instance does not assume ownership for the data and 608c339a94Sopenharmony_ciexpects it to be valid for the lifetime of the instance. The data can only be 618c339a94Sopenharmony_cifreed once the `finalizeCallback` is invoked to indicate that the 628c339a94Sopenharmony_ci`Napi::ArrayBuffer` has been released. 638c339a94Sopenharmony_ci 648c339a94Sopenharmony_ci```cpp 658c339a94Sopenharmony_citemplate <typename Finalizer> 668c339a94Sopenharmony_cistatic Napi::ArrayBuffer Napi::ArrayBuffer::New(napi_env env, 678c339a94Sopenharmony_ci void* externalData, 688c339a94Sopenharmony_ci size_t byteLength, 698c339a94Sopenharmony_ci Finalizer finalizeCallback); 708c339a94Sopenharmony_ci``` 718c339a94Sopenharmony_ci 728c339a94Sopenharmony_ci- `[in] env`: The environment in which to create the `Napi::ArrayBuffer` instance. 738c339a94Sopenharmony_ci- `[in] externalData`: The pointer to the external data to wrap. 748c339a94Sopenharmony_ci- `[in] byteLength`: The length of the `externalData`, in bytes. 758c339a94Sopenharmony_ci- `[in] finalizeCallback`: A function to be called when the `Napi::ArrayBuffer` is 768c339a94Sopenharmony_ci destroyed. It must implement `operator()`, accept an Napi::Env, a `void*` (which is the 778c339a94Sopenharmony_ci `externalData` pointer), and return `void`. 788c339a94Sopenharmony_ci 798c339a94Sopenharmony_ciReturns a new `Napi::ArrayBuffer` instance. 808c339a94Sopenharmony_ci 818c339a94Sopenharmony_ci### New 828c339a94Sopenharmony_ci 838c339a94Sopenharmony_ci> When `NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED` is defined, this method is not available. 848c339a94Sopenharmony_ci> See [External Buffer][] for more information. 858c339a94Sopenharmony_ci 868c339a94Sopenharmony_ciWraps the provided external data into a new `Napi::ArrayBuffer` instance. 878c339a94Sopenharmony_ci 888c339a94Sopenharmony_ciThe `Napi::ArrayBuffer` instance does not assume ownership for the data and expects it 898c339a94Sopenharmony_cito be valid for the lifetime of the instance. The data can only be freed once 908c339a94Sopenharmony_cithe `finalizeCallback` is invoked to indicate that the `Napi::ArrayBuffer` has been 918c339a94Sopenharmony_cireleased. 928c339a94Sopenharmony_ci 938c339a94Sopenharmony_ci```cpp 948c339a94Sopenharmony_citemplate <typename Finalizer, typename Hint> 958c339a94Sopenharmony_cistatic Napi::ArrayBuffer Napi::ArrayBuffer::New(napi_env env, 968c339a94Sopenharmony_ci void* externalData, 978c339a94Sopenharmony_ci size_t byteLength, 988c339a94Sopenharmony_ci Finalizer finalizeCallback, 998c339a94Sopenharmony_ci Hint* finalizeHint); 1008c339a94Sopenharmony_ci``` 1018c339a94Sopenharmony_ci 1028c339a94Sopenharmony_ci- `[in] env`: The environment in which to create the `Napi::ArrayBuffer` instance. 1038c339a94Sopenharmony_ci- `[in] externalData`: The pointer to the external data to wrap. 1048c339a94Sopenharmony_ci- `[in] byteLength`: The length of the `externalData`, in bytes. 1058c339a94Sopenharmony_ci- `[in] finalizeCallback`: The function to be called when the `Napi::ArrayBuffer` is 1068c339a94Sopenharmony_ci destroyed. It must implement `operator()`, accept an Napi::Env, a `void*` (which is the 1078c339a94Sopenharmony_ci `externalData` pointer) and `Hint*`, and return `void`. 1088c339a94Sopenharmony_ci- `[in] finalizeHint`: The hint to be passed as the second parameter of the 1098c339a94Sopenharmony_ci finalize callback. 1108c339a94Sopenharmony_ci 1118c339a94Sopenharmony_ciReturns a new `Napi::ArrayBuffer` instance. 1128c339a94Sopenharmony_ci 1138c339a94Sopenharmony_ci### Constructor 1148c339a94Sopenharmony_ci 1158c339a94Sopenharmony_ciInitializes an empty instance of the `Napi::ArrayBuffer` class. 1168c339a94Sopenharmony_ci 1178c339a94Sopenharmony_ci```cpp 1188c339a94Sopenharmony_ciNapi::ArrayBuffer::ArrayBuffer(); 1198c339a94Sopenharmony_ci``` 1208c339a94Sopenharmony_ci 1218c339a94Sopenharmony_ci### Constructor 1228c339a94Sopenharmony_ci 1238c339a94Sopenharmony_ciInitializes a wrapper instance of an existing `Napi::ArrayBuffer` object. 1248c339a94Sopenharmony_ci 1258c339a94Sopenharmony_ci```cpp 1268c339a94Sopenharmony_ciNapi::ArrayBuffer::ArrayBuffer(napi_env env, napi_value value); 1278c339a94Sopenharmony_ci``` 1288c339a94Sopenharmony_ci 1298c339a94Sopenharmony_ci- `[in] env`: The environment in which to create the `Napi::ArrayBuffer` instance. 1308c339a94Sopenharmony_ci- `[in] value`: The `Napi::ArrayBuffer` reference to wrap. 1318c339a94Sopenharmony_ci 1328c339a94Sopenharmony_ci### ByteLength 1338c339a94Sopenharmony_ci 1348c339a94Sopenharmony_ci```cpp 1358c339a94Sopenharmony_cisize_t Napi::ArrayBuffer::ByteLength() const; 1368c339a94Sopenharmony_ci``` 1378c339a94Sopenharmony_ci 1388c339a94Sopenharmony_ciReturns the length of the wrapped data, in bytes. 1398c339a94Sopenharmony_ci 1408c339a94Sopenharmony_ci### Data 1418c339a94Sopenharmony_ci 1428c339a94Sopenharmony_ci```cpp 1438c339a94Sopenharmony_civoid* Napi::ArrayBuffer::Data() const; 1448c339a94Sopenharmony_ci``` 1458c339a94Sopenharmony_ci 1468c339a94Sopenharmony_ciReturns a pointer the wrapped data. 1478c339a94Sopenharmony_ci 1488c339a94Sopenharmony_ci### Detach 1498c339a94Sopenharmony_ci 1508c339a94Sopenharmony_ci```cpp 1518c339a94Sopenharmony_civoid Napi::ArrayBuffer::Detach(); 1528c339a94Sopenharmony_ci``` 1538c339a94Sopenharmony_ci 1548c339a94Sopenharmony_ciInvokes the `ArrayBuffer` detach operation on a detachable `ArrayBuffer`. 1558c339a94Sopenharmony_ci 1568c339a94Sopenharmony_ci### IsDetached 1578c339a94Sopenharmony_ci 1588c339a94Sopenharmony_ci```cpp 1598c339a94Sopenharmony_cibool Napi::ArrayBuffer::IsDetached() const; 1608c339a94Sopenharmony_ci``` 1618c339a94Sopenharmony_ci 1628c339a94Sopenharmony_ciReturns `true` if this `ArrayBuffer` has been detached. 1638c339a94Sopenharmony_ci 1648c339a94Sopenharmony_ci[`Napi::Object`]: ./object.md 1658c339a94Sopenharmony_ci[External Buffer]: ./external_buffer.md 166