> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
A **Buffer** object represents a byte sequence of a fixed length. It is used to store binary data.
You can use the APIs provided by the **Buffer** module to process images and a large amount of binary data, receive and upload files, and use network protocols.
| buf1 | Buffer \| Uint8Array | Yes| **Buffer** instance to compare.|
| buf2 | Buffer \| Uint8Array | Yes| **Buffer** instance to compare.|
**Return value**
| Type| Description|
| -------- | -------- |
| number | Returns **0** if **buf1** is the same as **buf2**.<br>Returns **1** if **buf1** comes after **buf2** when sorted.<br>Returns **-1** if **buf1** comes before **buf2** when sorted.|
| arrayBuffer | ArrayBuffer \| SharedArrayBuffer | Yes| Array of **Buffer** instances, whose memory is to be shared.|
| byteOffset | number | No| Byte offset. The default value is **0**.|
| length | number | No| Length of the **Buffer** instance to create, in bytes. The default value is the length of **arrayBuffer** minus **byteOffset**.|
**Return value**
| Type| Description|
| -------- | -------- |
| Buffer | **Buffer** instance with shared memory.|
**Example**
```ts
importbufferfrom'@ohos.buffer';
letab=newArrayBuffer(10);
letbuf=buffer.from(ab,0,2);
```
### from
from(data: Buffer | Uint8Array): Buffer
Creates a **Buffer** instance with the copy of another instance.
| targetStart | number | No| Offset to the start of the data to compare in the target **Buffer** instance. The default value is **0**.|
| targetEnd | number | No| Offset to the end of the data to compare in the target **Buffer** instance (not inclusive). The default value is length of the target **Buffer** instance.|
| sourceStart | number | No| Offset to the start of the data to compare in this **Buffer** instance. The default value is **0**.|
| sourceEnd | number | No| Offset to the end of the data to compare in this **Buffer** instance (not inclusive). The default value is the length of this **Buffer** instance.|
| Type| Description|
| -------- | -------- |
| number | Returns **0** if the two **Buffer** instances are the same.<br>Returns **1** if the target instance comes before this instance when sorted.<br>Returns **-1** if the target instance comes after this instance when sorted.|
| target | Buffer \| Uint8Array | Yes| Instance to which data is copied.|
| targetStart | number | No| Offset to the start position in the target instance where data is copied. The default value is **0**.|
| sourceStart | number | No| Offset to the start position in this **Buffer** instance where data is copied. The default value is **0**.|
| sourceEnd | number | No| Offset to the end position in this **Buffer** instance (not inclusive). The default value is the length of this **Buffer** instance.|
**Return value**
| Type| Description|
| -------- | -------- |
| number | Total length of the data copied, in bytes.|
| value | string \| Buffer \| Uint8Array \| number | Yes| Value to fill.|
| offset | number | No| Offset to the start position in this **Buffer** instance where data is filled. The default value is **0**.|
| end | number | No| Offset to the end position in this **Buffer** instance (not inclusive). The default value is the length of this **Buffer** instance.|
| encoding | BufferEncoding | No| Encoding format (valid only when **value** is a string). The default value is **utf-8**.|
**Return value**
| Type| Description|
| -------- | -------- |
| Buffer | **Buffer** instance filled with the specified value.|
| value | string \| number \| Buffer \| Uint8Array | Yes| Value to match.|
| byteOffset | number | No| Number of bytes to skip before checking data. If the offset is a negative number, data is searched from the end of the **Buffer** instance. The default value is **0**.|
| encoding | BufferEncoding | No| Encoding format used if **value** is a string. The default value is **utf-8**.|
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the instance contains the specified value; returns **false** otherwise.|
**Example**
```ts
importbufferfrom'@ohos.buffer';
letbuf=buffer.from('this is a buffer');
console.log(buf.includes('this'));// Print: true
console.log(buf.includes('be'));// Print: false
```
### indexOf
indexOf(value: string | number | Buffer | Uint8Array, byteOffset?: number, encoding?: BufferEncoding): number
Obtains the index of the first occurrence of the specified value in this **Buffer** instance.
| value | string \| number \| Buffer \| Uint8Array | Yes| Value to match.|
| byteOffset | number | No| Number of bytes to skip before checking data. If the offset is a negative number, data is searched from the end of the **Buffer** instance. The default value is **0**.|
| encoding | BufferEncoding | No| Encoding format used if **value** is a string. The default value is **utf-8**.|
**Return value**
| Type| Description|
| -------- | -------- |
| number | Index obtained. <br>If **-1** is returned, the **Buffer** instance does not contain the specified value.|
**Example**
```ts
importbufferfrom'@ohos.buffer';
letbuf=buffer.from('this is a buffer');
console.log(buf.indexOf('this'));// Print: 0
console.log(buf.indexOf('is'));// Print: 2
```
### keys
keys(): IterableIterator<number>
Creates and returns an iterator that contains the keys of this **Buffer** instance.
| value | string \| number \| Buffer \| Uint8Array | Yes| Value to match.|
| byteOffset | number | No| Number of bytes to skip before checking data. If the offset is a negative number, data is searched from the end of the **Buffer** instance. The default value is **0**.|
| encoding | BufferEncoding | No| Encoding format used if **value** is a string. The default value is **utf-8**.|
**Return value**
| Type| Description|
| -------- | -------- |
| number | Index obtained.<br>If **-1** is returned, the **Buffer** instance does not contain the specified value.|
readIntBE(offset: number, byteLength: number): number
Reads the specified number of bytes from this **Buffer** instance at the specified offset, and interprets the result as a big-endian, two's complement signed value that supports up to 48 bits of precision.
| offset | number | No| Number of bytes to skip before reading data. The default value is **0**.|
| byteLength | number | No| Number of bytes to read.|
**Return value**
| Type| Description|
| -------- | -------- |
| number | Data read.|
**Example**
```ts
importbufferfrom'@ohos.buffer';
letbuf=Buffer.from("ab");
letnum=buf.readIntBE(0,1);
console.log(num);// 97
```
### readIntLE
readIntLE(offset: number, byteLength: number): number
Reads the specified number of bytes from this **Buffer** instance at the specified offset and interprets the result as a little-endian, two's complement signed value that supports up to 48 bits of precision.
| offset | number | No| Number of bytes to skip before reading data. The default value is **0**.|
**Return value**
| Type| Description|
| -------- | -------- |
| number | An unsigned, little-endian 32-bit integer.|
**Example**
```ts
importbufferfrom'@ohos.buffer';
letbuf=buffer.from([0x12,0x34,0x56,0x78]);
console.log(buf.readUInt32LE(0).toString(16));
```
### readUIntBE
readUIntBE(offset: number, byteLength: number): number
Reads the specified number of bytes from this **Buffer** instance at the specified offset, and interprets the result as an unsigned, big-endian integer that supports up to 48 bits of precision.
readUIntLE(offset: number, byteLength: number): number
Reads the specified number of bytes from this **Buffer** instance at the specified offset, and interprets the result as an unsigned, little-endian integer that supports up to 48 bits of precision.
| start | number | No| Offset to the start position in this **Buffer** instance where data is truncated. The default value is **0**.|
| end | number | No| Offset to the end position in this **Buffer** instance (not inclusive). The default value is the length of this **Buffer** instance.|
| sources | string[] \| ArrayBuffer[] \| TypedArray[] \| DataView[] \| Blob[] | Yes| Data sources of the **Blob** instance.|
| options | Object | No| options:<br>- **endings**: 'transparent' or 'native'.<br>- **type**: type of the data in **Blob**.|