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