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