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