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