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