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