> 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.
A **Buffer** object represents a fixed-length sequence of bytes. 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.
You can use the APIs provided by the **Buffer** module to process images and a large amount of binary data, and receive or upload files.
## Modules to Import
...
...
@@ -52,7 +53,7 @@ let buf3 = buffer.alloc(11, 'aGVsbG8gd29ybGQ=', 'base64');
allocUninitializedFromPool(size: number): Buffer
Allocates a **Buffer** instance of the specified size from the buffer pool, without initializing it.
To initialize the **Buffer** instance, call the **fill()** function.
To initialize the **Buffer** instance, call **fill()**.
@@ -157,7 +158,7 @@ Compares two **Buffer** instances.
| 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.|
| -1 \| 0 \| 1 | 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.|
**Example**
...
...
@@ -166,8 +167,9 @@ import buffer from '@ohos.buffer';
letbuf1=buffer.from('1234');
letbuf2=buffer.from('0123');
letres=buf1.compare(buf2);
console.log(buf1.compare(buf2));
console.log(Number(res).toString());
// Print 1
```
...
...
@@ -200,12 +202,12 @@ import buffer from '@ohos.buffer';
| 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.|
| targetEnd | number | No| Offset to the end of the data to compare in the target **Buffer** instance (not inclusive). The default value is the 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.|
**Return value**
| 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.|
| number | Returns **0** if the two **Buffer** instances are the same.<br>Returns **1** if this instance comes after the target instance when sorted.<br>Returns **-1** if this instance comes before the target instance when sorted. |
**Example**
...
...
@@ -452,9 +453,9 @@ import buffer from '@ohos.buffer';
@@ -596,7 +597,7 @@ Checks whether this **Buffer** instance contains the specified value.
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| 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**.|
| byteOffset | number | No| Number of bytes to skip before starting to check data. If the offset is a negative number, data is checked 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**
...
...
@@ -611,8 +612,8 @@ Checks whether this **Buffer** instance contains the specified value.
@@ -628,7 +629,7 @@ Obtains the index of the first occurrence of the specified value in this **Buffe
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| 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**.|
| byteOffset | number | No| Number of bytes to skip before starting to check data. If the offset is a negative number, data is checked 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**
...
...
@@ -643,8 +644,8 @@ Obtains the index of the first occurrence of the specified value in this **Buffe
@@ -668,7 +669,7 @@ import buffer from '@ohos.buffer';
letbuf=buffer.from('buffer');
for(constkeyofbuf.keys()){
console.log(key);
console.log(key.toString());
}
```
...
...
@@ -685,7 +686,7 @@ Obtains the index of the last occurrence of the specified value in this **Buffer
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| 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**.|
| byteOffset | number | No| Number of bytes to skip before starting to check data. If the offset is a negative number, data is checked 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**
...
...
@@ -700,16 +701,16 @@ Obtains the index of the last occurrence of the specified value in this **Buffer