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