1# @ohos.buffer (Buffer) 2 3A **Buffer** object represents a byte sequence of a fixed length. It is used to store binary data. 4 5**Recommended use case**: Use Buffer when you need to process images and a large amount of binary data, and receive or upload files. 6 7> **NOTE** 8> 9> 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. 10 11## Modules to Import 12 13```ts 14import { buffer } from '@kit.ArkTS'; 15``` 16 17## BufferEncoding 18 19Enumerates the supported encoding formats. 20 21**Atomic service API**: This API can be used in atomic services since API version 11. 22 23**System capability**: SystemCapability.Utils.Lang 24 25| Type | Description | 26| ------- | -------------------- | 27| 'ascii' | ASCII format.| 28| 'utf8' | UTF-8 format.| 29| 'utf-8' | UTF-8 format.| 30| 'utf16le' | UTF-16LE format.| 31| 'ucs2' | Alias of UTF-16LE.| 32| 'ucs-2' | Alias of UTF-16LE.| 33| 'base64' | Base64 format.| 34| 'base64url' | Base64URL format.| 35| 'latin1' | Alias of iso-8859-1, which is backward compatible with the ASCII format.| 36| 'binary' | Binary format.| 37| 'hex' | Hexadecimal format.| 38 39## buffer.alloc 40 41alloc(size: number, fill?: string | Buffer | number, encoding?: BufferEncoding): Buffer 42 43Creates and initializes a **Buffer** object of the specified length. 44 45**Atomic service API**: This API can be used in atomic services since API version 11. 46 47**System capability**: SystemCapability.Utils.Lang 48 49**Parameters** 50 51| Name| Type| Mandatory| Description| 52| -------- | -------- | -------- | -------- | 53| size | number | Yes| Size of the **Buffer** object to create, in bytes.| 54| fill | string \| [Buffer](#buffer) \| number | No| Value to be filled in the buffer. The default value is **0**.| 55| encoding | [BufferEncoding](#bufferencoding) | No| Encoding format (valid only when **fill** is a string). The default value is **'utf8'**.| 56 57**Return value** 58 59| Type| Description| 60| -------- | -------- | 61| [Buffer](#buffer) | **Buffer** object created.| 62 63**Error codes** 64 65For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 66 67| ID| Error Message| 68| -------- | -------- | 69| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 70 71**Example** 72 73```ts 74import { buffer, JSON } from '@kit.ArkTS'; 75 76let buf1 = buffer.alloc(5); 77console.info(JSON.stringify(buf1)); // {"type":"Buffer","data":[0,0,0,0,0]} 78 79let buf2 = buffer.alloc(5, 'a'); 80console.info(JSON.stringify(buf2)); // {"type":"Buffer","data":[97,97,97,97,97]} 81 82let buf3 = buffer.alloc(11, 'aGVsbG8gd29ybGQ=', 'base64'); 83console.info(JSON.stringify(buf3)); // {"type":"Buffer","data":[104,101,108,108,111,32,119,111,114,108,100]} 84``` 85 86## buffer.allocUninitializedFromPool 87 88allocUninitializedFromPool(size: number): Buffer 89 90Creates a **Buffer** object of the specified size from the buffer pool, without initializing it. 91 92You need to use [fill()](#fill) to initialize the **Buffer** object created. 93 94**Atomic service API**: This API can be used in atomic services since API version 11. 95 96**System capability**: SystemCapability.Utils.Lang 97 98**Parameters** 99 100| Name| Type| Mandatory| Description| 101| -------- | -------- | -------- | -------- | 102| size | number | Yes| Size of the **Buffer** object to create, in bytes.| 103 104**Return value** 105 106| Type| Description| 107| -------- | -------- | 108| [Buffer](#buffer) | Uninitialized **Buffer** object.| 109 110**Error codes** 111 112For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 113 114| ID| Error Message| 115| -------- | -------- | 116| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 117 118**Example** 119 120```ts 121import { buffer, JSON } from '@kit.ArkTS'; 122 123let buf = buffer.allocUninitializedFromPool(10); 124buf.fill(0); 125console.info(JSON.stringify(buf)); // {"type":"Buffer","data":[0,0,0,0,0,0,0,0,0,0]} 126``` 127 128## buffer.allocUninitialized 129 130allocUninitialized(size: number): Buffer 131 132Creates a **Buffer** object of the specified size, without initializing it. This API does not allocate memory from the buffer pool. 133 134You need to use [fill()](#fill) to initialize the **Buffer** object created. 135 136**Atomic service API**: This API can be used in atomic services since API version 11. 137 138**System capability**: SystemCapability.Utils.Lang 139 140**Parameters** 141 142| Name| Type| Mandatory| Description| 143| -------- | -------- | -------- | -------- | 144| size | number | Yes|Size of the **Buffer** object to create, in bytes.| 145 146**Return value** 147 148| Type| Description| 149| -------- | -------- | 150| [Buffer](#buffer) | Uninitialized **Buffer** object.| 151 152**Error codes** 153 154For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 155 156| ID| Error Message| 157| -------- | -------- | 158| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 159 160**Example** 161 162```ts 163import { buffer, JSON } from '@kit.ArkTS'; 164 165let buf = buffer.allocUninitialized(10); 166buf.fill(0); 167console.info(JSON.stringify(buf)); // {"type":"Buffer","data":[0,0,0,0,0,0,0,0,0,0]} 168``` 169 170## buffer.byteLength 171 172byteLength(string: string | Buffer | TypedArray | DataView | ArrayBuffer | SharedArrayBuffer, encoding?: BufferEncoding): number 173 174Obtains the number of bytes of a string based on the encoding format. 175 176**Atomic service API**: This API can be used in atomic services since API version 11. 177 178**System capability**: SystemCapability.Utils.Lang 179 180**Parameters** 181 182| Name| Type| Mandatory| Description| 183| -------- | -------- | -------- | -------- | 184| string | string \| [Buffer](#buffer) \| TypedArray \| DataView \| ArrayBuffer \| SharedArrayBuffer | Yes| Target string.| 185| encoding | [BufferEncoding](#bufferencoding) | No| Encoding format. The default value is **'utf8'**.| 186 187**Return value** 188 189| Type| Description| 190| -------- | -------- | 191| number | Number of bytes of the string.| 192 193**Error codes** 194 195For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 196 197| ID| Error Message| 198| -------- | -------- | 199| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 200 201**Example** 202 203```ts 204import { buffer } from '@kit.ArkTS'; 205 206let str = '\u00bd + \u00bc = \u00be'; 207console.info(`${str}: ${str.length} characters, ${buffer.byteLength(str, 'utf-8')} bytes`); 208// Output: ½ + ¼ = ¾: 9 characters, 12 bytes 209``` 210 211## buffer.compare 212 213compare(buf1: Buffer | Uint8Array, buf2: Buffer | Uint8Array): -1 | 0 | 1 214 215Compares two **Buffer** objects. This API is used for sorting **Buffer** objects. 216 217**Atomic service API**: This API can be used in atomic services since API version 11. 218 219**System capability**: SystemCapability.Utils.Lang 220 221**Parameters** 222 223| Name| Type| Mandatory| Description| 224| -------- | -------- | -------- | -------- | 225| buf1 | [Buffer](#buffer) \| Uint8Array | Yes| **Buffer** object to compare.| 226| buf2 | [Buffer](#buffer) \| Uint8Array | Yes| **Buffer** object to compare.| 227 228**Return value** 229 230| Type| Description| 231| -------- | -------- | 232| -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.| 233 234**Error codes** 235 236For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 237 238| ID| Error Message| 239| -------- | -------- | 240| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 241 242**Example** 243 244```ts 245import { buffer } from '@kit.ArkTS'; 246 247let buf1 = buffer.from('1234'); 248let buf2 = buffer.from('0123'); 249let res = buffer.compare(buf1, buf2); 250 251console.info(Number(res).toString()); 252// Output: 1 253``` 254 255## buffer.concat 256 257concat(list: Buffer[] | Uint8Array[], totalLength?: number): Buffer 258 259Concatenates an array of **Buffer** objects of the specified length into a new object. 260 261**System capability**: SystemCapability.Utils.Lang 262 263**Atomic service API**: This API can be used in atomic services since API version 11. 264 265**Parameters** 266 267| Name| Type| Mandatory| Description| 268| -------- | -------- | -------- | -------- | 269| list | Buffer[] \| Uint8Array[] | Yes| Array of objects to concatenate.| 270| totalLength | number | No| Total length of bytes to be copied. The default value is **0**.| 271 272**Return value** 273 274| Type| Description| 275| -------- | -------- | 276| [Buffer](#buffer) | **Buffer** object created.| 277 278**Error codes** 279 280For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 281 282| ID| Error Message| 283| -------- | -------- | 284| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 285| 10200001 | The value of "length" is out of range. It must be >= 0 and <= uint32 max. Received value is: [length] | 286 287**Example** 288 289```ts 290import { buffer } from '@kit.ArkTS'; 291 292let buf1 = buffer.from("1234"); 293let buf2 = buffer.from("abcd"); 294let buf = buffer.concat([buf1, buf2]); 295console.info(buf.toString('hex')); 296// Output: 3132333461626364 297``` 298 299## buffer.from 300 301from(array: number[]): Buffer 302 303Creates a **Buffer** object with the specified array. 304 305**System capability**: SystemCapability.Utils.Lang 306 307**Atomic service API**: This API can be used in atomic services since API version 11. 308 309**Parameters** 310 311| Name| Type| Mandatory| Description| 312| -------- | -------- | -------- | -------- | 313| array | number[] | Yes| Array to create a **Buffer** object.| 314 315**Return value** 316 317| Type| Description| 318| -------- | -------- | 319| [Buffer](#buffer) | **Buffer** object created.| 320 321**Error codes** 322 323For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 324 325| ID| Error Message| 326| -------- | -------- | 327| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 328 329**Example** 330 331```ts 332import { buffer } from '@kit.ArkTS'; 333 334let buf = buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]); 335console.info(buf.toString('hex')); 336// Output: 627566666572 337``` 338 339## buffer.from 340 341from(arrayBuffer: ArrayBuffer | SharedArrayBuffer, byteOffset?: number, length?: number): Buffer 342 343Creates a **Buffer** object of the specified length that shares memory with ArrayBuffer. 344 345**Atomic service API**: This API can be used in atomic services since API version 11. 346 347**System capability**: SystemCapability.Utils.Lang 348 349**Parameters** 350 351| Name| Type| Mandatory| Description| 352| -------- | -------- | -------- | -------- | 353| arrayBuffer | ArrayBuffer \| SharedArrayBuffer | Yes| **ArrayBuffer** or **SharedArrayBuffer** object whose memory is to be shared.| 354| byteOffset | number | No| Byte offset. The default value is **0**.| 355| length | number | No| Length of the **Buffer** object to create, in bytes. The default value is **arrayBuffer.byteLength** minus **byteOffset**.| 356 357**Return value** 358 359| Type| Description| 360| -------- | -------- | 361| [Buffer](#buffer) | **Buffer** object created.| 362 363**Error codes** 364 365For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 366 367| ID| Error Message| 368| -------- | -------- | 369| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 370| 10200001 | The value of "[byteOffset/length]" is out of range. It must be >= [left range] and <= [right range]. Received value is: [byteOffset/length] | 371 372**Example** 373 374```ts 375import { buffer, JSON } from '@kit.ArkTS'; 376 377let ab = new ArrayBuffer(10); 378let buf = buffer.from(ab, 0, 2); 379console.info(JSON.stringify(buf)); // {"type":"Buffer","data":[0,0]} 380``` 381 382## buffer.from 383 384from(buffer: Buffer | Uint8Array): Buffer 385 386Copies the data of a passed **Buffer** object to create a new **Buffer** object and returns the new one. 387 388Creates a **Buffer** object based on the memory of a passed **Uint8Array** object and returns the new object, maintaining the memory association of the data. 389 390**Atomic service API**: This API can be used in atomic services since API version 11. 391 392**System capability**: SystemCapability.Utils.Lang 393 394**Parameters** 395 396| Name| Type| Mandatory| Description| 397| -------- | -------- | -------- | -------- | 398| buffer | [Buffer](#buffer) \| Uint8Array | Yes| Target object.| 399 400**Return value** 401 402| Type| Description| 403| -------- | -------- | 404| [Buffer](#buffer) | **Buffer** object created.| 405 406**Error codes** 407 408For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 409 410| ID| Error Message| 411| -------- | -------- | 412| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 413 414**Example** 415 416```ts 417import { buffer } from '@kit.ArkTS'; 418 419// Create a Buffer object of the Buffer type. 420let buf1 = buffer.from('buffer'); 421let buf2 = buffer.from(buf1); 422 423// Create a Buffer object of the Uint8Array type to ensure memory sharing between objects. 424let uint8Array = new Uint8Array(10); 425let buf3 = buffer.from(uint8Array); 426buf3.fill(1); 427console.info("uint8Array:", uint8Array); 428// Output: 1,1,1,1,1,1,1,1,1,1 429``` 430 431## buffer.from 432 433from(object: Object, offsetOrEncoding: number | string, length: number): Buffer 434 435Creates a **Buffer** object based on the specified object. 436 437**Atomic service API**: This API can be used in atomic services since API version 11. 438 439**System capability**: SystemCapability.Utils.Lang 440 441**Parameters** 442 443| Name| Type| Mandatory| Description| 444| -------- | -------- | -------- | -------- | 445| object | Object | Yes| Object that supports **Symbol.toPrimitive** or **valueOf()**.| 446| offsetOrEncoding | number \| string | Yes| Byte offset or encoding format.| 447| length | number | Yes| Length of the **Buffer** object to create, in bytes. This parameter is valid only when the return value of **valueOf()** of **object** is **ArrayBuffer**. Value range: 0 <= length <= ArrayBuffer.byteLength. Error 10200001 is reported if a value outside this range is reported. In other cases, you can set this parameter to any value of the number type. This parameter does not affect the result.| 448 449**Return value** 450 451| Type| Description| 452| -------- | -------- | 453| [Buffer](#buffer) | **Buffer** object created.| 454 455**Error codes** 456 457For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 458 459| ID| Error Message| 460| -------- | -------- | 461| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 462 463**Example** 464 465```ts 466import { buffer, JSON } from '@kit.ArkTS'; 467 468let buf = buffer.from(new String('this is a test'), 'utf8', 14); 469console.info(JSON.stringify(buf)); // {"type":"Buffer","data":[116,104,105,115,32,105,115,32,97,32,116,101,115,116]} 470``` 471 472## buffer.from 473 474from(string: String, encoding?: BufferEncoding): Buffer 475 476Creates a **Buffer** object based on a string in the given encoding format. 477 478**Atomic service API**: This API can be used in atomic services since API version 11. 479 480**System capability**: SystemCapability.Utils.Lang 481 482**Parameters** 483 484| Name| Type| Mandatory| Description| 485| -------- | -------- | -------- | -------- | 486| string | String | Yes| String.| 487| encoding | [BufferEncoding](#bufferencoding) | No| Encoding format. The default value is **'utf8'**.| 488 489**Return value** 490 491| Type| Description| 492| -------- | -------- | 493| [Buffer](#buffer) | **Buffer** object created.| 494 495**Error codes** 496 497For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 498 499| ID| Error Message| 500| -------- | -------- | 501| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 502 503**Example** 504 505```ts 506import { buffer } from '@kit.ArkTS'; 507 508let buf1 = buffer.from('this is a test'); 509let buf2 = buffer.from('7468697320697320612074c3a97374', 'hex'); 510 511console.info(buf1.toString()); 512// Output: this is a test 513console.info(buf2.toString()); 514// Output: this is a tést 515``` 516 517 518## buffer.isBuffer 519 520isBuffer(obj: Object): boolean 521 522Checks whether the specified object is a **Buffer** object. 523 524**Atomic service API**: This API can be used in atomic services since API version 11. 525 526**System capability**: SystemCapability.Utils.Lang 527 528**Parameters** 529 530| Name| Type| Mandatory| Description| 531| -------- | -------- | -------- | -------- | 532| obj | Object | Yes| Object to check.| 533 534**Return value** 535 536| Type| Description| 537| -------- | -------- | 538| boolean | Check result. The value **true** is returned if the object is a **Buffer** object; otherwise, **false** is returned.| 539 540**Example** 541 542```ts 543import { buffer } from '@kit.ArkTS'; 544 545let result = buffer.isBuffer(buffer.alloc(10)); // 10: buffer size 546console.info("result = " + result); 547// Output: result = true 548let result1 = buffer.isBuffer(buffer.from('foo')); 549console.info("result1 = " + result1); 550// Output: result1 = true 551let result2 = buffer.isBuffer('a string'); 552console.info("result2 = " + result2); 553// Output: result2 = false 554let result3 = buffer.isBuffer([]); 555console.info("result3 = " + result3); 556// Output: result3 = false 557let result4 = buffer.isBuffer(new Uint8Array(1024)); 558console.info("result4 = " + result4); 559// Output: result4 = false 560``` 561 562## buffer.isEncoding 563 564isEncoding(encoding: string): boolean 565 566Checks whether the encoding format is supported. 567 568**Atomic service API**: This API can be used in atomic services since API version 11. 569 570**System capability**: SystemCapability.Utils.Lang 571 572**Parameters** 573 574| Name| Type| Mandatory| Description| 575| -------- | -------- | -------- | -------- | 576| encoding | string | Yes| Encoding format.| 577 578**Return value** 579 580| Type| Description| 581| -------- | -------- | 582| boolean | Check result. The value **true** is returned if the encoding format is supported; otherwise, **false** is returned.| 583 584**Example** 585 586```ts 587import { buffer } from '@kit.ArkTS'; 588 589console.info(buffer.isEncoding('utf-8').toString()); 590// Output: true 591console.info(buffer.isEncoding('hex').toString()); 592// Output: true 593console.info(buffer.isEncoding('utf/8').toString()); 594// Output: false 595console.info(buffer.isEncoding('').toString()); 596// Output: false 597``` 598 599## buffer.transcode 600 601transcode(source: Buffer | Uint8Array, fromEnc: string, toEnc: string): Buffer 602 603Transcodes a **Buffer** or **Uint8Array** object from one encoding format to another. 604 605**System capability**: SystemCapability.Utils.Lang 606 607**Atomic service API**: This API can be used in atomic services since API version 11. 608 609**Parameters** 610 611| Name| Type| Mandatory| Description| 612| -------- | -------- | -------- | -------- | 613| source | [Buffer](#buffer) \| Uint8Array | Yes| Object to encode.| 614| fromEnc | string | Yes| Current encoding format. For details about the supported formats, see [BufferEncoding](#bufferencoding).| 615| toEnc | string | Yes| Target encoding format. For details about the supported formats, see [BufferEncoding](#bufferencoding).| 616 617**Return value** 618 619| Type| Description| 620| -------- | -------- | 621| [Buffer](#buffer) | New **Buffer** object in the target encoding format.| 622 623**Error codes** 624 625For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 626 627| ID| Error Message| 628| -------- | -------- | 629| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 630 631**Example** 632 633```ts 634import { buffer } from '@kit.ArkTS'; 635 636let newBuf = buffer.transcode(buffer.from('€'), 'utf-8', 'ascii'); 637console.info("newBuf = " + newBuf.toString('ascii')); 638// Output: newBuf = , 639``` 640 641## Buffer 642 643### Properties 644 645**System capability**: SystemCapability.Utils.Lang 646 647**Atomic service API**: This API can be used in atomic services since API version 11. 648 649| Name| Type| Read-Only| Optional| Description| 650| -------- | -------- | -------- | -------- | -------- | 651| length | number | Yes| No| Length of the **Buffer** object, in bytes.| 652| buffer | ArrayBuffer | Yes| No| **ArrayBuffer** object.| 653| byteOffset | number | Yes| No| Offset of the **Buffer** object in the memory pool.| 654 655**Error codes** 656 657For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 658 659| ID| Error Message| 660| -------- | -------- | 661| 10200013 | ${propertyName} cannot be set for the buffer that has only a getter. | 662 663**Example** 664 665```ts 666import { buffer } from '@kit.ArkTS'; 667 668let buf = buffer.from("1236"); 669console.info(JSON.stringify(buf.length)); 670// Output: 4 671let arrayBuffer = buf.buffer; 672console.info(JSON.stringify(new Uint8Array(arrayBuffer))); 673// Output: {"0":49,"1":50,"2":51,"3":54} 674console.info(JSON.stringify(buf.byteOffset)); 675// Output: 0 676``` 677 678### compare 679 680compare(target: Buffer | Uint8Array, targetStart?: number, targetEnd?: number, sourceStart?: number, sourceEnd?: number): -1 | 0 | 1 681 682Compares this **Buffer** object with another object. 683 684**Atomic service API**: This API can be used in atomic services since API version 11. 685 686**System capability**: SystemCapability.Utils.Lang 687 688**Parameters** 689 690| Name| Type| Mandatory| Description| 691| -------- | -------- | -------- | -------- | 692| target | [Buffer](#buffer) \| Uint8Array | Yes| Target **Buffer** object to compare.| 693| targetStart | number | No| Offset to the start of the data to compare in the target **Buffer** object. The default value is **0**.| 694| targetEnd | number | No| Offset to the end of the data to compare in the target **Buffer** object (not inclusive). The default value is the length of the target **Buffer** object.| 695| sourceStart | number | No| Offset to the start of the data to compare in this **Buffer** object. The default value is **0**.| 696| sourceEnd | number | No| Offset to the end of the data to compare in this **Buffer** object (not inclusive). The default value is the length of this **Buffer** object.| 697 698**Return value** 699 700| Type| Description| 701| -------- | -------- | 702| number | Comparison result. The value **0** is returned if the two **Buffer** objects are the same; **1** is returned if this object comes after the target object when sorted; **-1** is returned if this object comes before the target object when sorted.| 703 704**Error codes** 705 706For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 707 708| ID| Error Message| 709| -------- | -------- | 710| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 711| 10200001 | The value of "[targetStart/targetEnd/sourceStart/sourceEnd]" is out of range. | 712 713**Example** 714 715```ts 716import { buffer } from '@kit.ArkTS'; 717 718let buf1 = buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9]); 719let buf2 = buffer.from([5, 6, 7, 8, 9, 1, 2, 3, 4]); 720 721console.info(buf1.compare(buf2, 5, 9, 0, 4).toString()); 722// Output: 0 723console.info(buf1.compare(buf2, 0, 6, 4).toString()); 724// Output: -1 725console.info(buf1.compare(buf2, 5, 6, 5).toString()); 726// Output: 1 727``` 728 729### copy 730 731copy(target: Buffer| Uint8Array, targetStart?: number, sourceStart?: number, sourceEnd?: number): number 732 733Copies data at the specified position in this **Buffer** object to the specified position in another **Buffer** object. 734 735**Atomic service API**: This API can be used in atomic services since API version 11. 736 737**System capability**: SystemCapability.Utils.Lang 738 739**Parameters** 740 741| Name| Type| Mandatory| Description| 742| -------- | -------- | -------- | -------- | 743| target | [Buffer](#buffer) \| Uint8Array | Yes| **Buffer** or **Uint8Array** object to which data is copied.| 744| targetStart | number | No| Offset to the start position in the target object where data is copied. The default value is **0**.| 745| sourceStart | number | No| Offset to the start position in this **Buffer** object where data is copied. The default value is **0**.| 746| sourceEnd | number | No| Offset to the end position in this **Buffer** object (not inclusive). The default value is the length of this **Buffer** object.| 747 748**Return value** 749 750| Type| Description| 751| -------- | -------- | 752| number | Total length of the data copied, in bytes.| 753 754**Error codes** 755 756For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 757 758| ID| Error Message| 759| -------- | -------- | 760| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 761| 10200001 | The value of "[targetStart/sourceStart/sourceEnd]" is out of range. | 762 763**Example** 764 765```ts 766import { buffer } from '@kit.ArkTS'; 767 768let buf1 = buffer.allocUninitializedFromPool(26); 769let buf2 = buffer.allocUninitializedFromPool(26).fill('!'); 770 771for (let i = 0; i < 26; i++) { 772 buf1.writeInt8(i + 97, i); 773} 774 775buf1.copy(buf2, 8, 16, 20); 776console.info(buf2.toString('ascii', 0, 25)); 777// Output: !!!!!!!!qrst!!!!!!!!!!!!! 778``` 779 780### entries 781 782entries(): IterableIterator<[number, number]> 783 784Creates and returns an iterator that contains key-value pairs of this **Buffer** object. 785 786**Atomic service API**: This API can be used in atomic services since API version 11. 787 788**System capability**: SystemCapability.Utils.Lang 789 790**Return value** 791 792| Type| Description| 793| -------- | -------- | 794| IterableIterator<[number, number]> | Iterator that contains the key and value, both of which are of the number type.| 795 796**Example** 797 798```ts 799import { buffer } from '@kit.ArkTS'; 800 801let buf = buffer.from('buffer'); 802let pair = buf.entries(); 803let next: IteratorResult<Object[]> = pair.next(); 804while (!next.done) { 805 console.info("buffer: " + next.value); 806 /* 807 Output: buffer: 0,98 808 buffer: 1,117 809 buffer: 2,102 810 buffer: 3,102 811 buffer: 4,101 812 buffer: 5,114 813 */ 814 next = pair.next(); 815} 816``` 817 818### equals 819 820equals(otherBuffer: Uint8Array | Buffer): boolean 821 822Checks whether this **Buffer** object is the same as another **Buffer** object. 823 824**Atomic service API**: This API can be used in atomic services since API version 11. 825 826**System capability**: SystemCapability.Utils.Lang 827 828**Parameters** 829 830| Name| Type| Mandatory| Description| 831| -------- | -------- | -------- | -------- | 832| otherBuffer | Uint8Array \| [Buffer](#buffer) | Yes| **Buffer** object to compare.| 833 834**Return value** 835 836| Type| Description| 837| -------- | -------- | 838| boolean | Check result. The value **true** is returned if the two objects are the same; otherwise, **false** is returned.| 839 840**Error codes** 841 842For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 843 844| ID| Error Message| 845| -------- | -------- | 846| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 847 848**Example** 849 850```ts 851import { buffer } from '@kit.ArkTS'; 852 853let buf1 = buffer.from('ABC'); 854let buf2 = buffer.from('414243', 'hex'); 855let buf3 = buffer.from('ABCD'); 856 857console.info(buf1.equals(buf2).toString()); 858// Output: true 859console.info(buf1.equals(buf3).toString()); 860// Output: false 861``` 862 863### fill 864 865fill(value: string | Buffer | Uint8Array | number, offset?: number, end?: number, encoding?: BufferEncoding): Buffer 866 867Fills this **Buffer** object at the specified position. By default, data is filled cyclically. 868 869**Atomic service API**: This API can be used in atomic services since API version 11. 870 871**System capability**: SystemCapability.Utils.Lang 872 873**Parameters** 874 875| Name| Type| Mandatory| Description| 876| -------- | -------- | -------- | -------- | 877| value | string \| [Buffer](#buffer) \| Uint8Array \| number | Yes| Value to fill.| 878| offset | number | No| Offset to the start position in this **Buffer** object where data is filled. The default value is **0**.| 879| end | number | No| Offset to the end position in this **Buffer** object (not inclusive). The default value is the length of this **Buffer** object.| 880| encoding | [BufferEncoding](#bufferencoding) | No| Encoding format (valid only when **value** is a string). The default value is **'utf8'**.| 881 882**Return value** 883 884| Type| Description| 885| -------- | -------- | 886| [Buffer](#buffer) | **Buffer** object filled with the specified value.| 887 888**Error codes** 889 890For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 891 892| ID| Error Message| 893| -------- | -------- | 894| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 895| 10200001 | The value of "[offset/end]" is out of range. | 896 897**Example** 898 899```ts 900import { buffer } from '@kit.ArkTS'; 901 902let b = buffer.allocUninitializedFromPool(50).fill('h'); 903console.info(b.toString()); 904// Output: hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh 905``` 906 907 908### includes 909 910includes(value: string | number | Buffer | Uint8Array, byteOffset?: number, encoding?: BufferEncoding): boolean 911 912Checks whether this **Buffer** object contains the specified value. 913 914**Atomic service API**: This API can be used in atomic services since API version 11. 915 916**System capability**: SystemCapability.Utils.Lang 917 918**Parameters** 919 920| Name| Type| Mandatory| Description| 921| -------- | -------- | -------- | -------- | 922| value | string \| number \| [Buffer](#buffer) \| Uint8Array | Yes| Value to match.| 923| 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** object. The default value is **0**.| 924| encoding | [BufferEncoding](#bufferencoding) | No| Encoding format (valid only when **value** is a string). The default value is **'utf8'**.| 925 926**Return value** 927 928| Type| Description| 929| -------- | -------- | 930| boolean | Check result. The value **true** is returned if the object contains the specified value; otherwise, **false** is returned.| 931 932**Error codes** 933 934For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 935 936| ID| Error Message| 937| -------- | -------- | 938| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 939 940**Example** 941 942```ts 943import { buffer } from '@kit.ArkTS'; 944 945let buf = buffer.from('this is a buffer'); 946console.info(buf.includes('this').toString()); 947// Output: true 948console.info(buf.includes('be').toString()); 949// Output: false 950``` 951 952### indexOf 953 954indexOf(value: string | number | Buffer | Uint8Array, byteOffset?: number, encoding?: BufferEncoding): number 955 956Obtains the index of the first occurrence of the specified value in this **Buffer** object. If no match is found, **-1** is returned. 957 958**Atomic service API**: This API can be used in atomic services since API version 11. 959 960**System capability**: SystemCapability.Utils.Lang 961 962**Parameters** 963 964| Name| Type| Mandatory| Description| 965| -------- | -------- | -------- | -------- | 966| value | string \| number \| [Buffer](#buffer) \| Uint8Array | Yes| Value to match.| 967| 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** object. The default value is **0**.| 968| encoding | [BufferEncoding](#bufferencoding) | No| Encoding format (valid only when **value** is a string). The default value is **'utf8'**.| 969 970**Return value** 971 972| Type| Description| 973| -------- | -------- | 974| number | Index obtained.| 975 976**Error codes** 977 978For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 979 980| ID| Error Message| 981| -------- | -------- | 982| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 983 984**Example** 985 986```ts 987import { buffer } from '@kit.ArkTS'; 988 989let buf = buffer.from('this is a buffer'); 990console.info(buf.indexOf('this').toString()); 991// Output: 0 992console.info(buf.indexOf('is').toString()); 993// Output: 2 994``` 995 996### keys 997 998keys(): IterableIterator<number> 999 1000Creates and returns an iterator that contains the keys of this **Buffer** object. 1001 1002**Atomic service API**: This API can be used in atomic services since API version 11. 1003 1004**System capability**: SystemCapability.Utils.Lang 1005 1006**Return value** 1007 1008| Type| Description| 1009| -------- | -------- | 1010| IterableIterator<number> | Iterator created.| 1011 1012**Example** 1013 1014```ts 1015import { buffer } from '@kit.ArkTS'; 1016 1017let buf = buffer.from('buffer'); 1018let numbers = Array.from(buf.keys()); 1019for (const key of numbers) { 1020 console.info(key.toString()); 1021 /* 1022 Output: 0 1023 1 1024 2 1025 3 1026 4 1027 5 1028 */ 1029} 1030``` 1031 1032### lastIndexOf 1033 1034lastIndexOf(value: string | number | Buffer | Uint8Array, byteOffset?: number, encoding?: BufferEncoding): number 1035 1036Obtains the index of the last occurrence of the specified value in this **Buffer** object. If no match is found, **-1** is returned. 1037 1038**Atomic service API**: This API can be used in atomic services since API version 11. 1039 1040**System capability**: SystemCapability.Utils.Lang 1041 1042**Parameters** 1043 1044| Name| Type| Mandatory| Description| 1045| -------- | -------- | -------- | -------- | 1046| value | string \| number \| [Buffer](#buffer) \| Uint8Array | Yes| Value to match.| 1047| 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** object. The default value is the length of this **Buffer** object.| 1048| encoding | [BufferEncoding](#bufferencoding) | No| Encoding format (valid only when **value** is a string). The default value is **'utf8'**.| 1049 1050**Return value** 1051 1052| Type| Description| 1053| -------- | -------- | 1054| number | Index obtained.| 1055 1056**Error codes** 1057 1058For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1059 1060| ID| Error Message| 1061| -------- | -------- | 1062| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1063 1064**Example** 1065 1066```ts 1067import { buffer } from '@kit.ArkTS'; 1068 1069let buf = buffer.from('this buffer is a buffer'); 1070console.info(buf.lastIndexOf('this').toString()); 1071// Output: 0 1072console.info(buf.lastIndexOf('buffer').toString()); 1073// Output: 17 1074``` 1075 1076 1077### readBigInt64BE 1078 1079readBigInt64BE(offset?: number): bigint 1080 1081Reads a 64-bit, big-endian, signed big integer from this **Buffer** object at the specified offset. 1082 1083**Atomic service API**: This API can be used in atomic services since API version 11. 1084 1085**System capability**: SystemCapability.Utils.Lang 1086 1087**Parameters** 1088 1089| Name| Type| Mandatory| Description| 1090| -------- | -------- | -------- | -------- | 1091| offset | number | No| Number of bytes to skip before starting to read data. The default value is **0**. Value range: 0 <= offset <= Buffer.length - 8| 1092 1093**Return value** 1094 1095| Type| Description| 1096| -------- | -------- | 1097| bigint | Data read.| 1098 1099**Error codes** 1100 1101For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 1102 1103| ID| Error Message| 1104| -------- | -------- | 1105| 401 | Parameter error. Possible causes: 1.Incorrect parameter types. | 1106| 10200001 | The value of "offset" is out of range. It must be >= 0 and <= buf.length - 8. Received value is: [offset]. | 1107 1108**Example** 1109 1110```ts 1111import { buffer } from '@kit.ArkTS'; 1112 1113let buf = buffer.from([0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x70, 1114 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78]); 1115console.info(buf.readBigInt64BE(0).toString()); 1116// Output: 7161960797921896816 1117 1118let buf1 = buffer.allocUninitializedFromPool(8); 1119let result = buf1.writeBigInt64BE(BigInt(0x0102030405060708), 0); 1120console.info("result = " + result); 1121// Output: result = 8 1122``` 1123 1124### readBigInt64LE 1125 1126readBigInt64LE(offset?: number): bigint 1127 1128Reads a 64-bit, little-endian, signed big integer from this **Buffer** object at the specified offset. 1129 1130**Atomic service API**: This API can be used in atomic services since API version 11. 1131 1132**System capability**: SystemCapability.Utils.Lang 1133 1134**Parameters** 1135 1136| Name| Type| Mandatory| Description| 1137| -------- | -------- | -------- | -------- | 1138| offset | number | No| Number of bytes to skip before starting to read data. The default value is **0**. Value range: 0 <= offset <= Buffer.length - 8| 1139 1140**Return value** 1141 1142| Type| Description| 1143| -------- | -------- | 1144| bigint | Data read.| 1145 1146**Error codes** 1147 1148For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 1149 1150| ID| Error Message| 1151| -------- | -------- | 1152| 401 | Parameter error. Possible causes: 1.Incorrect parameter types. | 1153| 10200001 | The value of "offset" is out of range. It must be >= 0 and <= buf.length - 8. Received value is: [offset]. | 1154 1155**Example** 1156 1157```ts 1158import { buffer } from '@kit.ArkTS'; 1159 1160let buf = buffer.from([0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x70, 1161 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78]); 1162console.info(buf.readBigUInt64BE(0).toString()); 1163// Output: 7161960797921896816 1164 1165let buf1 = buffer.allocUninitializedFromPool(8); 1166let result = buf1.writeBigUInt64BE(BigInt(0xdecafafecacefade), 0); 1167console.info("result = " + result); 1168// Output: result = 8 1169``` 1170 1171### readBigUInt64BE 1172 1173readBigUInt64BE(offset?: number): bigint 1174 1175Reads a 64-bit, big-endian, unsigned big integer from this **Buffer** object at the specified offset. 1176 1177**Atomic service API**: This API can be used in atomic services since API version 11. 1178 1179**System capability**: SystemCapability.Utils.Lang 1180 1181**Parameters** 1182 1183| Name| Type| Mandatory| Description| 1184| -------- | -------- | -------- | -------- | 1185| offset | number | No| Number of bytes to skip before starting to read data. The default value is **0**. Value range: 0 <= offset <= Buffer.length - 8| 1186 1187**Return value** 1188 1189| Type| Description| 1190| -------- | -------- | 1191| bigint | Data read.| 1192 1193**Error codes** 1194 1195For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 1196 1197| ID| Error Message| 1198| -------- | -------- | 1199| 401 | Parameter error. Possible causes: 1.Incorrect parameter types. | 1200| 10200001 | The value of "offset" is out of range. It must be >= 0 and <= buf.length - 8. Received value is: [offset]. | 1201 1202**Example** 1203 1204```ts 1205import { buffer } from '@kit.ArkTS'; 1206 1207let buf = buffer.from([0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x70, 1208 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78]); 1209console.info(buf.readBigUInt64BE(0).toString()); 1210// Output: 7161960797921896816 1211let buf1 = buffer.allocUninitializedFromPool(8); 1212let result = buf1.writeBigUInt64BE(BigInt(0xdecafafecacefade), 0); 1213console.info("result = " + result); 1214// Output: result = 8 1215``` 1216 1217### readBigUInt64LE 1218 1219readBigUInt64LE(offset?: number): bigint 1220 1221Reads a 64-bit, little-endian, unsigned big integer from this **Buffer** object at the specified offset. 1222 1223**Atomic service API**: This API can be used in atomic services since API version 11. 1224 1225**System capability**: SystemCapability.Utils.Lang 1226 1227**Parameters** 1228 1229| Name| Type| Mandatory| Description| 1230| -------- | -------- | -------- | -------- | 1231| offset | number | No| Number of bytes to skip before starting to read data. The default value is **0**. Value range: 0 <= offset <= Buffer.length - 8| 1232 1233**Return value** 1234 1235| Type| Description| 1236| -------- | -------- | 1237| bigint | Data read.| 1238 1239**Error codes** 1240 1241For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 1242 1243| ID| Error Message| 1244| -------- | -------- | 1245| 401 | Parameter error. Possible causes: 1.Incorrect parameter types. | 1246| 10200001 | The value of "offset" is out of range. It must be >= 0 and <= buf.length - 8. Received value is: [offset]. | 1247 1248**Example** 1249 1250```ts 1251import { buffer } from '@kit.ArkTS'; 1252 1253let buf = buffer.from([0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x70, 1254 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78]); 1255console.info(buf.readBigUInt64LE(0).toString()); 1256// Output: 8100120198111388771 1257 1258let buf1 = buffer.allocUninitializedFromPool(8); 1259let result = buf1.writeBigUInt64BE(BigInt(0xdecafafecacefade), 0); 1260console.info("result = " + result); 1261// Output: result = 8 1262``` 1263 1264### readDoubleBE 1265 1266readDoubleBE(offset?: number): number 1267 1268Reads a 64-bit, big-endian, double-precision floating-point number from this **Buffer** object at the specified offset. 1269 1270**Atomic service API**: This API can be used in atomic services since API version 11. 1271 1272**System capability**: SystemCapability.Utils.Lang 1273 1274**Parameters** 1275 1276| Name| Type| Mandatory| Description| 1277| -------- | -------- | -------- | -------- | 1278| offset | number | No| Number of bytes to skip before starting to read data. The default value is **0**. Value range: 0 <= offset <= Buffer.length - 8| 1279 1280**Return value** 1281 1282| Type| Description| 1283| -------- | -------- | 1284| number | Data read.| 1285 1286**Error codes** 1287 1288For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 1289 1290| ID| Error Message| 1291| -------- | -------- | 1292| 401 | Parameter error. Possible causes: 1.Incorrect parameter types. | 1293| 10200001 | The value of "offset" is out of range. It must be >= 0 and <= buf.length - 8. Received value is: [offset]. | 1294 1295**Example** 1296 1297```ts 1298import { buffer } from '@kit.ArkTS'; 1299 1300let buf = buffer.from([1, 2, 3, 4, 5, 6, 7, 8]); 1301console.info(buf.readDoubleBE(0).toString()); 1302// Output: 8.20788039913184e-304 1303let buf1 = buffer.allocUninitializedFromPool(8); 1304let result = buf1.writeDoubleBE(123.456, 0); 1305console.info("result = " + result); 1306// Output: result = 8 1307``` 1308 1309### readDoubleLE 1310 1311readDoubleLE(offset?: number): number 1312 1313Reads a 64-bit, little-endian, double-precision floating-point number from this **Buffer** object at the specified offset. 1314 1315**Atomic service API**: This API can be used in atomic services since API version 11. 1316 1317**System capability**: SystemCapability.Utils.Lang 1318 1319**Parameters** 1320 1321| Name| Type| Mandatory| Description| 1322| -------- | -------- | -------- | -------- | 1323| offset | number | No| Number of bytes to skip before starting to read data. The default value is **0**. Value range: 0 <= offset <= Buffer.length - 8| 1324 1325**Return value** 1326 1327| Type| Description| 1328| -------- | -------- | 1329| number | Data read.| 1330 1331**Error codes** 1332 1333For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 1334 1335| ID| Error Message| 1336| -------- | -------- | 1337| 401 | Parameter error. Possible causes: 1.Incorrect parameter types. | 1338| 10200001 | The value of "offset" is out of range. It must be >= 0 and <= buf.length - 8. Received value is: [offset]. | 1339 1340**Example** 1341 1342```ts 1343import { buffer } from '@kit.ArkTS'; 1344 1345let buf = buffer.from([1, 2, 3, 4, 5, 6, 7, 8]); 1346console.info(buf.readDoubleLE(0).toString()); 1347// Output: 5.447603722011605e-270 1348let buf1 = buffer.allocUninitializedFromPool(8); 1349let result = buf1.writeDoubleLE(123.456, 0); 1350console.info("result = " + result); 1351// Output: result = 8 1352``` 1353 1354### readFloatBE 1355 1356readFloatBE(offset?: number): number 1357 1358Reads a 32-bit, big-endian, single-precision floating-point number from this **Buffer** object at the specified offset. 1359 1360**Atomic service API**: This API can be used in atomic services since API version 11. 1361 1362**System capability**: SystemCapability.Utils.Lang 1363 1364**Parameters** 1365 1366| Name| Type| Mandatory| Description| 1367| -------- | -------- | -------- | -------- | 1368| offset | number | No| Number of bytes to skip before starting to read data. The default value is **0**. Value range: 0 <= offset <= Buffer.length - 4| 1369 1370**Return value** 1371 1372| Type| Description| 1373| -------- | -------- | 1374| number | Data read.| 1375 1376**Error codes** 1377 1378For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 1379 1380| ID| Error Message| 1381| -------- | -------- | 1382| 401 | Parameter error. Possible causes: 1.Incorrect parameter types. | 1383| 10200001 | The value of "offset" is out of range. It must be >= 0 and <= buf.length - 4. Received value is: [offset]. | 1384 1385**Example** 1386 1387```ts 1388import { buffer } from '@kit.ArkTS'; 1389 1390let buf = buffer.from([1, 2, 3, 4, 5, 6, 7, 8]); 1391console.info(buf.readFloatBE(0).toString()); 1392// Output: 2.387939260590663e-38 1393let buf1 = buffer.allocUninitializedFromPool(4); 1394let result = buf1.writeFloatBE(0xcabcbcbc, 0); 1395console.info("result = " + result); 1396// Output: result = 4 1397``` 1398 1399### readFloatLE 1400 1401readFloatLE(offset?: number): number 1402 1403Reads a 32-bit, little-endian, single-precision floating-point number from this **Buffer** object at the specified offset. 1404 1405**Atomic service API**: This API can be used in atomic services since API version 11. 1406 1407**System capability**: SystemCapability.Utils.Lang 1408 1409**Parameters** 1410 1411| Name| Type| Mandatory| Description| 1412| -------- | -------- | -------- | -------- | 1413| offset | number | No| Number of bytes to skip before starting to read data. The default value is **0**. Value range: 0 <= offset <= Buffer.length - 4| 1414 1415**Return value** 1416 1417| Type| Description| 1418| -------- | -------- | 1419| number | Data read.| 1420 1421**Error codes** 1422 1423For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 1424 1425| ID| Error Message| 1426| -------- | -------- | 1427| 401 | Parameter error. Possible causes: 1.Incorrect parameter types. | 1428| 10200001 | The value of "offset" is out of range. It must be >= 0 and <= buf.length - 4. Received value is: [offset]. | 1429 1430**Example** 1431 1432```ts 1433import { buffer } from '@kit.ArkTS'; 1434 1435let buf = buffer.from([1, 2, 3, 4, 5, 6, 7, 8]); 1436console.info(buf.readFloatLE(0).toString()); 1437// Output: 1.539989614439558e-36 1438let buf1 = buffer.allocUninitializedFromPool(4); 1439let result = buf1.writeFloatLE(0xcabcbcbc, 0); 1440console.info("result = " + result); 1441// Output: result = 4 1442``` 1443 1444### readInt8 1445 1446readInt8(offset?: number): number 1447 1448Reads an 8-bit signed integer from this **Buffer** object at the specified offset. 1449 1450**Atomic service API**: This API can be used in atomic services since API version 11. 1451 1452**System capability**: SystemCapability.Utils.Lang 1453 1454**Parameters** 1455 1456| Name| Type| Mandatory| Description| 1457| -------- | -------- | -------- | -------- | 1458| offset | number | No| Number of bytes to skip before starting to read data. The default value is **0**. Value range: 0 <= offset <= Buffer.length - 1| 1459 1460**Return value** 1461 1462| Type| Description| 1463| -------- | -------- | 1464| number | Data read.| 1465 1466**Error codes** 1467 1468For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 1469 1470| ID| Error Message| 1471| -------- | -------- | 1472| 401 | Parameter error. Possible causes: 1.Incorrect parameter types. | 1473| 10200001 | The value of "offset" is out of range. It must be >= 0 and <= buf.length - 1. Received value is: [offset]. | 1474 1475**Example** 1476 1477```ts 1478import { buffer } from '@kit.ArkTS'; 1479 1480let buf = buffer.from([-1, 5]); 1481console.info(buf.readInt8(0).toString()); 1482// Output: 0 1483console.info(buf.readInt8(1).toString()); 1484// Output: 5 1485let buf1 = buffer.allocUninitializedFromPool(2); 1486let result = buf1.writeInt8(0x12); 1487console.info("result = " + result); 1488// Output: result = 1 1489``` 1490 1491### readInt16BE 1492 1493readInt16BE(offset?: number): number 1494 1495Reads a 16-bit, big-endian, signed integer from this **Buffer** object at the specified offset. 1496 1497**Atomic service API**: This API can be used in atomic services since API version 11. 1498 1499**System capability**: SystemCapability.Utils.Lang 1500 1501**Parameters** 1502 1503| Name| Type| Mandatory| Description| 1504| -------- | -------- | -------- | -------- | 1505| offset | number | No| Number of bytes to skip before starting to read data. The default value is **0**. Value range: 0 <= offset <= Buffer.length - 2| 1506 1507**Return value** 1508 1509| Type| Description| 1510| -------- | -------- | 1511| number | Data read.| 1512 1513**Error codes** 1514 1515For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 1516 1517| ID| Error Message| 1518| -------- | -------- | 1519| 401 | Parameter error. Possible causes: 1.Incorrect parameter types. | 1520| 10200001 | The value of "offset" is out of range. It must be >= 0 and <= buf.length - 2. Received value is: [offset]. | 1521 1522**Example** 1523 1524```ts 1525import { buffer } from '@kit.ArkTS'; 1526 1527let buf = buffer.from([0, 5]); 1528console.info(buf.readInt16BE(0).toString()); 1529// Output: 5 1530let buf1 = buffer.alloc(2); 1531let result = buf1.writeInt16BE(0x1234, 0); 1532console.info("result = " + result); 1533// Output: result = 2 1534``` 1535 1536### readInt16LE 1537 1538readInt16LE(offset?: number): number 1539 1540Reads a 16-bit, little-endian, signed integer from this **Buffer** object at the specified offset. 1541 1542**Atomic service API**: This API can be used in atomic services since API version 11. 1543 1544**System capability**: SystemCapability.Utils.Lang 1545 1546**Parameters** 1547 1548| Name| Type| Mandatory| Description| 1549| -------- | -------- | -------- | -------- | 1550| offset | number | No| Number of bytes to skip before starting to read data. The default value is **0**. Value range: 0 <= offset <= Buffer.length - 2| 1551 1552**Return value** 1553 1554| Type| Description| 1555| -------- | -------- | 1556| number | Data read.| 1557 1558**Error codes** 1559 1560For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 1561 1562| ID| Error Message| 1563| -------- | -------- | 1564| 401 | Parameter error. Possible causes: 1.Incorrect parameter types. | 1565| 10200001 | The value of "offset" is out of range. It must be >= 0 and <= buf.length - 2. Received value is: [offset]. | 1566 1567**Example** 1568 1569```ts 1570import { buffer } from '@kit.ArkTS'; 1571 1572let buf = buffer.from([0, 5]); 1573console.info(buf.readInt16LE(0).toString()); 1574// Output: 1280 1575let buf1 = buffer.alloc(2); 1576let result = buf1.writeInt16BE(0x1234, 0); 1577console.info("result = " + result); 1578// Output: result = 2 1579``` 1580 1581### readInt32BE 1582 1583readInt32BE(offset?: number): number 1584 1585Reads a 32-bit, big-endian, signed integer from this **Buffer** object at the specified offset. 1586 1587**Atomic service API**: This API can be used in atomic services since API version 11. 1588 1589**System capability**: SystemCapability.Utils.Lang 1590 1591**Parameters** 1592 1593| Name| Type| Mandatory| Description| 1594| -------- | -------- | -------- | -------- | 1595| offset | number | No| Number of bytes to skip before starting to read data. The default value is **0**. Value range: 0 <= offset <= Buffer.length - 4| 1596 1597**Return value** 1598 1599| Type| Description| 1600| -------- | -------- | 1601| number | Data read.| 1602 1603**Error codes** 1604 1605For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 1606 1607| ID| Error Message| 1608| -------- | -------- | 1609| 401 | Parameter error. Possible causes: 1.Incorrect parameter types. | 1610| 10200001 | The value of "offset" is out of range. It must be >= 0 and <= buf.length - 4. Received value is: [offset]. | 1611 1612**Example** 1613 1614```ts 1615import { buffer } from '@kit.ArkTS'; 1616 1617let buf = buffer.from([0, 0, 0, 5]); 1618console.info(buf.readInt32BE(0).toString()); 1619// Output: 5 1620let buf1 = buffer.alloc(4); 1621let result = buf1.writeInt32BE(0x12345678, 0); 1622console.info("result = " + result); 1623// Output: result = 4 1624``` 1625 1626### readInt32LE 1627 1628readInt32LE(offset?: number): number 1629 1630Reads a 32-bit, little-endian, signed integer from this **Buffer** object at the specified offset. 1631 1632**Atomic service API**: This API can be used in atomic services since API version 11. 1633 1634**System capability**: SystemCapability.Utils.Lang 1635 1636**Parameters** 1637 1638| Name| Type| Mandatory| Description| 1639| -------- | -------- | -------- | -------- | 1640| offset | number | No| Number of bytes to skip before starting to read data. The default value is **0**. Value range: 0 <= offset <= Buffer.length - 4| 1641 1642**Return value** 1643 1644| Type| Description| 1645| -------- | -------- | 1646| number | Data read.| 1647 1648**Error codes** 1649 1650For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 1651 1652| ID| Error Message| 1653| -------- | -------- | 1654| 401 | Parameter error. Possible causes: 1.Incorrect parameter types. | 1655| 10200001 | The value of "offset" is out of range. It must be >= 0 and <= buf.length - 4. Received value is: [offset]. | 1656 1657**Example** 1658 1659```ts 1660import { buffer } from '@kit.ArkTS'; 1661 1662let buf = buffer.from([0, 0, 0, 5]); 1663console.info(buf.readInt32LE(0).toString()); 1664// Output: 83886080 1665let buf1 = buffer.alloc(4); 1666let result = buf1.writeInt32BE(0x12345678, 0); 1667console.info("result = " + result); 1668// Output: result = 4 1669``` 1670 1671### readIntBE 1672 1673readIntBE(offset: number, byteLength: number): number 1674 1675Reads the specified number of bytes from this **Buffer** object 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. 1676 1677**Atomic service API**: This API can be used in atomic services since API version 11. 1678 1679**System capability**: SystemCapability.Utils.Lang 1680 1681**Parameters** 1682 1683| Name| Type| Mandatory| Description| 1684| -------- | -------- | -------- | -------- | 1685| offset | number | Yes| Number of bytes to skip before starting to read data. The default value is **0**. Value range: 0 <= offset <= Buffer.length - byteLength| 1686| byteLength | number | Yes| Number of bytes to read. Value range: 1 <= byteLength <= 6| 1687 1688 1689**Return value** 1690 1691| Type| Description| 1692| -------- | -------- | 1693| number | Data read. If the offset is a decimal, undefined is returned.| 1694 1695**Error codes** 1696 1697For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 1698 1699| ID| Error Message| 1700| -------- | -------- | 1701| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1702| 10200001 | The value of "[param]" is out of range. It must be >= [left range] and <= [right range]. Received value is: [param] | 1703 1704**Example** 1705 1706```ts 1707import { buffer } from '@kit.ArkTS'; 1708 1709let buf = buffer.from("ab"); 1710let num = buf.readIntBE(0, 1); 1711console.info(num.toString()); 1712// Output: 97 1713let buf1 = buffer.allocUninitializedFromPool(6); 1714let result = buf1.writeIntBE(0x123456789011, 0, 6); 1715console.info("result = " + result); 1716// Output: result = 6 1717``` 1718 1719 1720### readIntLE 1721 1722readIntLE(offset: number, byteLength: number): number 1723 1724Reads the specified number of bytes from this **Buffer** object 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. 1725 1726**Atomic service API**: This API can be used in atomic services since API version 11. 1727 1728**System capability**: SystemCapability.Utils.Lang 1729 1730**Parameters** 1731 1732| Name| Type| Mandatory| Description| 1733| -------- | -------- | -------- | -------- | 1734| offset | number | Yes| Number of bytes to skip before starting to read data. The default value is **0**. Value range: 0 <= offset <= Buffer.length - byteLength| 1735| byteLength | number | Yes| Number of bytes to read. Value range: 1 <= byteLength <= 6| 1736 1737 1738**Return value** 1739 1740| Type| Description| 1741| -------- | -------- | 1742| number | Data read. If the offset is a decimal, undefined is returned.| 1743 1744**Error codes** 1745 1746For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 1747 1748| ID| Error Message| 1749| -------- | -------- | 1750| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1751| 10200001 | The value of "[param]" is out of range. It must be >= [left range] and <= [right range]. Received value is: [param] | 1752 1753**Example** 1754 1755```ts 1756import { buffer } from '@kit.ArkTS'; 1757 1758let buf = buffer.from([0x12, 0x34, 0x56, 0x78, 0x90, 0xab]); 1759console.info(buf.readIntLE(0, 6).toString(16)); 1760// Output: -546f87a9cbee 1761let buf1 = buffer.allocUninitializedFromPool(6); 1762let result = buf1.writeIntLE(0x123456789011, 0, 6); 1763console.info("result = " + result); 1764// Output: result = 6 1765``` 1766 1767### readUInt8 1768 1769readUInt8(offset?: number): number 1770 1771Reads an 8-bit unsigned integer from this **Buffer** object at the specified offset. 1772 1773**Atomic service API**: This API can be used in atomic services since API version 11. 1774 1775**System capability**: SystemCapability.Utils.Lang 1776 1777**Parameters** 1778 1779| Name| Type| Mandatory| Description| 1780| -------- | -------- | -------- | -------- | 1781| offset | number | No| Number of bytes to skip before starting to read data. The default value is **0**. Value range: 0 <= offset <= Buffer.length - 1| 1782 1783 1784**Return value** 1785 1786| Type| Description| 1787| -------- | -------- | 1788| number | Data read.| 1789 1790**Error codes** 1791 1792For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 1793 1794| ID| Error Message| 1795| -------- | -------- | 1796| 401 | Parameter error. Possible causes: 1. Incorrect parameter types. | 1797| 10200001 | The value of "offset" is out of range. It must be >= 0 and <= buf.length - 1. Received value is: [offset]. | 1798 1799**Example** 1800 1801```ts 1802import { buffer } from '@kit.ArkTS'; 1803 1804let buf = buffer.from([1, -2]); 1805console.info(buf.readUInt8(0).toString()); 1806// Output: 1 1807console.info(buf.readUInt8(1).toString()); 1808// Output: 0 1809let buf1 = buffer.allocUninitializedFromPool(4); 1810let result = buf1.writeUInt8(0x42); 1811console.info("result = " + result); 1812// Output: result = 1 1813``` 1814 1815### readUInt16BE 1816 1817readUInt16BE(offset?: number): number 1818 1819Reads a 16-bit, big-endian, unsigned integer from this **Buffer** object at the specified offset. 1820 1821**System capability**: SystemCapability.Utils.Lang 1822 1823**Atomic service API**: This API can be used in atomic services since API version 11. 1824 1825**Parameters** 1826 1827| Name| Type| Mandatory| Description| 1828| -------- | -------- | -------- | -------- | 1829| offset | number | No| Number of bytes to skip before starting to read data. The default value is **0**. Value range: 0 <= offset <= Buffer.length - 2| 1830 1831 1832**Return value** 1833 1834| Type| Description| 1835| -------- | -------- | 1836| number | Data read.| 1837 1838**Error codes** 1839 1840For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 1841 1842| ID| Error Message| 1843| -------- | -------- | 1844| 401 | Parameter error. Possible causes: 1. Incorrect parameter types. | 1845| 10200001 | The value of "offset" is out of range. It must be >= 0 and <= buf.length - 2. Received value is: [offset]. | 1846 1847**Example** 1848 1849```ts 1850import { buffer } from '@kit.ArkTS'; 1851 1852let buf = buffer.from([0x12, 0x34, 0x56]); 1853console.info(buf.readUInt16BE(0).toString(16)); 1854// Output: 1234 1855console.info(buf.readUInt16BE(1).toString(16)); 1856// Output: 3456 1857let buf1 = buffer.allocUninitializedFromPool(4); 1858let result = buf1.writeUInt16BE(0x1234, 0); 1859console.info("result = " + result); 1860// Output: result = 2 1861``` 1862 1863### readUInt16LE 1864 1865readUInt16LE(offset?: number): number 1866 1867Reads a 16-bit, little-endian, unsigned integer from this **Buffer** object at the specified offset. 1868 1869**Atomic service API**: This API can be used in atomic services since API version 11. 1870 1871**System capability**: SystemCapability.Utils.Lang 1872 1873**Parameters** 1874 1875| Name| Type| Mandatory| Description| 1876| -------- | -------- | -------- | -------- | 1877| offset | number | No| Number of bytes to skip before starting to read data. The default value is **0**. Value range: 0 <= offset <= Buffer.length - 2| 1878 1879 1880**Return value** 1881 1882| Type| Description| 1883| -------- | -------- | 1884| number | Data read.| 1885 1886**Error codes** 1887 1888For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 1889 1890| ID| Error Message| 1891| -------- | -------- | 1892| 401 | Parameter error. Possible causes: 1. Incorrect parameter types. | 1893| 10200001 | The value of "offset" is out of range. It must be >= 0 and <= buf.length - 2. Received value is: [offset]. | 1894 1895**Example** 1896 1897```ts 1898import { buffer } from '@kit.ArkTS'; 1899 1900let buf = buffer.from([0x12, 0x34, 0x56]); 1901console.info(buf.readUInt16LE(0).toString(16)); 1902// Output: 3412 1903console.info(buf.readUInt16LE(1).toString(16)); 1904// Output: 5634 1905let buf1 = buffer.allocUninitializedFromPool(4); 1906let result = buf1.writeUInt16LE(0x1234, 0); 1907console.info("result = " + result); 1908// Output: result = 2 1909``` 1910 1911### readUInt32BE 1912 1913readUInt32BE(offset?: number): number 1914 1915Reads a 32-bit, big-endian, unsigned integer from this **Buffer** object at the specified offset. 1916 1917**Atomic service API**: This API can be used in atomic services since API version 11. 1918 1919**System capability**: SystemCapability.Utils.Lang 1920 1921**Parameters** 1922 1923| Name| Type| Mandatory| Description| 1924| -------- | -------- | -------- | -------- | 1925| offset | number | No| Number of bytes to skip before starting to read data. The default value is **0**. Value range: 0 <= offset <= Buffer.length - 4| 1926 1927 1928**Return value** 1929 1930| Type| Description| 1931| -------- | -------- | 1932| number | Data read.| 1933 1934**Error codes** 1935 1936For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 1937 1938| ID| Error Message| 1939| -------- | -------- | 1940| 401 | Parameter error. Possible causes: 1. Incorrect parameter types. | 1941| 10200001 | The value of "offset" is out of range. It must be >= 0 and <= buf.length - 4. Received value is: [offset]. | 1942 1943**Example** 1944 1945```ts 1946import { buffer } from '@kit.ArkTS'; 1947 1948let buf = buffer.from([0x12, 0x34, 0x56, 0x78]); 1949console.info(buf.readUInt32BE(0).toString(16)); 1950// Output: 12345678 1951let buf1 = buffer.allocUninitializedFromPool(4); 1952let result = buf1.writeUInt32BE(0x12345678, 0); 1953console.info("result = " + result); 1954// Output: result = 4 1955``` 1956 1957### readUInt32LE 1958 1959readUInt32LE(offset?: number): number 1960 1961Reads a 32-bit, little-endian, unsigned integer from this **Buffer** object at the specified offset. 1962 1963**System capability**: SystemCapability.Utils.Lang 1964 1965**Atomic service API**: This API can be used in atomic services since API version 11. 1966 1967**Parameters** 1968 1969| Name| Type| Mandatory| Description| 1970| -------- | -------- | -------- | -------- | 1971| offset | number | No| Number of bytes to skip before starting to read data. The default value is **0**. Value range: 0 <= offset <= Buffer.length - 4| 1972 1973 1974**Return value** 1975 1976| Type| Description| 1977| -------- | -------- | 1978| number | Data read.| 1979 1980**Error codes** 1981 1982For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 1983 1984| ID| Error Message| 1985| -------- | -------- | 1986| 401 | Parameter error. Possible causes: 1. Incorrect parameter types. | 1987| 10200001 | The value of "offset" is out of range. It must be >= 0 and <= buf.length - 4. Received value is: [offset]. | 1988 1989**Example** 1990 1991```ts 1992import { buffer } from '@kit.ArkTS'; 1993 1994let buf = buffer.from([0x12, 0x34, 0x56, 0x78]); 1995console.info(buf.readUInt32LE(0).toString(16)); 1996// Output: 78563412 1997let buf1 = buffer.allocUninitializedFromPool(4); 1998let result = buf1.writeUInt32LE(0x12345678, 0); 1999console.info("result = " + result); 2000// Output: result = 4 2001``` 2002 2003### readUIntBE 2004 2005readUIntBE(offset: number, byteLength: number): number 2006 2007Reads the specified number of bytes from this **Buffer** object at the specified offset, and interprets the result as an unsigned, big-endian integer that supports up to 48 bits of precision. 2008 2009**Atomic service API**: This API can be used in atomic services since API version 11. 2010 2011**System capability**: SystemCapability.Utils.Lang 2012 2013**Parameters** 2014 2015| Name| Type| Mandatory| Description| 2016| -------- | -------- | -------- | -------- | 2017| offset | number | Yes| Number of bytes to skip before starting to read data. The default value is **0**. Value range: 0 <= offset <= Buffer.length - byteLength| 2018| byteLength | number | Yes| Number of bytes to read. Value range: 1 <= byteLength <= 6| 2019 2020 2021**Return value** 2022 2023| Type| Description| 2024| -------- | -------- | 2025| number | Data read. If the offset is a decimal, undefined is returned.| 2026 2027**Error codes** 2028 2029For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 2030 2031| ID| Error Message| 2032| -------- | -------- | 2033| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2034| 10200001 | The value of "[param]" is out of range. It must be >= [left range] and <= [right range]. Received value is: [param] | 2035 2036**Example** 2037 2038```ts 2039import { buffer } from '@kit.ArkTS'; 2040 2041let buf = buffer.from([0x12, 0x34, 0x56, 0x78, 0x90, 0xab]); 2042console.info(buf.readUIntBE(0, 6).toString(16)); 2043// Output: 1234567890ab 2044let buf1 = buffer.allocUninitializedFromPool(4); 2045let result = buf1.writeUIntBE(0x13141516, 0, 4); 2046console.info("result = " + result); 2047// Output: result = 4 2048``` 2049 2050### readUIntLE 2051 2052readUIntLE(offset: number, byteLength: number): number 2053 2054Reads the specified number of bytes from this **Buffer** object at the specified offset, and interprets the result as an unsigned, little-endian integer that supports up to 48 bits of precision. 2055 2056**Atomic service API**: This API can be used in atomic services since API version 11. 2057 2058**System capability**: SystemCapability.Utils.Lang 2059 2060**Parameters** 2061 2062| Name| Type| Mandatory| Description| 2063| -------- | -------- | -------- | -------- | 2064| offset | number | Yes| Number of bytes to skip before starting to read data. The default value is **0**. Value range: 0 <= offset <= Buffer.length - byteLength| 2065| byteLength | number | Yes| Number of bytes to read. Value range: 1 <= byteLength <= 6| 2066 2067 2068**Return value** 2069 2070| Type| Description| 2071| -------- | -------- | 2072| number | Data read. If the offset is a decimal, undefined is returned.| 2073 2074**Error codes** 2075 2076For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 2077 2078| ID| Error Message| 2079| -------- | -------- | 2080| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2081| 10200001 | The value of "[param]" is out of range. It must be >= [left range] and <= [right range]. Received value is: [param] | 2082 2083**Example** 2084 2085```ts 2086import { buffer } from '@kit.ArkTS'; 2087 2088let buf = buffer.from([0x12, 0x34, 0x56, 0x78, 0x90, 0xab]); 2089console.info(buf.readUIntLE(0, 6).toString(16)); 2090// Output: ab9078563412 2091let buf1 = buffer.allocUninitializedFromPool(4); 2092let result = buf1.writeUIntLE(0x13141516, 0, 4); 2093console.info("result = " + result); 2094// Output: result = 4 2095``` 2096 2097### subarray 2098 2099subarray(start?: number, end?: number): Buffer 2100 2101Truncates this **Buffer** object from the specified position to create a new **Buffer** object. 2102 2103**System capability**: SystemCapability.Utils.Lang 2104 2105**Atomic service API**: This API can be used in atomic services since API version 11. 2106 2107**Parameters** 2108 2109| Name| Type| Mandatory| Description| 2110| -------- | -------- | -------- | -------- | 2111| start | number | No| Offset to the start position in this **Buffer** object where data is truncated. The default value is **0**.| 2112| end | number | No| Offset to the end position in this **Buffer** object (not inclusive). The default value is the length of this **Buffer** object.| 2113 2114**Return value** 2115 2116| Type| Description| 2117| -------- | -------- | 2118| [Buffer](#buffer) | **Buffer** object created. When the value of **start** or **end** is less than **0**, an empty buffer is returned.| 2119 2120**Example** 2121 2122```ts 2123import { buffer } from '@kit.ArkTS'; 2124 2125let buf1 = buffer.allocUninitializedFromPool(26); 2126 2127for (let i = 0; i < 26; i++) { 2128 buf1.writeInt8(i + 97, i); 2129} 2130const buf2 = buf1.subarray(0, 3); 2131console.info(buf2.toString('ascii', 0, buf2.length)); 2132// Output: abc 2133``` 2134 2135### swap16 2136 2137swap16(): Buffer 2138 2139Converts this **Buffer** object into an array of unsigned 16-bit integers and swaps the byte order in place. 2140 2141**Atomic service API**: This API can be used in atomic services since API version 11. 2142 2143**System capability**: SystemCapability.Utils.Lang 2144 2145 2146**Return value** 2147 2148| Type| Description| 2149| -------- | -------- | 2150| [Buffer](#buffer) | **Buffer** object swapped.| 2151 2152**Error codes** 2153 2154For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 2155 2156| ID| Error Message| 2157| -------- | -------- | 2158| 10200009 | The buffer size must be a multiple of 16-bits. | 2159 2160**Example** 2161 2162```ts 2163import { buffer } from '@kit.ArkTS'; 2164 2165let buf1 = buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]); 2166console.info(buf1.toString('hex')); 2167// Output: 0102030405060708 2168buf1.swap16(); 2169console.info(buf1.toString('hex')); 2170// Output: 0201040306050807 2171``` 2172 2173### swap32 2174 2175swap32(): Buffer 2176 2177Converts this **Buffer** object into an array of unsigned 32-bit integers and swaps the byte order in place. 2178 2179**Atomic service API**: This API can be used in atomic services since API version 11. 2180 2181**System capability**: SystemCapability.Utils.Lang 2182 2183 2184**Return value** 2185 2186| Type| Description| 2187| -------- | -------- | 2188| [Buffer](#buffer) | **Buffer** object swapped.| 2189 2190**Error codes** 2191 2192For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 2193 2194| ID| Error Message| 2195| -------- | -------- | 2196| 10200009 | The buffer size must be a multiple of 32-bits. | 2197 2198**Example** 2199 2200```ts 2201import { buffer } from '@kit.ArkTS'; 2202 2203let buf1 = buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]); 2204console.info(buf1.toString('hex')); 2205// Output: 0102030405060708 2206buf1.swap32(); 2207console.info(buf1.toString('hex')); 2208// Output: 0403020108070605 2209``` 2210 2211### swap64 2212 2213swap64(): Buffer 2214 2215Converts this **Buffer** object into an array of unsigned 64-bit integers and swaps the byte order in place. 2216 2217**Atomic service API**: This API can be used in atomic services since API version 11. 2218 2219**System capability**: SystemCapability.Utils.Lang 2220 2221 2222**Return value** 2223 2224| Type| Description| 2225| -------- | -------- | 2226| [Buffer](#buffer) | **Buffer** object swapped.| 2227 2228**Error codes** 2229 2230For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 2231 2232| ID| Error Message| 2233| -------- | -------- | 2234| 10200009 | The buffer size must be a multiple of 64-bits. | 2235 2236**Example** 2237 2238```ts 2239import { buffer } from '@kit.ArkTS'; 2240 2241let buf1 = buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]); 2242console.info(buf1.toString('hex')); 2243// Output: 0102030405060708 2244buf1.swap64(); 2245console.info(buf1.toString('hex')); 2246// Output: 0807060504030201 2247``` 2248 2249### toJSON 2250 2251toJSON(): Object 2252 2253Converts this **Buffer** object into a JSON object. 2254 2255**Atomic service API**: This API can be used in atomic services since API version 11. 2256 2257**System capability**: SystemCapability.Utils.Lang 2258 2259 2260**Return value** 2261 2262| Type| Description| 2263| -------- | -------- | 2264| Object | JSON object.| 2265 2266**Example** 2267 2268```ts 2269import { buffer } from '@kit.ArkTS'; 2270 2271let buf1 = buffer.from([0x1, 0x2, 0x3, 0x4, 0x5]); 2272let obj = buf1.toJSON(); 2273console.info(JSON.stringify(obj)); 2274// Output: {"type":"Buffer","data":[1,2,3,4,5]} 2275``` 2276 2277### toString 2278 2279toString(encoding?: string, start?: number, end?: number): string 2280 2281Converts the data at the specified position in this **Buffer** object into a string in the specified encoding format. 2282 2283**Atomic service API**: This API can be used in atomic services since API version 11. 2284 2285**System capability**: SystemCapability.Utils.Lang 2286 2287**Parameters** 2288 2289| Name| Type| Mandatory| Description| 2290| -------- | -------- | -------- | -------- | 2291| encoding | string | No| Encoding format (valid only when **value** is a string). The default value is **'utf8'**.| 2292| start | number | No| Offset to the start position of the data to convert. The default value is **0**.| 2293| end | number | No| Offset to the end position of the data to convert. The default value is the length of this **Buffer** object.| 2294 2295**Return value** 2296 2297| Type| Description| 2298| -------- | -------- | 2299| string | String. When the value of **start** is greater than or equal to **Buffer.length** or **start** is greater than **end**, an empty string is returned.| 2300 2301**Error codes** 2302 2303For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 2304 2305| ID| Error Message| 2306| -------- | -------- | 2307| 401 | Parameter error. Possible causes: 1. Incorrect parameter types. | 2308 2309**Example** 2310 2311```ts 2312import { buffer } from '@kit.ArkTS'; 2313 2314let buf1 = buffer.allocUninitializedFromPool(26); 2315for (let i = 0; i < 26; i++) { 2316 buf1.writeInt8(i + 97, i); 2317} 2318console.info(buf1.toString('utf-8')); 2319// Output: abcdefghijklmnopqrstuvwxyz 2320``` 2321 2322### values 2323 2324values(): IterableIterator<number> 2325 2326Creates and returns an iterator that contains the values of this **Buffer** object. 2327 2328**Atomic service API**: This API can be used in atomic services since API version 11. 2329 2330**System capability**: SystemCapability.Utils.Lang 2331 2332**Return value** 2333 2334| Type| Description| 2335| -------- | -------- | 2336| IterableIterator<number> | Iterator.| 2337 2338**Example** 2339 2340```ts 2341import { buffer } from '@kit.ArkTS'; 2342 2343let buf1 = buffer.from('buffer'); 2344let pair = buf1.values(); 2345let next:IteratorResult<number> = pair.next(); 2346while (!next.done) { 2347 console.info(next.value.toString()); 2348 /* 2349 Output: 98 2350 117 2351 102 2352 102 2353 101 2354 114 2355 */ 2356 next = pair.next(); 2357} 2358``` 2359 2360### write 2361 2362write(str: string, offset?: number, length?: number, encoding?: string): number 2363 2364Writes a string of the specified length to this **Buffer** object at the specified position in the given encoding format. 2365 2366**Atomic service API**: This API can be used in atomic services since API version 11. 2367 2368**System capability**: SystemCapability.Utils.Lang 2369 2370**Parameters** 2371 2372| Name| Type| Mandatory| Description| 2373| -------- | -------- | -------- | -------- | 2374| str | string | Yes| String to write.| 2375| offset | number | No| Number of bytes to skip before starting to write data. The default value is **0**.| 2376| length | number | No| Maximum number of bytes to write. The default value is **Buffer.length** minus **offset**.| 2377| encoding | string | No| Encoding format of the string. The default value is **'utf8'**.| 2378 2379 2380**Return value** 2381 2382| Type| Description| 2383| -------- | -------- | 2384| number | Number of bytes written.| 2385 2386**Error codes** 2387 2388For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 2389 2390| ID| Error Message| 2391| -------- | -------- | 2392| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2393| 10200001 | The value of "[offset/length]" is out of range. It must be >= 0 and <= buf.length. Received value is: [offset/length]. | 2394 2395**Example** 2396 2397```ts 2398import { buffer } from '@kit.ArkTS'; 2399 2400let buf = buffer.alloc(256); 2401let len = buf.write('\u00bd + \u00bc = \u00be', 0); 2402console.info(`${len} bytes: ${buf.toString('utf-8', 0, len)}`); 2403// Output: 12 bytes: ½ + ¼ = ¾ 2404 2405let buffer1 = buffer.alloc(10); 2406let length = buffer1.write('abcd', 8); 2407console.info("length = " + length); 2408// Output: length = 2 2409``` 2410 2411### writeBigInt64BE 2412 2413writeBigInt64BE(value: bigint, offset?: number): number 2414 2415Writes a 64-bit, big-endian, signed big integer to this **Buffer** object at the specified offset. 2416 2417**Atomic service API**: This API can be used in atomic services since API version 11. 2418 2419**System capability**: SystemCapability.Utils.Lang 2420 2421**Parameters** 2422 2423| Name| Type| Mandatory| Description| 2424| -------- | -------- | -------- | -------- | 2425| value | bigint | Yes| Data to write.| 2426| offset | number | No| Number of bytes to skip before starting to write data. The default value is **0**. Value range: 0 <= offset <= Buffer.length - 8| 2427 2428 2429**Return value** 2430 2431| Type| Description| 2432| -------- | -------- | 2433| number | Offset plus the number of written bytes.| 2434 2435**Error codes** 2436 2437For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 2438 2439| ID| Error Message| 2440| -------- | -------- | 2441| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2442| 10200001 | The value of "[param]" is out of range. It must be >= [left range] and <= [right range]. Received value is: [param] | 2443 2444**Example** 2445 2446```ts 2447import { buffer } from '@kit.ArkTS'; 2448 2449let buf = buffer.allocUninitializedFromPool(8); 2450let result = buf.writeBigInt64BE(BigInt(0x0102030405060708), 0); 2451console.info("result = " + result); 2452// Output: result = 8 2453``` 2454 2455### writeBigInt64LE 2456 2457writeBigInt64LE(value: bigint, offset?: number): number 2458 2459Writes a 64-bit, little-endian, signed big integer to this **Buffer** object at the specified offset. 2460 2461**Atomic service API**: This API can be used in atomic services since API version 11. 2462 2463**System capability**: SystemCapability.Utils.Lang 2464 2465**Parameters** 2466 2467| Name| Type| Mandatory| Description| 2468| -------- | -------- | -------- | -------- | 2469| value | bigint | Yes| Data to write.| 2470| offset | number | No| Number of bytes to skip before starting to write data. The default value is **0**. Value range: 0 <= offset <= Buffer.length - 8| 2471 2472 2473**Return value** 2474 2475| Type| Description| 2476| -------- | -------- | 2477| number | Offset plus the number of written bytes.| 2478 2479**Error codes** 2480 2481For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 2482 2483| ID| Error Message| 2484| -------- | -------- | 2485| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2486| 10200001 | The value of "[param]" is out of range. It must be >= [left range] and <= [right range]. Received value is: [param] | 2487 2488**Example** 2489 2490```ts 2491import { buffer } from '@kit.ArkTS'; 2492 2493let buf = buffer.allocUninitializedFromPool(8); 2494let result = buf.writeBigInt64LE(BigInt(0x0102030405060708), 0); 2495console.info("result = " + result); 2496// Output: result = 8 2497``` 2498 2499### writeBigUInt64BE 2500 2501writeBigUInt64BE(value: bigint, offset?: number): number 2502 2503**Atomic service API**: This API can be used in atomic services since API version 11. 2504 2505Writes a 64-bit, big-endian, unsigned big integer to this **Buffer** object at the specified offset. 2506 2507**System capability**: SystemCapability.Utils.Lang 2508 2509**Parameters** 2510 2511| Name| Type| Mandatory| Description| 2512| -------- | -------- | -------- | -------- | 2513| value | bigint | Yes| Data to write.| 2514| offset | number | No| Number of bytes to skip before starting to write data. The default value is **0**. Value range: 0 <= offset <= Buffer.length - 8| 2515 2516 2517**Return value** 2518 2519| Type| Description| 2520| -------- | -------- | 2521| number | Offset plus the number of written bytes.| 2522 2523**Error codes** 2524 2525For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 2526 2527| ID| Error Message| 2528| -------- | -------- | 2529| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2530| 10200001 | The value of "[param]" is out of range. It must be >= [left range] and <= [right range]. Received value is: [param] | 2531 2532**Example** 2533 2534```ts 2535import { buffer } from '@kit.ArkTS'; 2536 2537let buf = buffer.allocUninitializedFromPool(8); 2538let result = buf.writeBigUInt64BE(BigInt(0xdecafafecacefade), 0); 2539console.info("result = " + result); 2540// Output: result = 8 2541``` 2542 2543### writeBigUInt64LE 2544 2545writeBigUInt64LE(value: bigint, offset?: number): number 2546 2547Writes a 64-bit, little-endian, unsigned big integer to this **Buffer** object at the specified offset. 2548 2549**Atomic service API**: This API can be used in atomic services since API version 11. 2550 2551**System capability**: SystemCapability.Utils.Lang 2552 2553**Parameters** 2554 2555| Name| Type| Mandatory| Description| 2556| -------- | -------- | -------- | -------- | 2557| value | bigint | Yes| Data to write.| 2558| offset | number | No| Number of bytes to skip before starting to write data. The default value is **0**. Value range: 0 <= offset <= Buffer.length - 8| 2559 2560 2561**Return value** 2562 2563| Type| Description| 2564| -------- | -------- | 2565| number | Offset plus the number of written bytes.| 2566 2567**Error codes** 2568 2569For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 2570 2571| ID| Error Message| 2572| -------- | -------- | 2573| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2574| 10200001 | The value of "[param]" is out of range. It must be >= [left range] and <= [right range]. Received value is: [param] | 2575 2576**Example** 2577 2578```ts 2579import { buffer } from '@kit.ArkTS'; 2580 2581let buf = buffer.allocUninitializedFromPool(8); 2582let result = buf.writeBigUInt64LE(BigInt(0xdecafafecacefade), 0); 2583console.info("result = " + result); 2584// Output: result = 8 2585``` 2586 2587### writeDoubleBE 2588 2589writeDoubleBE(value: number, offset?: number): number 2590 2591Writes a 64-bit, big-endian, double-precision floating-point number to this **Buffer** object at the specified offset. 2592 2593**Atomic service API**: This API can be used in atomic services since API version 11. 2594 2595**System capability**: SystemCapability.Utils.Lang 2596 2597**Parameters** 2598 2599| Name| Type| Mandatory| Description| 2600| -------- | -------- | -------- | -------- | 2601| value | number | Yes| Data to write.| 2602| offset | number | No| Number of bytes to skip before starting to write data. The default value is **0**. Value range: 0 <= offset <= Buffer.length - 8| 2603 2604 2605**Return value** 2606 2607| Type| Description| 2608| -------- | -------- | 2609| number | Offset plus the number of written bytes.| 2610 2611**Error codes** 2612 2613For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 2614 2615| ID| Error Message| 2616| -------- | -------- | 2617| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2618| 10200001 | The value of "offset" is out of range. It must be >= 0 and <= buf.length - 8. Received value is: [offset] | 2619 2620**Example** 2621 2622```ts 2623import { buffer } from '@kit.ArkTS'; 2624 2625let buf = buffer.allocUninitializedFromPool(8); 2626let result = buf.writeDoubleBE(123.456, 0); 2627console.info("result = " + result); 2628// Output: result = 8 2629``` 2630 2631### writeDoubleLE 2632 2633writeDoubleLE(value: number, offset?: number): number 2634 2635Writes a 64-bit, little-endian, double-precision floating-point number to this **Buffer** object at the specified offset. 2636 2637**Atomic service API**: This API can be used in atomic services since API version 11. 2638 2639**System capability**: SystemCapability.Utils.Lang 2640 2641**Parameters** 2642 2643| Name| Type| Mandatory| Description| 2644| -------- | -------- | -------- | -------- | 2645| value | number | Yes| Data to write.| 2646| offset | number | No| Number of bytes to skip before starting to write data. The default value is **0**. Value range: 0 <= offset <= Buffer.length - 8| 2647 2648 2649**Return value** 2650 2651| Type| Description| 2652| -------- | -------- | 2653| number | Offset plus the number of written bytes.| 2654 2655**Error codes** 2656 2657For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 2658 2659| ID| Error Message| 2660| -------- | -------- | 2661| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2662| 10200001 | The value of "offset" is out of range. It must be >= 0 and <= buf.length - 8. Received value is: [offset] | 2663 2664**Example** 2665 2666```ts 2667import { buffer } from '@kit.ArkTS'; 2668 2669let buf = buffer.allocUninitializedFromPool(8); 2670let result = buf.writeDoubleLE(123.456, 0); 2671console.info("result = " + result); 2672// Output: result = 8 2673``` 2674 2675### writeFloatBE 2676 2677writeFloatBE(value: number, offset?: number): number 2678 2679Writes a 32-bit, big-endian, single-precision floating-point number to this **Buffer** object at the specified offset. 2680 2681**Atomic service API**: This API can be used in atomic services since API version 11. 2682 2683**System capability**: SystemCapability.Utils.Lang 2684 2685**Parameters** 2686 2687| Name| Type| Mandatory| Description| 2688| -------- | -------- | -------- | -------- | 2689| value | number | Yes| Data to write.| 2690| offset | number | No| Number of bytes to skip before starting to write data. The default value is **0**. Value range: 0 <= offset <= Buffer.length - 4| 2691 2692 2693**Return value** 2694 2695| Type| Description| 2696| -------- | -------- | 2697| number | Offset plus the number of written bytes.| 2698 2699**Error codes** 2700 2701For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 2702 2703| ID| Error Message| 2704| -------- | -------- | 2705| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2706| 10200001 | The value of "offset" is out of range. It must be >= 0 and <= buf.length - 4. Received value is: [offset] | 2707 2708**Example** 2709 2710```ts 2711import { buffer } from '@kit.ArkTS'; 2712 2713let buf = buffer.allocUninitializedFromPool(8); 2714let result = buf.writeFloatBE(0xcafebabe, 0); 2715console.info("result = " + result); 2716// Output: result = 4 2717``` 2718 2719 2720### writeFloatLE 2721 2722writeFloatLE(value: number, offset?: number): number 2723 2724Writes a 32-bit, little-endian, single-precision floating-point number to this **Buffer** object at the specified offset. 2725 2726**Atomic service API**: This API can be used in atomic services since API version 11. 2727 2728**System capability**: SystemCapability.Utils.Lang 2729 2730**Parameters** 2731 2732| Name| Type| Mandatory| Description| 2733| -------- | -------- | -------- | -------- | 2734| value | number | Yes| Data to write.| 2735| offset | number | No| Number of bytes to skip before starting to write data. The default value is **0**. Value range: 0 <= offset <= Buffer.length - 4| 2736 2737 2738**Return value** 2739 2740| Type| Description| 2741| -------- | -------- | 2742| number | Offset plus the number of written bytes.| 2743 2744**Error codes** 2745 2746For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 2747 2748| ID| Error Message| 2749| -------- | -------- | 2750| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2751| 10200001 | The value of "offset" is out of range. It must be >= 0 and <= buf.length - 4. Received value is: [offset] | 2752 2753**Example** 2754 2755```ts 2756import { buffer } from '@kit.ArkTS'; 2757 2758let buf = buffer.allocUninitializedFromPool(8); 2759let result = buf.writeFloatLE(0xcafebabe, 0); 2760console.info("result = " + result); 2761// Output: result = 4 2762``` 2763 2764### writeInt8 2765 2766writeInt8(value: number, offset?: number): number 2767 2768Writes an 8-bit signed integer to this **Buffer** object at the specified offset. 2769 2770**Atomic service API**: This API can be used in atomic services since API version 11. 2771 2772**System capability**: SystemCapability.Utils.Lang 2773 2774**Parameters** 2775 2776| Name| Type| Mandatory| Description| 2777| -------- | -------- | -------- | -------- | 2778| value | number | Yes| Data to write.| 2779| offset | number | No| Number of bytes to skip before starting to write data. The default value is **0**. Value range: 0 <= offset <= Buffer.length - 1| 2780 2781 2782**Return value** 2783 2784| Type| Description| 2785| -------- | -------- | 2786| number | Offset plus the number of written bytes.| 2787 2788**Error codes** 2789 2790For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 2791 2792| ID| Error Message| 2793| -------- | -------- | 2794| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2795| 10200001 | The value of "[param]" is out of range. It must be >= [left range] and <= [right range]. Received value is: [param] | 2796 2797**Example** 2798 2799```ts 2800import { buffer } from '@kit.ArkTS'; 2801 2802let buf = buffer.allocUninitializedFromPool(2); 2803let result = buf.writeInt8(2, 0); 2804console.info("result = " + result); 2805// Output: result = 1 2806let result1 = buf.writeInt8(-2, 1); 2807console.info("result1 = " + result1); 2808// Output: result1 = 2 2809``` 2810 2811 2812### writeInt16BE 2813 2814writeInt16BE(value: number, offset?: number): number 2815 2816Writes a 16-bit, big-endian, signed integer to this **Buffer** object at the specified offset. 2817 2818**Atomic service API**: This API can be used in atomic services since API version 11. 2819 2820**System capability**: SystemCapability.Utils.Lang 2821 2822**Parameters** 2823 2824| Name| Type| Mandatory| Description| 2825| -------- | -------- | -------- | -------- | 2826| value | number | Yes| Data to write.| 2827| offset | number | No| Number of bytes to skip before starting to write data. The default value is **0**. Value range: 0 <= offset <= Buffer.length - 2| 2828 2829 2830**Return value** 2831 2832| Type| Description| 2833| -------- | -------- | 2834| number | Offset plus the number of written bytes.| 2835 2836**Error codes** 2837 2838For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 2839 2840| ID| Error Message| 2841| -------- | -------- | 2842| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2843| 10200001 | The value of "[param]" is out of range. It must be >= [left range] and <= [right range]. Received value is: [param] | 2844 2845**Example** 2846 2847```ts 2848import { buffer } from '@kit.ArkTS'; 2849 2850let buf = buffer.allocUninitializedFromPool(2); 2851let result = buf.writeInt16BE(0x0102, 0); 2852console.info("result = " + result); 2853// Output: result = 2 2854``` 2855 2856 2857### writeInt16LE 2858 2859writeInt16LE(value: number, offset?: number): number 2860 2861Writes a 16-bit, little-endian, signed integer to this **Buffer** object at the specified offset. 2862 2863**Atomic service API**: This API can be used in atomic services since API version 11. 2864 2865**System capability**: SystemCapability.Utils.Lang 2866 2867**Parameters** 2868 2869| Name| Type| Mandatory| Description| 2870| -------- | -------- | -------- | -------- | 2871| value | number | Yes| Data to write.| 2872| offset | number | No| Number of bytes to skip before starting to write data. The default value is **0**. Value range: 0 <= offset <= Buffer.length - 2| 2873 2874 2875**Return value** 2876 2877| Type| Description| 2878| -------- | -------- | 2879| number | Offset plus the number of written bytes.| 2880 2881**Error codes** 2882 2883For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 2884 2885| ID| Error Message| 2886| -------- | -------- | 2887| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2888| 10200001 | The value of "[param]" is out of range. It must be >= [left range] and <= [right range]. Received value is: [param] | 2889 2890**Example** 2891 2892```ts 2893import { buffer } from '@kit.ArkTS'; 2894 2895let buf = buffer.allocUninitializedFromPool(2); 2896let result = buf.writeInt16LE(0x0304, 0); 2897console.info("result = " + result); 2898// Output: result = 2 2899``` 2900 2901### writeInt32BE 2902 2903writeInt32BE(value: number, offset?: number): number 2904 2905Writes a 32-bit, big-endian, signed integer to this **Buffer** object at the specified offset. 2906 2907**Atomic service API**: This API can be used in atomic services since API version 11. 2908 2909**System capability**: SystemCapability.Utils.Lang 2910 2911**Parameters** 2912 2913| Name| Type| Mandatory| Description| 2914| -------- | -------- | -------- | -------- | 2915| value | number | Yes| Data to write.| 2916| offset | number | No| Number of bytes to skip before starting to write data. The default value is **0**. Value range: 0 <= offset <= Buffer.length - 4| 2917 2918 2919**Return value** 2920 2921| Type| Description| 2922| -------- | -------- | 2923| number | Offset plus the number of written bytes.| 2924 2925**Error codes** 2926 2927For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 2928 2929| ID| Error Message| 2930| -------- | -------- | 2931| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2932| 10200001 | The value of "[param]" is out of range. It must be >= [left range] and <= [right range]. Received value is: [param] | 2933 2934**Example** 2935 2936```ts 2937import { buffer } from '@kit.ArkTS'; 2938 2939let buf = buffer.allocUninitializedFromPool(4); 2940let result = buf.writeInt32BE(0x01020304, 0); 2941console.info("result = " + result); 2942// Output: result = 4 2943``` 2944 2945 2946### writeInt32LE 2947 2948writeInt32LE(value: number, offset?: number): number 2949 2950Writes a 32-bit, little-endian, signed integer to this **Buffer** object at the specified offset. 2951 2952**Atomic service API**: This API can be used in atomic services since API version 11. 2953 2954**System capability**: SystemCapability.Utils.Lang 2955 2956**Parameters** 2957 2958| Name| Type| Mandatory| Description| 2959| -------- | -------- | -------- | -------- | 2960| value | number | Yes| Data to write.| 2961| offset | number | No| Number of bytes to skip before starting to write data. The default value is **0**. Value range: 0 <= offset <= Buffer.length - 4| 2962 2963 2964**Return value** 2965 2966| Type| Description| 2967| -------- | -------- | 2968| number | Offset plus the number of written bytes.| 2969 2970**Error codes** 2971 2972For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 2973 2974| ID| Error Message| 2975| -------- | -------- | 2976| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2977| 10200001 | The value of "[param]" is out of range. It must be >= [left range] and <= [right range]. Received value is: [param] | 2978 2979**Example** 2980 2981```ts 2982import { buffer } from '@kit.ArkTS'; 2983 2984let buf = buffer.allocUninitializedFromPool(4); 2985let result = buf.writeInt32LE(0x05060708, 0); 2986console.info("result = " + result); 2987// Output: result = 4 2988``` 2989 2990### writeIntBE 2991 2992writeIntBE(value: number, offset: number, byteLength: number): number 2993 2994Writes a big-endian signed value of the specified length to this **Buffer** object at the specified offset. 2995 2996**Atomic service API**: This API can be used in atomic services since API version 11. 2997 2998**System capability**: SystemCapability.Utils.Lang 2999 3000**Parameters** 3001 3002| Name| Type| Mandatory| Description| 3003| -------- | -------- | -------- | -------- | 3004| value | number | Yes| Data to write.| 3005| offset | number | Yes| Number of bytes to skip before starting to write data. The default value is **0**. Value range: 0 <= offset <= Buffer.length - byteLength| 3006| byteLength | number | Yes| Number of bytes to write.| 3007 3008 3009**Return value** 3010 3011| Type| Description| 3012| -------- | -------- | 3013| number | Offset plus the number of written bytes.| 3014 3015**Error codes** 3016 3017For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 3018 3019| ID| Error Message| 3020| -------- | -------- | 3021| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3022| 10200001 | The value of "[param]" is out of range. It must be >= [left range] and <= [right range]. Received value is: [param] | 3023 3024**Example** 3025 3026```ts 3027import { buffer } from '@kit.ArkTS'; 3028 3029let buf = buffer.allocUninitializedFromPool(6); 3030let result = buf.writeIntBE(0x1234567890ab, 0, 6); 3031console.info("result = " + result); 3032// Output: result = 6 3033``` 3034 3035 3036### writeIntLE 3037 3038writeIntLE(value: number, offset: number, byteLength: number): number 3039 3040Writes a little-endian signed value of the specified length to this **Buffer** object at the specified offset. 3041 3042**Atomic service API**: This API can be used in atomic services since API version 11. 3043 3044**System capability**: SystemCapability.Utils.Lang 3045 3046**Parameters** 3047 3048| Name| Type| Mandatory| Description| 3049| -------- | -------- | -------- | -------- | 3050| value | number | Yes| Data to write.| 3051| offset | number | Yes| Number of bytes to skip before starting to write data. The default value is **0**. Value range: 0 <= offset <= Buffer.length - byteLength| 3052| byteLength | number | Yes| Number of bytes to write.| 3053 3054 3055**Return value** 3056 3057| Type| Description| 3058| -------- | -------- | 3059| number | Offset plus the number of written bytes.| 3060 3061**Error codes** 3062 3063For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 3064 3065| ID| Error Message| 3066| -------- | -------- | 3067| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3068| 10200001 | The value of "[param]" is out of range. It must be >= [left range] and <= [right range]. Received value is: [param] | 3069 3070**Example** 3071 3072```ts 3073import { buffer } from '@kit.ArkTS'; 3074 3075let buf = buffer.allocUninitializedFromPool(6); 3076let result = buf.writeIntLE(0x1234567890ab, 0, 6); 3077console.info("result = " + result); 3078// Output: result = 6 3079``` 3080 3081### writeUInt8 3082 3083writeUInt8(value: number, offset?: number): number 3084 3085Writes an 8-bit unsigned integer to this **Buffer** object at the specified offset. 3086 3087**Atomic service API**: This API can be used in atomic services since API version 11. 3088 3089**System capability**: SystemCapability.Utils.Lang 3090 3091**Parameters** 3092 3093| Name| Type| Mandatory| Description| 3094| -------- | -------- | -------- | -------- | 3095| value | number | Yes| Data to write.| 3096| offset | number | No| Number of bytes to skip before starting to write data. The default value is **0**. Value range: 0 <= offset <= Buffer.length - 1| 3097 3098 3099**Return value** 3100 3101| Type| Description| 3102| -------- | -------- | 3103| number | Offset plus the number of written bytes.| 3104 3105**Error codes** 3106 3107For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 3108 3109| ID| Error Message| 3110| -------- | -------- | 3111| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 3112| 10200001 | The value of "[param]" is out of range. It must be >= [left range] and <= [right range]. Received value is: [param] | 3113 3114**Example** 3115 3116```ts 3117import { buffer } from '@kit.ArkTS'; 3118 3119let buf = buffer.allocUninitializedFromPool(4); 3120let result = buf.writeUInt8(0x3, 0); 3121console.info("result = " + result); 3122// Output: result = 1 3123let result1 = buf.writeUInt8(0x4, 1); 3124console.info("result1 = " + result1); 3125// Output: result1 = 2 3126let result2 = buf.writeUInt8(0x23, 2); 3127console.info("result2 = " + result2); 3128// Output: result2 = 3 3129let result3 = buf.writeUInt8(0x42, 3); 3130console.info("result3 = " + result3); 3131// Output: result3 = 4 3132``` 3133 3134### writeUInt16BE 3135 3136writeUInt16BE(value: number, offset?: number): number 3137 3138Writes a 16-bit, big-endian, unsigned integer to this **Buffer** object at the specified offset. 3139 3140**Atomic service API**: This API can be used in atomic services since API version 11. 3141 3142**System capability**: SystemCapability.Utils.Lang 3143 3144**Parameters** 3145 3146| Name| Type| Mandatory| Description| 3147| -------- | -------- | -------- | -------- | 3148| value | number | Yes| Data to write.| 3149| offset | number | No| Number of bytes to skip before starting to write data. The default value is **0**. Value range: 0 <= offset <= Buffer.length - 2| 3150 3151 3152**Return value** 3153 3154| Type| Description| 3155| -------- | -------- | 3156| number | Offset plus the number of written bytes.| 3157 3158**Error codes** 3159 3160For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 3161 3162| ID| Error Message| 3163| -------- | -------- | 3164| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 3165| 10200001 | The value of "[param]" is out of range. It must be >= [left range] and <= [right range]. Received value is: [param] | 3166 3167**Example** 3168 3169```ts 3170import { buffer } from '@kit.ArkTS'; 3171 3172let buf = buffer.allocUninitializedFromPool(4); 3173let result = buf.writeUInt16BE(0xdead, 0); 3174console.info("result = " + result); 3175// Output: result = 2 3176let result1 = buf.writeUInt16BE(0xbeef, 2); 3177console.info("result1 = " + result1); 3178// Output: result1 = 4 3179``` 3180 3181### writeUInt16LE 3182 3183writeUInt16LE(value: number, offset?: number): number 3184 3185Writes a 16-bit, little-endian, unsigned integer to this **Buffer** object at the specified offset. 3186 3187**Atomic service API**: This API can be used in atomic services since API version 11. 3188 3189**System capability**: SystemCapability.Utils.Lang 3190 3191**Parameters** 3192 3193| Name| Type| Mandatory| Description| 3194| -------- | -------- | -------- | -------- | 3195| value | number | Yes| Data to write.| 3196| offset | number | No| Number of bytes to skip before starting to write data. The default value is **0**. Value range: 0 <= offset <= Buffer.length - 2| 3197 3198 3199**Return value** 3200 3201| Type| Description| 3202| -------- | -------- | 3203| number | Offset plus the number of written bytes.| 3204 3205**Error codes** 3206 3207For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 3208 3209| ID| Error Message| 3210| -------- | -------- | 3211| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 3212| 10200001 | The value of "[param]" is out of range. It must be >= [left range] and <= [right range]. Received value is: [param] | 3213 3214**Example** 3215 3216```ts 3217import { buffer } from '@kit.ArkTS'; 3218 3219let buf = buffer.allocUninitializedFromPool(4); 3220let result = buf.writeUInt16LE(0xdead, 0); 3221console.info("result = " + result); 3222// Output: result = 2 3223let result1 = buf.writeUInt16LE(0xbeef, 2); 3224console.info("result1 = " + result1); 3225// Output: result1 = 4 3226``` 3227 3228### writeUInt32BE 3229 3230writeUInt32BE(value: number, offset?: number): number 3231 3232Writes a 32-bit, big-endian, unsigned integer to this **Buffer** object at the specified offset. 3233 3234**Atomic service API**: This API can be used in atomic services since API version 11. 3235 3236**System capability**: SystemCapability.Utils.Lang 3237 3238**Parameters** 3239 3240| Name| Type| Mandatory| Description| 3241| -------- | -------- | -------- | -------- | 3242| value | number | Yes| Data to write.| 3243| offset | number | No| Number of bytes to skip before starting to write data. The default value is **0**. Value range: 0 <= offset <= Buffer.length - 4| 3244 3245 3246**Return value** 3247 3248| Type| Description| 3249| -------- | -------- | 3250| number | Offset plus the number of written bytes.| 3251 3252**Error codes** 3253 3254For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 3255 3256| ID| Error Message| 3257| -------- | -------- | 3258| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 3259| 10200001 | The value of "[param]" is out of range. It must be >= [left range] and <= [right range]. Received value is: [param] | 3260 3261**Example** 3262 3263```ts 3264import { buffer } from '@kit.ArkTS'; 3265 3266let buf = buffer.allocUninitializedFromPool(4); 3267let result = buf.writeUInt32BE(0xfeedface, 0); 3268console.info("result = " + result); 3269// Output: result = 4 3270``` 3271 3272### writeUInt32LE 3273 3274writeUInt32LE(value: number, offset?: number): number 3275 3276Writes a 32-bit, little-endian, unsigned integer to this **Buffer** object at the specified offset. 3277 3278**Atomic service API**: This API can be used in atomic services since API version 11. 3279 3280**System capability**: SystemCapability.Utils.Lang 3281 3282**Parameters** 3283 3284| Name| Type| Mandatory| Description| 3285| -------- | -------- | -------- | -------- | 3286| value | number | Yes| Data to write.| 3287| offset | number | No| Number of bytes to skip before starting to write data. The default value is **0**. Value range: 0 <= offset <= Buffer.length - 4| 3288 3289 3290**Return value** 3291 3292| Type| Description| 3293| -------- | -------- | 3294| number | Offset plus the number of written bytes.| 3295 3296**Error codes** 3297 3298For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 3299 3300| ID| Error Message| 3301| -------- | -------- | 3302| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 3303| 10200001 | The value of "[param]" is out of range. It must be >= [left range] and <= [right range]. Received value is: [param] | 3304 3305**Example** 3306 3307```ts 3308import { buffer } from '@kit.ArkTS'; 3309 3310let buf = buffer.allocUninitializedFromPool(4); 3311let result = buf.writeUInt32LE(0xfeedface, 0); 3312console.info("result = " + result); 3313// Output: result = 4 3314``` 3315 3316### writeUIntBE 3317 3318writeUIntBE(value: number, offset: number, byteLength: number): number 3319 3320Writes an unsigned big-endian value of the specified length to this **Buffer** object at the specified offset. 3321 3322**Atomic service API**: This API can be used in atomic services since API version 11. 3323 3324**System capability**: SystemCapability.Utils.Lang 3325 3326**Parameters** 3327 3328| Name| Type| Mandatory| Description| 3329| -------- | -------- | -------- | -------- | 3330| value | number | Yes| Data to write.| 3331| offset | number | Yes| Number of bytes to skip before starting to write data. The default value is **0**. Value range: 0 <= offset <= Buffer.length - byteLength| 3332| byteLength | number | Yes| Number of bytes to write.| 3333 3334 3335**Return value** 3336 3337| Type| Description| 3338| -------- | -------- | 3339| number | Offset plus the number of written bytes.| 3340 3341**Error codes** 3342 3343For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 3344 3345| ID| Error Message| 3346| -------- | -------- | 3347| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3348| 10200001 | The value of "[param]" is out of range. It must be >= [left range] and <= [right range]. Received value is: [param] | 3349 3350**Example** 3351 3352```ts 3353import { buffer } from '@kit.ArkTS'; 3354 3355let buf = buffer.allocUninitializedFromPool(6); 3356let result = buf.writeUIntBE(0x1234567890ab, 0, 6); 3357console.info("result = " + result); 3358// Output: result = 6 3359``` 3360 3361### writeUIntLE 3362 3363writeUIntLE(value: number, offset: number, byteLength: number): number 3364 3365Writes an unsigned little-endian value of the specified length to this **Buffer** object at the specified offset. 3366 3367**Atomic service API**: This API can be used in atomic services since API version 11. 3368 3369**System capability**: SystemCapability.Utils.Lang 3370 3371**Parameters** 3372 3373| Name| Type| Mandatory| Description| 3374| -------- | -------- | -------- | -------- | 3375| value | number | Yes| Data to write.| 3376| offset | number | Yes| Number of bytes to skip before starting to write data. The default value is **0**. Value range: 0 <= offset <= Buffer.length - byteLength| 3377| byteLength | number | Yes| Number of bytes to write.| 3378 3379 3380**Return value** 3381 3382| Type| Description| 3383| -------- | -------- | 3384| number | Offset plus the number of written bytes.| 3385 3386**Error codes** 3387 3388For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 3389 3390| ID| Error Message| 3391| -------- | -------- | 3392| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3393| 10200001 | The value of "[param]" is out of range. It must be >= [left range] and <= [right range]. Received value is: [param] | 3394 3395**Example** 3396 3397```ts 3398import { buffer } from '@kit.ArkTS'; 3399 3400let buf = buffer.allocUninitializedFromPool(6); 3401let result = buf.writeUIntLE(0x1234567890ab, 0, 6); 3402console.info("result = " + result); 3403// Output: result = 6 3404``` 3405 3406## Blob 3407 3408### Properties 3409 3410**Atomic service API**: This API can be used in atomic services since API version 11. 3411 3412**System capability**: SystemCapability.Utils.Lang 3413 3414| Name| Type| Read-Only| Optional| Description| 3415| -------- | -------- | -------- | -------- | -------- | 3416| size | number | Yes| No| Total size of the **Blob** object, in bytes.| 3417| type | string | Yes| No| Type of the data in the **Blob** object.| 3418 3419### constructor 3420 3421constructor(sources: string[] | ArrayBuffer[] | TypedArray[] | DataView[] | Blob[] , options?: Object) 3422 3423A constructor used to create a **Blob** object. 3424 3425**Atomic service API**: This API can be used in atomic services since API version 11. 3426 3427**System capability**: SystemCapability.Utils.Lang 3428 3429**Parameters** 3430 3431| Name| Type| Mandatory| Description| 3432| -------- | -------- | -------- | -------- | 3433| sources | string[] \| ArrayBuffer[] \| TypedArray[] \| DataView[] \| Blob[] | Yes| Data sources of the **Blob** object.| 3434| options | Object | No| options:<br>- **endings**: specifies how the terminator **'\n'** is output. The value can be **'native'** or **'transparent'**. **'native'** means that the terminator follows the system. **'transparent'** means that the terminator stored in the **Blob** object remains unchanged. The default value is **'transparent'**.<br>- **type**: type of the data in the **Blob** object. This type represents the MIME type of the data. However, it is not used for type format validation. The default value is **''**.| 3435 3436**Error codes** 3437 3438For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 3439 3440| ID| Error Message| 3441| -------- | -------- | 3442| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3443 3444**Example** 3445```ts 3446import { buffer } from '@kit.ArkTS'; 3447 3448let blob: buffer.Blob = new buffer.Blob(['a', 'b', 'c']); 3449 3450class option { 3451 endings: string = ""; 3452 type: string = ""; 3453} 3454let o1: option = {endings:'native', type: 'MIME'} 3455let blob1: buffer.Blob = new buffer.Blob(['a', 'b', 'c'], o1); 3456``` 3457 3458### arrayBuffer 3459 3460arrayBuffer(): Promise<ArrayBuffer> 3461 3462Puts the **Blob** data into an **ArrayBuffer** object. This API uses a promise to return the result. 3463 3464**Atomic service API**: This API can be used in atomic services since API version 11. 3465 3466**System capability**: SystemCapability.Utils.Lang 3467 3468**Return value** 3469| Type| Description| 3470| -------- | -------- | 3471| Promise<ArrayBuffer> | Promise used to return the **ArrayBuffer** containing the **Blob** data.| 3472 3473**Example** 3474```ts 3475import { buffer } from '@kit.ArkTS'; 3476 3477let blob: buffer.Blob = new buffer.Blob(['a', 'b', 'c']); 3478let pro = blob.arrayBuffer(); 3479pro.then((val: ArrayBuffer) => { 3480 let uint8Array: Uint8Array = new Uint8Array(val); 3481 console.info(uint8Array.toString()); 3482 // Output: 97,98,99 3483}); 3484``` 3485### slice 3486 3487slice(start?: number, end?: number, type?: string): Blob 3488 3489Creates and returns a **Blob** object that contains specified data from this **Blob** object. 3490 3491**Atomic service API**: This API can be used in atomic services since API version 11. 3492 3493**System capability**: SystemCapability.Utils.Lang 3494 3495**Parameters** 3496 3497| Name| Type| Mandatory| Description| 3498| -------- | -------- | -------- | -------- | 3499| start | number | No| Offset to the start position of the data to copy. The default value is **0**.| 3500| end | number | No| Offset to the end position of the data to copy. The default value is the data length in the original **Blob** object.| 3501| type | string | No| Type of the data in the new **Blob** object. The default value is **''**.| 3502 3503**Return value** 3504| Type| Description| 3505| -------- | -------- | 3506| Blob | New **Blob** object created.| 3507 3508**Example** 3509```ts 3510import { buffer } from '@kit.ArkTS'; 3511 3512let blob: buffer.Blob = new buffer.Blob(['a', 'b', 'c']); 3513let blob2 = blob.slice(0, 2); 3514let blob3 = blob.slice(0, 2, "MIME"); 3515console.info("type:", blob3.type); // type: MIME 3516``` 3517 3518### text 3519 3520text(): Promise<string> 3521 3522Decodes and returns text using UTF-8. This API uses a promise to return the result. 3523 3524**Atomic service API**: This API can be used in atomic services since API version 11. 3525 3526**System capability**: SystemCapability.Utils.Lang 3527 3528**Return value** 3529| Type| Description| 3530| -------- | -------- | 3531| Promise<string> | Promise used to return the text decoded in UTF-8.| 3532 3533**Example** 3534```ts 3535import { buffer } from '@kit.ArkTS'; 3536 3537let blob: buffer.Blob = new buffer.Blob(['a', 'b', 'c']); 3538let pro = blob.text(); 3539pro.then((val: string) => { 3540 console.info(val); 3541 // Output: abc 3542}); 3543``` 3544