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