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