18c339a94Sopenharmony_ci# Buffer 28c339a94Sopenharmony_ci 38c339a94Sopenharmony_ciClass `Napi::Buffer` inherits from class [`Napi::Uint8Array`][]. 48c339a94Sopenharmony_ci 58c339a94Sopenharmony_ciThe `Napi::Buffer` class creates a projection of raw data that can be consumed by 68c339a94Sopenharmony_ciscript. 78c339a94Sopenharmony_ci 88c339a94Sopenharmony_ci## Methods 98c339a94Sopenharmony_ci 108c339a94Sopenharmony_ci### New 118c339a94Sopenharmony_ci 128c339a94Sopenharmony_ciAllocates a new `Napi::Buffer` object with a given length. 138c339a94Sopenharmony_ci 148c339a94Sopenharmony_ci```cpp 158c339a94Sopenharmony_cistatic Napi::Buffer<T> Napi::Buffer::New(napi_env env, size_t length); 168c339a94Sopenharmony_ci``` 178c339a94Sopenharmony_ci 188c339a94Sopenharmony_ci- `[in] env`: The environment in which to create the `Napi::Buffer` object. 198c339a94Sopenharmony_ci- `[in] length`: The number of `T` elements to allocate. 208c339a94Sopenharmony_ci 218c339a94Sopenharmony_ciReturns a new `Napi::Buffer` object. 228c339a94Sopenharmony_ci 238c339a94Sopenharmony_ci### New 248c339a94Sopenharmony_ci 258c339a94Sopenharmony_ci> When `NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED` is defined, this method is not available. 268c339a94Sopenharmony_ci> See [External Buffer][] for more information. 278c339a94Sopenharmony_ci 288c339a94Sopenharmony_ciWraps the provided external data into a new `Napi::Buffer` object. 298c339a94Sopenharmony_ci 308c339a94Sopenharmony_ciThe `Napi::Buffer` object does not assume ownership for the data and expects it to be 318c339a94Sopenharmony_civalid for the lifetime of the object. Since the `Napi::Buffer` is subject to garbage 328c339a94Sopenharmony_cicollection this overload is only suitable for data which is static and never 338c339a94Sopenharmony_cineeds to be freed. 348c339a94Sopenharmony_ciThis factory method will not provide the caller with an opportunity to free the 358c339a94Sopenharmony_cidata when the `Napi::Buffer` gets garbage-collected. If you need to free the 368c339a94Sopenharmony_cidata retained by the `Napi::Buffer` object please use other variants of the 378c339a94Sopenharmony_ci`Napi::Buffer::New` factory method that accept `Napi::Finalizer`, which is a 388c339a94Sopenharmony_cifunction that will be invoked when the `Napi::Buffer` object has been 398c339a94Sopenharmony_cidestroyed. 408c339a94Sopenharmony_ci 418c339a94Sopenharmony_ci```cpp 428c339a94Sopenharmony_cistatic Napi::Buffer<T> Napi::Buffer::New(napi_env env, T* data, size_t length); 438c339a94Sopenharmony_ci``` 448c339a94Sopenharmony_ci 458c339a94Sopenharmony_ci- `[in] env`: The environment in which to create the `Napi::Buffer` object. 468c339a94Sopenharmony_ci- `[in] data`: The pointer to the external data to expose. 478c339a94Sopenharmony_ci- `[in] length`: The number of `T` elements in the external data. 488c339a94Sopenharmony_ci 498c339a94Sopenharmony_ciReturns a new `Napi::Buffer` object. 508c339a94Sopenharmony_ci 518c339a94Sopenharmony_ci### New 528c339a94Sopenharmony_ci 538c339a94Sopenharmony_ci> When `NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED` is defined, this method is not available. 548c339a94Sopenharmony_ci> See [External Buffer][] for more information. 558c339a94Sopenharmony_ci 568c339a94Sopenharmony_ciWraps the provided external data into a new `Napi::Buffer` object. 578c339a94Sopenharmony_ci 588c339a94Sopenharmony_ciThe `Napi::Buffer` object does not assume ownership for the data and expects it 598c339a94Sopenharmony_cito be valid for the lifetime of the object. The data can only be freed once the 608c339a94Sopenharmony_ci`finalizeCallback` is invoked to indicate that the `Napi::Buffer` has been released. 618c339a94Sopenharmony_ci 628c339a94Sopenharmony_ci```cpp 638c339a94Sopenharmony_citemplate <typename Finalizer> 648c339a94Sopenharmony_cistatic Napi::Buffer<T> Napi::Buffer::New(napi_env env, 658c339a94Sopenharmony_ci T* data, 668c339a94Sopenharmony_ci size_t length, 678c339a94Sopenharmony_ci Finalizer finalizeCallback); 688c339a94Sopenharmony_ci``` 698c339a94Sopenharmony_ci 708c339a94Sopenharmony_ci- `[in] env`: The environment in which to create the `Napi::Buffer` object. 718c339a94Sopenharmony_ci- `[in] data`: The pointer to the external data to expose. 728c339a94Sopenharmony_ci- `[in] length`: The number of `T` elements in the external data. 738c339a94Sopenharmony_ci- `[in] finalizeCallback`: The function to be called when the `Napi::Buffer` is 748c339a94Sopenharmony_ci destroyed. It must implement `operator()`, accept an Napi::Env, a `T*` (which is the 758c339a94Sopenharmony_ci external data pointer), and return `void`. 768c339a94Sopenharmony_ci 778c339a94Sopenharmony_ciReturns a new `Napi::Buffer` object. 788c339a94Sopenharmony_ci 798c339a94Sopenharmony_ci### New 808c339a94Sopenharmony_ci 818c339a94Sopenharmony_ci> When `NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED` is defined, this method is not available. 828c339a94Sopenharmony_ci> See [External Buffer][] for more information. 838c339a94Sopenharmony_ci 848c339a94Sopenharmony_ciWraps the provided external data into a new `Napi::Buffer` object. 858c339a94Sopenharmony_ci 868c339a94Sopenharmony_ciThe `Napi::Buffer` object does not assume ownership for the data and expects it to be 878c339a94Sopenharmony_civalid for the lifetime of the object. The data can only be freed once the 888c339a94Sopenharmony_ci`finalizeCallback` is invoked to indicate that the `Napi::Buffer` has been released. 898c339a94Sopenharmony_ci 908c339a94Sopenharmony_ci```cpp 918c339a94Sopenharmony_citemplate <typename Finalizer, typename Hint> 928c339a94Sopenharmony_cistatic Napi::Buffer<T> Napi::Buffer::New(napi_env env, 938c339a94Sopenharmony_ci T* data, 948c339a94Sopenharmony_ci size_t length, 958c339a94Sopenharmony_ci Finalizer finalizeCallback, 968c339a94Sopenharmony_ci Hint* finalizeHint); 978c339a94Sopenharmony_ci``` 988c339a94Sopenharmony_ci 998c339a94Sopenharmony_ci- `[in] env`: The environment in which to create the `Napi::Buffer` object. 1008c339a94Sopenharmony_ci- `[in] data`: The pointer to the external data to expose. 1018c339a94Sopenharmony_ci- `[in] length`: The number of `T` elements in the external data. 1028c339a94Sopenharmony_ci- `[in] finalizeCallback`: The function to be called when the `Napi::Buffer` is 1038c339a94Sopenharmony_ci destroyed. It must implement `operator()`, accept an Napi::Env, a `T*` (which is the 1048c339a94Sopenharmony_ci external data pointer) and `Hint*`, and return `void`. 1058c339a94Sopenharmony_ci- `[in] finalizeHint`: The hint to be passed as the second parameter of the 1068c339a94Sopenharmony_ci finalize callback. 1078c339a94Sopenharmony_ci 1088c339a94Sopenharmony_ciReturns a new `Napi::Buffer` object. 1098c339a94Sopenharmony_ci 1108c339a94Sopenharmony_ci### NewOrCopy 1118c339a94Sopenharmony_ci 1128c339a94Sopenharmony_ciWraps the provided external data into a new `Napi::Buffer` object. When the 1138c339a94Sopenharmony_ci[external buffer][] is not supported, allocates a new `Napi::Buffer` object and 1148c339a94Sopenharmony_cicopies the provided external data into it. 1158c339a94Sopenharmony_ci 1168c339a94Sopenharmony_ciThe `Napi::Buffer` object does not assume ownership for the data and expects it to be 1178c339a94Sopenharmony_civalid for the lifetime of the object. Since the `Napi::Buffer` is subject to garbage 1188c339a94Sopenharmony_cicollection this overload is only suitable for data which is static and never 1198c339a94Sopenharmony_cineeds to be freed. 1208c339a94Sopenharmony_ci 1218c339a94Sopenharmony_ciThis factory method will not provide the caller with an opportunity to free the 1228c339a94Sopenharmony_cidata when the `Napi::Buffer` gets garbage-collected. If you need to free the 1238c339a94Sopenharmony_cidata retained by the `Napi::Buffer` object please use other variants of the 1248c339a94Sopenharmony_ci`Napi::Buffer::New` factory method that accept `Napi::Finalizer`, which is a 1258c339a94Sopenharmony_cifunction that will be invoked when the `Napi::Buffer` object has been 1268c339a94Sopenharmony_cidestroyed. 1278c339a94Sopenharmony_ci 1288c339a94Sopenharmony_ci```cpp 1298c339a94Sopenharmony_cistatic Napi::Buffer<T> Napi::Buffer::NewOrCopy(napi_env env, T* data, size_t length); 1308c339a94Sopenharmony_ci``` 1318c339a94Sopenharmony_ci 1328c339a94Sopenharmony_ci- `[in] env`: The environment in which to create the `Napi::Buffer` object. 1338c339a94Sopenharmony_ci- `[in] data`: The pointer to the external data to expose. 1348c339a94Sopenharmony_ci- `[in] length`: The number of `T` elements in the external data. 1358c339a94Sopenharmony_ci 1368c339a94Sopenharmony_ciReturns a new `Napi::Buffer` object. 1378c339a94Sopenharmony_ci 1388c339a94Sopenharmony_ci### NewOrCopy 1398c339a94Sopenharmony_ci 1408c339a94Sopenharmony_ciWraps the provided external data into a new `Napi::Buffer` object. When the 1418c339a94Sopenharmony_ci[external buffer][] is not supported, allocates a new `Napi::Buffer` object and 1428c339a94Sopenharmony_cicopies the provided external data into it and the `finalizeCallback` is invoked 1438c339a94Sopenharmony_ciimmediately. 1448c339a94Sopenharmony_ci 1458c339a94Sopenharmony_ciThe `Napi::Buffer` object does not assume ownership for the data and expects it 1468c339a94Sopenharmony_cito be valid for the lifetime of the object. The data can only be freed once the 1478c339a94Sopenharmony_ci`finalizeCallback` is invoked to indicate that the `Napi::Buffer` has been released. 1488c339a94Sopenharmony_ci 1498c339a94Sopenharmony_ci```cpp 1508c339a94Sopenharmony_citemplate <typename Finalizer> 1518c339a94Sopenharmony_cistatic Napi::Buffer<T> Napi::Buffer::NewOrCopy(napi_env env, 1528c339a94Sopenharmony_ci T* data, 1538c339a94Sopenharmony_ci size_t length, 1548c339a94Sopenharmony_ci Finalizer finalizeCallback); 1558c339a94Sopenharmony_ci``` 1568c339a94Sopenharmony_ci 1578c339a94Sopenharmony_ci- `[in] env`: The environment in which to create the `Napi::Buffer` object. 1588c339a94Sopenharmony_ci- `[in] data`: The pointer to the external data to expose. 1598c339a94Sopenharmony_ci- `[in] length`: The number of `T` elements in the external data. 1608c339a94Sopenharmony_ci- `[in] finalizeCallback`: The function to be called when the `Napi::Buffer` is 1618c339a94Sopenharmony_ci destroyed. It must implement `operator()`, accept an Napi::Env, a `T*` (which is the 1628c339a94Sopenharmony_ci external data pointer), and return `void`. 1638c339a94Sopenharmony_ci 1648c339a94Sopenharmony_ciReturns a new `Napi::Buffer` object. 1658c339a94Sopenharmony_ci 1668c339a94Sopenharmony_ci### NewOrCopy 1678c339a94Sopenharmony_ci 1688c339a94Sopenharmony_ciWraps the provided external data into a new `Napi::Buffer` object. When the 1698c339a94Sopenharmony_ci[external buffer][] is not supported, allocates a new `Napi::Buffer` object and 1708c339a94Sopenharmony_cicopies the provided external data into it and the `finalizeCallback` is invoked 1718c339a94Sopenharmony_ciimmediately. 1728c339a94Sopenharmony_ci 1738c339a94Sopenharmony_ciThe `Napi::Buffer` object does not assume ownership for the data and expects it to be 1748c339a94Sopenharmony_civalid for the lifetime of the object. The data can only be freed once the 1758c339a94Sopenharmony_ci`finalizeCallback` is invoked to indicate that the `Napi::Buffer` has been released. 1768c339a94Sopenharmony_ci 1778c339a94Sopenharmony_ci```cpp 1788c339a94Sopenharmony_citemplate <typename Finalizer, typename Hint> 1798c339a94Sopenharmony_cistatic Napi::Buffer<T> Napi::Buffer::NewOrCopy(napi_env env, 1808c339a94Sopenharmony_ci T* data, 1818c339a94Sopenharmony_ci size_t length, 1828c339a94Sopenharmony_ci Finalizer finalizeCallback, 1838c339a94Sopenharmony_ci Hint* finalizeHint); 1848c339a94Sopenharmony_ci``` 1858c339a94Sopenharmony_ci 1868c339a94Sopenharmony_ci- `[in] env`: The environment in which to create the `Napi::Buffer` object. 1878c339a94Sopenharmony_ci- `[in] data`: The pointer to the external data to expose. 1888c339a94Sopenharmony_ci- `[in] length`: The number of `T` elements in the external data. 1898c339a94Sopenharmony_ci- `[in] finalizeCallback`: The function to be called when the `Napi::Buffer` is 1908c339a94Sopenharmony_ci destroyed. It must implement `operator()`, accept an Napi::Env, a `T*` (which is the 1918c339a94Sopenharmony_ci external data pointer) and `Hint*`, and return `void`. 1928c339a94Sopenharmony_ci- `[in] finalizeHint`: The hint to be passed as the second parameter of the 1938c339a94Sopenharmony_ci finalize callback. 1948c339a94Sopenharmony_ci 1958c339a94Sopenharmony_ciReturns a new `Napi::Buffer` object. 1968c339a94Sopenharmony_ci 1978c339a94Sopenharmony_ci### Copy 1988c339a94Sopenharmony_ci 1998c339a94Sopenharmony_ciAllocates a new `Napi::Buffer` object and copies the provided external data into it. 2008c339a94Sopenharmony_ci 2018c339a94Sopenharmony_ci```cpp 2028c339a94Sopenharmony_cistatic Napi::Buffer<T> Napi::Buffer::Copy(napi_env env, const T* data, size_t length); 2038c339a94Sopenharmony_ci``` 2048c339a94Sopenharmony_ci 2058c339a94Sopenharmony_ci- `[in] env`: The environment in which to create the `Napi::Buffer` object. 2068c339a94Sopenharmony_ci- `[in] data`: The pointer to the external data to copy. 2078c339a94Sopenharmony_ci- `[in] length`: The number of `T` elements in the external data. 2088c339a94Sopenharmony_ci 2098c339a94Sopenharmony_ciReturns a new `Napi::Buffer` object containing a copy of the data. 2108c339a94Sopenharmony_ci 2118c339a94Sopenharmony_ci### Constructor 2128c339a94Sopenharmony_ci 2138c339a94Sopenharmony_ciInitializes an empty instance of the `Napi::Buffer` class. 2148c339a94Sopenharmony_ci 2158c339a94Sopenharmony_ci```cpp 2168c339a94Sopenharmony_ciNapi::Buffer::Buffer(); 2178c339a94Sopenharmony_ci``` 2188c339a94Sopenharmony_ci 2198c339a94Sopenharmony_ci### Constructor 2208c339a94Sopenharmony_ci 2218c339a94Sopenharmony_ciInitializes the `Napi::Buffer` object using an existing Uint8Array. 2228c339a94Sopenharmony_ci 2238c339a94Sopenharmony_ci```cpp 2248c339a94Sopenharmony_ciNapi::Buffer::Buffer(napi_env env, napi_value value); 2258c339a94Sopenharmony_ci``` 2268c339a94Sopenharmony_ci 2278c339a94Sopenharmony_ci- `[in] env`: The environment in which to create the `Napi::Buffer` object. 2288c339a94Sopenharmony_ci- `[in] value`: The Uint8Array reference to wrap. 2298c339a94Sopenharmony_ci 2308c339a94Sopenharmony_ci### Data 2318c339a94Sopenharmony_ci 2328c339a94Sopenharmony_ci```cpp 2338c339a94Sopenharmony_ciT* Napi::Buffer::Data() const; 2348c339a94Sopenharmony_ci``` 2358c339a94Sopenharmony_ci 2368c339a94Sopenharmony_ciReturns a pointer the external data. 2378c339a94Sopenharmony_ci 2388c339a94Sopenharmony_ci### Length 2398c339a94Sopenharmony_ci 2408c339a94Sopenharmony_ci```cpp 2418c339a94Sopenharmony_cisize_t Napi::Buffer::Length() const; 2428c339a94Sopenharmony_ci``` 2438c339a94Sopenharmony_ci 2448c339a94Sopenharmony_ciReturns the number of `T` elements in the external data. 2458c339a94Sopenharmony_ci 2468c339a94Sopenharmony_ci[`Napi::Uint8Array`]: ./typed_array_of.md 2478c339a94Sopenharmony_ci[External Buffer]: ./external_buffer.md 248