1# @ohos.util.stream (数据流基类stream) 2 3本模块提供基本流类型的处理能力。可以将数据分块读取或写入,而不是一次将整个数据加载到内存当中。 4 5包括可写流([Writable](#writable))、可读流([Readable](#readable))、双工流([Duplex](#duplex))、转换流([Transform](#transform))这几种流。 6 7> **说明:** 8> 9> 本模块首批接口从API version 12开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 10 11## 导入模块 12 13```ts 14import { stream } from '@kit.ArkTS'; 15``` 16 17## Writable 18 19可写入数据的流。可写流允许将数据写入到目标中,这个目标可以是文件、HTTP 响应、标准输出、另一个流等。 20 21### 属性 22 23**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 24 25**系统能力:** SystemCapability.Utils.Lang 26 27| 名称 | 类型 | 只读 | 可选 | 说明 | 28| ------- | -------- | ------ | ------ | ----------- | 29| writableObjectMode | boolean | 是 | 否 | 指定可写流是否以对象模式工作。true表示流被配置为对象模式,false表示流处于非对象模式。当前版本只支持原始数据(字符串和Uint8Array),返回值为false。 | 30| writableHighWatermark | number | 是 | 否 | 定义缓冲区可以存放的最大数据量。默认为16 * 1024,单位为字节。| 31| writable | boolean | 是 | 否 | 表示可写流是否处于可写状态。true表示流当前是可写的,false表示流当前不再接受写入操作。| 32| writableLength | number | 是 | 否 | 表示可读流缓冲区中待写入的字节数。| 33| writableCorked | number | 是 | 否 | 表示需要调用uncork()方法的次数,以完全解除可写流的封住状态。| 34| writableEnded | boolean | 是 | 否 | 表示当前可写流的[end()](#end)是否被调用,该状态不代表数据已经全部写入。true表示[end()](#end)已被调用,false表示[end()](#end)未被调用。 | 35| writableFinished | boolean | 是 | 否 | 表示当前可写流是否处于写入完成状态。true表示当前流处于写入完成状态,false表示当前流写入操作可能还在进行中。 | 36 37### constructor 38 39constructor() 40 41Writable的构造函数。 42 43**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 44 45**系统能力:** SystemCapability.Utils.Lang 46 47**示例:** 48 49```ts 50let writableStream = new stream.Writable(); 51``` 52 53### write 54 55write(chunk?: string | Uint8Array, encoding?: string, callback?: Function): boolean 56 57将数据写入流的缓冲区中。使用callback异步回调。 58 59**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 60 61**系统能力:** SystemCapability.Utils.Lang 62 63**参数:** 64 65| 参数名 | 类型 | 必填 | 说明 | 66| ------ | ------ | ---- | -------------------------- | 67| chunk | string \| Uint8Array | 否 | 需要写入的数据。当前版本不支持输入null、undefined和空字符串。 | 68| encoding | string | 否 | 字符编码类型。默认值是'utf8',当前版本支持'utf8'、'gb18030'、'gbk'以及'gb2312'。| 69| callback | Function | 否 | 回调函数。默认不调用。 | 70 71**返回值:** 72 73| 类型 | 说明 | 74| ------ | ---------------------- | 75| boolean | 可写流的缓冲区中是否还有空间。true表示缓冲区还有空间,false表示流的内部缓冲区已满。 | 76 77**错误码:** 78 79以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 80 81| 错误码ID | 错误信息 | 82| -------- | -------- | 83| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 84| 10200035 | The doWrite method has not been implemented. | 85| 10200036 | The stream has been ended. | 86| 10200037 | The callback is invoked multiple times consecutively. | 87 88**示例:** 89 90```ts 91class TestWritable extends stream.Writable { 92 constructor() { 93 super(); 94 } 95 96 doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) { 97 console.info("Writable chunk is", chunk); // Writable chunk is test 98 callback(); 99 } 100} 101 102let writableStream = new TestWritable(); 103writableStream.write('test', 'utf8'); 104``` 105 106### end 107 108end(chunk?: string | Uint8Array, encoding?: string, callback?: Function): Writable 109 110结束可写流的写入操作。如果传入chunk参数,则根据实际运行情况,通过write或者doWrite将其作为最后一块数据写入。其中通过doWrite写入时,encoding参数的合法性检查依赖doWrite。end单独使用(不使用write)并传入chunk参数的情况下,必然通过doWrite写入。使用callback异步回调。 111 112**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 113 114**系统能力:** SystemCapability.Utils.Lang 115 116**参数:** 117 118| 参数名 | 类型 | 必填 | 说明 | 119| ------ | ------ | ---- | -------------------------- | 120| chunk | string \| Uint8Array | 否 | 需要写入的数据。默认为undefined。 | 121| encoding | string | 否 | 字符编码类型。默认值是'utf8',当前版本支持'utf8'、'gb18030'、'gbk'以及'gb2312'。| 122| callback | Function | 否 | 回调函数。| 123 124**返回值:** 125 126| 类型 | 说明 | 127| ------ | ---------------------- | 128| [Writable](#writable) | 返回当前可写流对象。 | 129 130**错误码:** 131 132以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 133 134| 错误码ID | 错误信息 | 135| -------- | -------- | 136| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 137| 10200035 | The doWrite method has not been implemented. | 138 139**示例:** 140 141```ts 142class TestWritable extends stream.Writable { 143 constructor() { 144 super(); 145 } 146 147 doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) { 148 console.info("Writable chunk is", chunk); 149 callback(); 150 } 151/** 152 * Writable chunk is test 153 * Writable chunk is finish 154 * */ 155} 156 157let writableStream = new TestWritable(); 158writableStream.write('test', 'utf8'); 159writableStream.end('finish', 'utf8', () => { 160 console.info("Writable is end"); // Writable is end 161}); 162``` 163 164### setDefaultEncoding 165 166setDefaultEncoding(encoding?: string): boolean 167 168设置可写流的默认字符编码。 169 170**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 171 172**系统能力:** SystemCapability.Utils.Lang 173 174**参数:** 175 176| 参数名 | 类型 | 必填 | 说明 | 177| -------- | -------- | -------- | -------- | 178| encoding | string | 否 | 设置默认字符编码。默认值是'utf8',当前版本支持'utf8'、'gb18030'、'gbk'以及'gb2312'。| 179 180**返回值:** 181 182| 类型 | 说明 | 183| -------- | -------- | 184| boolean | 返回是否设置成功。true表示设置成功,false表示设置失败。 | 185 186**错误码:** 187 188以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 189 190| 错误码ID | 错误信息 | 191| -------- | -------- | 192| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 193 194**示例:** 195 196```ts 197class TestWritable extends stream.Writable { 198 constructor() { 199 super(); 200 } 201 202 doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) { 203 callback(); 204 } 205} 206 207let writableStream = new TestWritable(); 208let result = writableStream.setDefaultEncoding('utf8'); 209console.info("Writable is result", result); // Writable is result true 210``` 211 212### cork 213 214cork(): boolean 215 216将写入的数据强制写入缓冲区暂存,用来优化连续写入操作的性能。 217 218**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 219 220**系统能力:** SystemCapability.Utils.Lang 221 222**返回值:** 223 224| 类型 | 说明 | 225| -------- | -------- | 226| boolean | 返回设置cork状态是否成功。true表示设置成功,false表示设置失败。 | 227 228**示例:** 229 230```ts 231class TestWritable extends stream.Writable { 232 constructor() { 233 super(); 234 } 235 236 doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) { 237 callback(); 238 } 239} 240 241let writableStream = new TestWritable(); 242let result = writableStream.cork(); 243console.info("Writable cork result", result); // Writable cork result true 244``` 245 246### uncork 247 248uncork(): boolean 249 250解除cork状态,将缓冲区中的数据全部刷新,并将其写入目标位置。 251 252**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 253 254**系统能力:** SystemCapability.Utils.Lang 255 256**返回值:** 257 258| 类型 | 说明 | 259| -------- | -------- | 260| boolean | 返回解除cork状态是否成功。true表示成功,false表示失败。 | 261 262**示例:** 263 264```ts 265class TestWritable extends stream.Writable { 266 constructor() { 267 super(); 268 } 269 270 doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) { 271 callback(); 272 } 273} 274 275let writableStream = new TestWritable(); 276writableStream.cork(); 277writableStream.write('data1', 'utf8'); 278writableStream.write('data2', 'utf8'); 279writableStream.uncork(); 280writableStream.end(); 281writableStream.on('finish', () => { 282 console.info("all Data is End"); // all Data is End 283}); 284``` 285 286### on 287 288on(event: string, callback: Callback<emitter.EventData>): void 289 290注册事件处理函数来监听可写流上的不同事件。 291 292**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 293 294**系统能力:** SystemCapability.Utils.Lang 295 296**参数:** 297 298| 参数名 | 类型 | 必填 | 说明 | 299| -------- | -------- | -------- | -------- | 300| event | string | 是 | 事件回调类型,支持的事件包括:`'close'` \| `'drain' `\|`'error'` \| `'finish'` 。<br/>\- `'close'`:完成[end()](#end)调用,结束写入操作,触发该事件。<br/>\- `'drain'`:在可写流缓冲区中数据清空时触发该事件。<br/>\- `'error'`:在可写流发生异常时触发该事件。<br/>\- `'finish'`:在数据缓冲区全部写入到目标后触发该事件。 | 301| callback | Callback\<[emitter.EventData](../apis-basic-services-kit/js-apis-emitter.md#eventdata)\> | 是 | 回调函数,返回事件传输的数据。 | 302 303**错误码:** 304 305以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 306 307| 错误码ID | 错误信息 | 308| -------- | -------- | 309| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 310 311**示例:** 312 313```ts 314class TestWritable extends stream.Writable { 315 constructor() { 316 super(); 317 } 318 319 doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) { 320 callback(new Error()); 321 } 322} 323 324let callbackCalled = false; 325let writable = new TestWritable(); 326writable.on('error', () => { 327 console.info("Writable event test", callbackCalled.toString()); // Writable event test false 328}); 329writable.write('hello', 'utf8', () => { 330}); 331``` 332 333### off 334 335off(event: string, callback?: Callback<emitter.EventData>): void 336 337取消通过[on](#on)注册的事件处理函数。 338 339**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 340 341**系统能力:** SystemCapability.Utils.Lang 342 343**参数:** 344 345| 参数名 | 类型 | 必填 | 说明 | 346| -------- | -------- | -------- | -------- | 347| event | string | 是 | 事件回调类型,支持的事件包括:`'close'` \| `'drain' `\|`'error'` \| `'finish'` 。<br/>\- `'close'`:完成[end()](#end)调用,结束写入操作,触发该事件。<br/>\- `'drain'`:在可写流缓冲区中数据清空时触发该事件。<br/>\- `'error'`:在可写流发生异常时触发该事件。<br/>\- `'finish'`:在数据缓冲区全部写入到目标后触发该事件。 | 348| callback | Callback\<[emitter.EventData](../apis-basic-services-kit/js-apis-emitter.md#eventdata)\> | 否 | 回调函数。 | 349 350**错误码:** 351 352以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 353 354| 错误码ID | 错误信息 | 355| -------- | -------- | 356| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 357 358**示例:** 359 360```ts 361class TestWritable extends stream.Writable { 362 constructor() { 363 super(); 364 } 365 366 doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) { 367 callback(); 368 } 369} 370 371let writableStream = new TestWritable(); 372let testListenerCalled = false; 373let testListener = () => { 374 testListenerCalled = true; 375}; 376writableStream.on('finish', testListener); 377writableStream.off('finish'); 378writableStream.write('test'); 379writableStream.end(); 380setTimeout(() => { 381 console.info("Writable off test", testListenerCalled.toString()); // Writable off test false 382}, 0); 383``` 384 385### doInitialize 386 387doInitialize(callback: Function): void 388 389使用者实现这个函数,这个函数在可写流初始化阶段被调用,无需用户调用。使用callback异步回调。 390 391**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 392 393**系统能力:** SystemCapability.Utils.Lang 394 395**参数:** 396 397| 参数名 | 类型 | 必填 | 说明 | 398| -------- | -------- | -------- | -------- | 399| callback | Function | 是 | 回调函数。 | 400 401**错误码:** 402 403以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 404 405| 错误码ID | 错误信息 | 406| -------- | -------- | 407| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 408 409**示例:** 410 411```ts 412class MyWritable extends stream.Writable { 413 doInitialize(callback: Function) { 414 super.doInitialize(callback); 415 console.info("Writable doInitialize"); // Writable doInitialize 416 } 417 418 doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) { 419 super.doWrite(chunk, encoding, callback); 420 } 421} 422 423new MyWritable(); 424``` 425 426### doWrite 427 428doWrite(chunk: string | Uint8Array, encoding: string, callback: Function): void 429 430提供一个数据写出接口供使用者实现,该接口函数会在数据被成功写出时自动调用,无需用户手动触发。使用callback异步回调。 431 432**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 433 434**系统能力:** SystemCapability.Utils.Lang 435 436**参数:** 437 438| 参数名 | 类型 | 必填 | 说明 | 439| ------ | ------ | ---- | -------------------------- | 440| chunk | string \| Uint8Array | 是 | 要写出的数据。 | 441| encoding | string | 是 | 字符编码类型。当前版本支持'utf8'、'gb18030'、'gbk'以及'gb2312'。| 442| callback | Function | 是 | 回调函数。 | 443 444**错误码:** 445 446以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 447 448| 错误码ID | 错误信息 | 449| -------- | -------- | 450| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 451 452**示例:** 453 454```ts 455class TestWritable extends stream.Writable { 456 constructor() { 457 super(); 458 } 459 460 doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) { 461 console.info("Writable chunk is", chunk); // Writable chunk is data 462 callback(); 463 } 464} 465 466let writableStream = new TestWritable(); 467writableStream.write('data', 'utf8'); 468``` 469 470### doWritev 471 472doWritev(chunks: string[] | Uint8Array[], callback: Function): void 473 474提供一个数据批量写出接口供使用者实现,该接口函数会在数据被成功写出时自动调用,无需用户手动触发。使用callback异步回调。 475 476**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 477 478**系统能力:** SystemCapability.Utils.Lang 479 480**参数:** 481 482| 参数名 | 类型 | 必填 | 说明 | 483| -------- | -------- | -------- | -------- | 484| chunks | string[] \| Uint8Array[] | 是 | 被批量写出的数据数组。 | 485| callback | Function | 是 | 回调函数。 | 486 487**错误码:** 488 489以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 490 491| 错误码ID | 错误信息 | 492| -------- | -------- | 493| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 494 495**示例:** 496 497```ts 498class TestWritable extends stream.Writable { 499 constructor() { 500 super(); 501 } 502 503 doWritev(chunks: string[] | Uint8Array[], callback: Function) { 504 console.info("Writable chunk", chunks); 505 callback(); 506 } 507/** 508 * Writable chunk data1 509 * Writable chunk data2 510* */ 511} 512 513let writableStream = new TestWritable(); 514writableStream.write('data1', 'utf8'); 515writableStream.write('data2', 'utf8'); 516writableStream.uncork(); 517writableStream.end(); 518``` 519 520## ReadableOptions 521 522Readable构造函数的选项信息。 523 524**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 525 526**系统能力:** SystemCapability.Utils.Lang 527 528| 名称 | 类型 | 必填 | 说明 | 529| ---- | -------- | ---- | -------------- | 530| encoding | string | 否 | 指定数据的编码格式,如果传入非法字符串,将会在Readable构造函数中抛出异常。<br/>- 支持格式:utf-8、UTF-8、GBK、GB2312、gb2312、GB18030、gb18030、ibm866、iso-8859-2、iso-8859-3、iso-8859-4、iso-8859-5、iso-8859-6、iso-8859-7、iso-8859-8、iso-8859-8-i、iso-8859-10、iso-8859-13、iso-8859-14、iso-8859-15、koi8-r、koi8-u、macintosh、windows-874、windows-1250、windows-1251、windows-1252、windows-1253、windows-1254、windows-1255、windows-1256、windows-1257、windows-1258、gbk、big5、euc-jp、iso-2022-jp、shift_jis、euc-kr、x-mac-cyrillic、utf-16be、utf-16le。 <br/>- 默认值是:'utf-8'。| 531 532## Readable 533 534表示可读取数据的流。可读流用于从数据源(如文件、网络套接字等)读取数据。 535 536### 属性 537 538**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 539 540**系统能力:** SystemCapability.Utils.Lang 541 542| 名称 | 类型 | 只读| 可选 | 说明 | 543| ------- | -------- | ------ | ------ | ----------- | 544| readableObjectMode | boolean | 是 | 否 | 用于指定可读流是否以对象模式工作。true表示流被配置为对象模式,false表示流处于非对象模式。当前版本只支持原始数据(字符串和Uint8Array),返回值为false。| 545| readable | boolean | 是 | 否 | 表示可读流是否处于可读状态。true表示流处于可读状态,false表示流中没有更多数据可供读取。 | 546| readableHighWatermark | number | 是 | 否 | 定义缓冲区可以存放的最大数据量。默认值为16 * 1024,单位为字节。| 547| readableFlowing | boolean \| null | 是 | 否 | 表示当前可读流的状态。true表示流处于流动模式,false表示流处于非流动模式。| 548| readableLength | number | 是 | 否 | 表示缓冲区的当前字节数。| 549| readableEncoding | string \| null | 是 | 否 | 被解码成字符串时所使用的字符编码。当前版本支持'utf8'、'gb18030'、'gbk'以及'gb2312'。| 550| readableEnded | boolean | 是 | 否 | 表示当前可读流是否已经结束。true表示流已经没有更多数据可读,并且已经结束,false表示流尚未结束,依然有数据可读或等待读取。 | 551 552### constructor 553 554constructor() 555 556Readable的构造函数。 557 558**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 559 560**系统能力:** SystemCapability.Utils.Lang 561 562**示例:** 563 564```ts 565let readableStream = new stream.Readable(); 566``` 567 568### constructor 569 570constructor(options: ReadableOptions) 571 572Readable的构造函数。 573 574**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 575 576**系统能力:** SystemCapability.Utils.Lang 577 578**参数:** 579 580| 参数名 | 类型 | 必填 | 说明 | 581| ------ | -------- | -------- | -------- | 582| options | [ReadableOptions](#readableoptions) | 是 | Readable构造函数的选项信息。| 583 584**错误码:** 585 586以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 587 588| 错误码ID | 错误信息 | 589| -------- | -------- | 590| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 591 592**示例:** 593 594```ts 595let option : stream.ReadableOptions = { 596 encoding : 'utf-8' 597}; 598let readableStream = new stream.Readable(option); 599``` 600 601### read 602 603read(size?: number): string | null 604 605从可读流缓冲区读取数据,并返回读取到的数据,如果未读取到数据,则返回null。 606 607**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 608 609**系统能力:** SystemCapability.Utils.Lang 610 611**参数:** 612 613| 参数名 | 类型 | 必填 | 说明 | 614| ------ | -------- | -------- | -------- | 615| size | number | 否 | 读取数据的字节数。默认为undefined。 | 616 617**返回值:** 618 619| 类型 | 说明 | 620| ------ | ---------------------- | 621| string \| null | 可读流读取出的数据。 | 622 623**错误码:** 624 625以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 626 627| 错误码ID | 错误信息 | 628| -------- | -------- | 629| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 630| 10200038 | The doRead method has not been implemented. | 631 632**示例:** 633 634```ts 635class TestReadable extends stream.Readable { 636 constructor() { 637 super(); 638 } 639 640 doRead(size: number) { 641 } 642} 643 644let readableStream = new TestReadable(); 645readableStream.push('test'); 646readableStream.pause(); 647let dataChunk = readableStream.read(); 648console.info('Readable data is', dataChunk); // Readable data is test 649``` 650 651### resume 652 653resume(): Readable 654 655将流的读取模式从暂停切换到流动模式,可用接口isPaused判断是否切换到流动模式。 656 657**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 658 659**系统能力:** SystemCapability.Utils.Lang 660 661**返回值:** 662 663| 类型 | 说明 | 664| ------ | ---------------------- | 665| [Readable](#readable) | 当前可读流本身。 | 666 667**示例:** 668 669```ts 670class TestReadable extends stream.Readable { 671 constructor() { 672 super(); 673 } 674 675 doRead(size: number) { 676 } 677} 678 679let readableStream = new TestReadable(); 680readableStream.resume(); 681console.info("Readable test resume", !readableStream.isPaused()); // 切换流动模式成功时,此处日志将打印"Readable test resume true" 682``` 683 684### pause 685 686pause(): Readable 687 688将流的读取模式从流动切换到暂停模式,可用接口isPaused判断是否切换到暂停模式。 689 690**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 691 692**系统能力:** SystemCapability.Utils.Lang 693 694**返回值:** 695 696| 类型 | 说明 | 697| ------ | ---------------------- | 698| [Readable](#readable) | 当前可读流本身。 | 699 700**示例:** 701 702```ts 703class TestReadable extends stream.Readable { 704 constructor() { 705 super(); 706 } 707 708 doRead(size: number) { 709 } 710} 711 712let readableStream = new TestReadable(); 713readableStream.pause(); 714console.info("Readable test pause", readableStream.isPaused()); // Readable test pause true 715``` 716 717### setEncoding 718 719setEncoding(encoding?: string): boolean 720 721设置可读流的字符编码。 722 723**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 724 725**系统能力:** SystemCapability.Utils.Lang 726 727**参数:** 728 729| 参数名 | 类型 | 必填 | 说明 | 730| -------- | -------- | -------- | -------- | 731| encoding | string | 否 | 需要设置的字符编码。默认值是'utf8',当前版本支持'utf8'、'gb18030'、'gbk'以及'gb2312'。 | 732 733**返回值:** 734 735| 类型 | 说明 | 736| -------- | -------- | 737| boolean | 返回是否设置成功。true表示设置成功,false表示设置失败。 | 738 739**错误码:** 740 741以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 742 743| 错误码ID | 错误信息 | 744| -------- | -------- | 745| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 746 747**示例:** 748 749```ts 750class TestReadable extends stream.Readable { 751 constructor() { 752 super(); 753 } 754 755 doRead(size: number) { 756 } 757} 758 759let readableStream = new TestReadable(); 760let result = readableStream.setEncoding('utf8'); 761console.info("Readable result", result); // Readable result true 762``` 763 764### isPaused 765 766isPaused(): boolean 767 768检查流是否处于暂停模式,调用[pause()](#pause)后为true,调用[resume()](#resume)为false。 769 770**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 771 772**系统能力:** SystemCapability.Utils.Lang 773 774**返回值:** 775 776| 类型 | 说明 | 777| -------- | -------- | 778| boolean | 返回流是否处于暂停模式。true表示流处于暂停模式,false表示流未处于暂停模式。 | 779 780**示例:** 781 782```ts 783class TestReadable extends stream.Readable { 784 constructor() { 785 super(); 786 } 787 788 doRead(size: number) { 789 } 790} 791 792let readableStream = new TestReadable(); 793console.info("Readable isPaused", readableStream.isPaused()); // Readable isPaused false 794readableStream.pause(); 795console.info("Readable isPaused", readableStream.isPaused()); // Readable isPaused true 796``` 797 798### pipe 799 800pipe(destination: Writable, options?: Object): Writable 801 802将一个可读流与一个可写流连接起来,实现数据的自动传输。 803 804**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 805 806**系统能力:** SystemCapability.Utils.Lang 807 808**参数:** 809 810| 参数名 | 类型 | 必填 | 说明 | 811| -------- | -------- | -------- | -------- | 812| destination | [Writable](#writable) | 是 | 接收数据的可写流。| 813| options | Object | 否 | 预留字段,暂不支持使用。 | 814 815**返回值:** 816 817| 类型 | 说明 | 818| -------- | -------- | 819| [Writable](#writable) | 返回当前可写流对象。 | 820 821**错误码:** 822 823以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 824 825| 错误码ID | 错误信息 | 826| -------- | -------- | 827| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 828 829**示例:** 830 831```ts 832class TestReadable extends stream.Readable { 833 constructor() { 834 super(); 835 } 836 837 doRead(size: number) { 838 readable.push('test'); 839 readable.push(null); 840 } 841} 842 843class TestWritable extends stream.Writable { 844 constructor() { 845 super(); 846 } 847 848 doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) { 849 console.info("Readable test pipe", chunk); // Readable test pipe test 850 callback(); 851 } 852} 853 854let readable = new TestReadable(); 855let writable = new TestWritable(); 856readable.pipe(writable); 857``` 858 859### unpipe 860 861unpipe(destination?: Writable): Readable 862 863从可写流中移除所有或指定的已连接的可读流。 864 865**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 866 867**系统能力:** SystemCapability.Utils.Lang 868 869**参数:** 870 871| 参数名 | 类型 | 必填 | 说明 | 872| -------- | -------- | -------- | -------- | 873| destination | [Writable](#writable) | 否 | 从当前可写流中移除指定的这个可读流。默认为undefined。| 874 875**返回值:** 876 877| 类型 | 说明 | 878| -------- | -------- | 879| [Readable](#readable) | 返回当前可读流对象。 | 880 881**错误码:** 882 883以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 884 885| 错误码ID | 错误信息 | 886| -------- | -------- | 887| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 888 889**示例:** 890 891```ts 892class TestReadable extends stream.Readable { 893 constructor() { 894 super(); 895 } 896 897 doRead(size: number) { 898 readable.push('test'); 899 readable.push(null); 900 } 901} 902 903class TestWritable extends stream.Writable { 904 constructor() { 905 super(); 906 } 907 908 doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) { 909 callback(); 910 } 911} 912 913let readable = new TestReadable(); 914let writable = new TestWritable(); 915readable.pipe(writable); 916readable.unpipe(writable); 917readable.on('data', () => { 918 console.info("Readable test unpipe data event called"); 919}); 920// unpipe成功断开连接之后,data事件将不会触发,不会打印"Readable test unpipe data event called" 921``` 922 923### on 924 925on(event: string, callback: Callback<emitter.EventData>): void 926 927注册事件处理函数来监听可读流上的不同事件。 928 929**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 930 931**系统能力:** SystemCapability.Utils.Lang 932 933**参数:** 934 935| 参数名 | 类型 | 必填 | 说明 | 936| -------- | -------- | -------- | -------- | 937| event | string | 是 | 事件回调类型,支持的事件包括:`'close'` \| `'data' `\|`'end'` \| `'error'`\|`'readable'`\|`'pause'`\|`'resume'` 。<br/>\- `'close'`:完成[push()](#push)调用,传入null值,触发该事件。<br/>\- `'data'`:当流传递给消费者一个数据块时触发该事件。<br/>\- `'end'`:完成[push()](#push)调用,传入null值,触发该事件。<br/>\- `'error'`:流发生异常时触发。<br/>\- `'readable'`:当有可从流中读取的数据时触发该事件。<br/>\- `'pause'`:完成[pause()](#pause)调用,触发该事件。<br/>\- `'resume'`:完成[resume()](#resume)调用,触发该事件。 | 938| callback | Callback\<[emitter.EventData](../apis-basic-services-kit/js-apis-emitter.md#eventdata)\> | 是 | 回调函数,返回事件数据。 | 939 940**错误码:** 941 942以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 943 944| 错误码ID | 错误信息 | 945| -------- | -------- | 946| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 947 948**示例:** 949 950```ts 951class TestReadable extends stream.Readable { 952 constructor() { 953 super(); 954 } 955 956 doRead(size: number) { 957 throw new Error('Simulated error'); 958 } 959} 960 961let readable = new TestReadable(); 962readable.push('test'); 963readable.on('error', () => { 964 console.info("error event called"); // error event called 965}); 966``` 967 968### off 969 970off(event: string, callback?: Callback<emitter.EventData>): void 971 972取消通过[on](#on)注册的事件处理函数。 973 974**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 975 976**系统能力:** SystemCapability.Utils.Lang 977 978**参数:** 979 980| 参数名 | 类型 | 必填 | 说明 | 981| -------- | -------- | -------- | -------- | 982| event | string | 是 | 事件回调类型,支持的事件包括:`'close'` \| `'data' `\|`'end'` \| `'error'`\|`'readable'`\|`'pause'`\|`'resume'` 。<br/>\- `'close'`:完成[push()](#push)调用,传入null值,触发该事件。<br/>\- `'data'`:当流传递给消费者一个数据块时触发该事件。<br/>\- `'end'`:完成[push()](#push)调用,传入null值,触发该事件。<br/>\- `'error'`:流发生异常时触发。<br/>\- `'readable'`:当有可从流中读取的数据时触发该事件。<br/>\- `'pause'`:完成[pause()](#pause)调用,触发该事件。<br/>\- `'resume'`:完成[resume()](#resume)调用,触发该事件。 | 983| callback | Callback\<[emitter.EventData](../apis-basic-services-kit/js-apis-emitter.md#eventdata)\> | 否 | 回调函数。 | 984 985**错误码:** 986 987以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 988 989| 错误码ID | 错误信息 | 990| -------- | -------- | 991| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 992 993**示例:** 994 995```ts 996class TestReadable extends stream.Readable { 997 constructor() { 998 super(); 999 } 1000 1001 doRead(size: number) { 1002 } 1003} 1004 1005let readable = new TestReadable(); 1006 1007function read() { 1008 console.info("read() called"); 1009} 1010 1011readable.setEncoding('utf8'); 1012readable.on('readable', read); 1013readable.off('readable'); 1014readable.push('test'); 1015// off注销对readable事件的监听后,read函数不会被调用,"read() called"也不会被打印 1016``` 1017 1018### doInitialize 1019 1020doInitialize(callback: Function): void 1021 1022使用者实现这个函数,这个函数在可读流第一次使用[on](#on-1)监听时被调用。使用callback异步回调。 1023 1024**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1025 1026**系统能力:** SystemCapability.Utils.Lang 1027 1028**参数:** 1029 1030| 参数名 | 类型 | 必填 | 说明 | 1031| -------- | -------- | -------- | -------- | 1032| callback | Function | 是 | 回调函数。 | 1033 1034**错误码:** 1035 1036以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1037 1038| 错误码ID | 错误信息 | 1039| -------- | -------- | 1040| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1041 1042**示例:** 1043 1044```ts 1045class MyReadable extends stream.Readable { 1046 doInitialize(callback: Function) { 1047 super.doInitialize(callback); 1048 console.info("Readable doInitialize"); // Readable doInitialize 1049} 1050 1051 doRead(size: number) { 1052 } 1053} 1054 1055let myReadable = new MyReadable(); 1056myReadable.on('data', () => { 1057}); 1058``` 1059 1060### doRead 1061 1062doRead(size: number): void 1063 1064数据读取接口,需要在子类中被实现。 1065 1066**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1067 1068**系统能力:** SystemCapability.Utils.Lang 1069 1070**参数:** 1071 1072| 参数名 | 类型 | 必填 | 说明 | 1073| -------- | -------- | -------- | -------- | 1074| size | number | 是 | 读取数据的字节数。 | 1075 1076**错误码:** 1077 1078以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1079 1080| 错误码ID | 错误信息 | 1081| -------- | -------- | 1082| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1083 1084**示例:** 1085 1086```ts 1087class TestReadable extends stream.Readable { 1088 constructor() { 1089 super(); 1090 } 1091 1092 doRead(size: number) { 1093 console.info("doRead called"); // doRead called 1094 } 1095} 1096 1097let readable = new TestReadable(); 1098readable.on('data', () => { 1099}); 1100``` 1101 1102### push 1103 1104push(chunk: Uint8Array | string | null, encoding?: string): boolean 1105 1106将数据推送到可读流缓冲区中。 1107 1108**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1109 1110**系统能力:** SystemCapability.Utils.Lang 1111 1112**参数:** 1113 1114| 参数名 | 类型 | 必填 | 说明 | 1115| -------- | -------- | -------- | -------- | 1116| chunk | Uint8Array \| string \| null | 是 | 读取的数据。 | 1117| encoding | string | 否 | 数据的编码格式。默认值是'utf8',当前版本支持'utf8'、'gb18030'、'gbk'以及'gb2312'。| 1118 1119**返回值:** 1120 1121| 类型 | 说明 | 1122| -------- | -------- | 1123| boolean | 可读流的缓冲区中是否还有空间。true表示缓冲区还有空间,false表示流的内部缓冲区已满。输入null时,固定返回false表示推送结束,没有数据块可推送。 | 1124 1125**错误码:** 1126 1127以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1128 1129| 错误码ID | 错误信息 | 1130| -------- | -------- | 1131| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1132 1133**示例:** 1134 1135```ts 1136class TestReadable extends stream.Readable { 1137 constructor() { 1138 super(); 1139 } 1140 1141 doRead(size: number) { 1142 } 1143} 1144 1145let readable = new TestReadable(); 1146let testData = 'Hello world'; 1147readable.push(testData); 1148console.info("Readable push test", readable.readableLength); // Readable push test 11 1149``` 1150 1151## Duplex 1152 1153双工流是一个同时支持可读和可写能力的流。双工流允许数据在两个方向上进行传输,既可以读取数据,又可以写入数据。 1154Duplex类继承[Readable](#readable),支持Readable中所有的方法。 1155 1156### 属性 1157 1158**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1159 1160**系统能力:** SystemCapability.Utils.Lang 1161 1162| 名称 | 类型 | 只读 | 可选 | 说明 | 1163| ------- | -------- | ------ | ------ | ----------- | 1164| writableObjectMode | boolean | 是 | 否 | 用于指定双工流的写模式是否以对象模式工作。true表示流的写模式被配置为对象模式,false表示流的写模式处于非对象模式。当前版本只支持原始数据(字符串和Uint8Array),返回值为false。 | 1165| writableHighWatermark | number | 是 | 否 | 定义双工流的写模式下缓冲区可以存放的最大数据量。默认值为16 * 1024,单位为字节。| 1166| writable | boolean | 是 | 否 | 表示双工流是否处于可写状态。true表示当前流是可写的,false表示流当前不再接受写入操作。| 1167| writableLength | number | 是 | 否 | 表示双工流缓冲区中待写入的字节数。| 1168| writableCorked | number | 是 | 否 | 表示需要调用uncork()方法的次数,以完全解除双工流的封住状态。| 1169| writableEnded | boolean | 是 | 否 | 表示当前双工流的[end()](#end)是否被调用,该状态不代表数据已经全部写入。true表示[end()](#end)已被调用,false表示[end()](#end)未被调用。| 1170| writableFinished | boolean | 是 | 否 | 表示当前双工流是否处于写入完成状态。true表示当前流处于写入完成状态,false表示当前流写入操作可能还在进行中。| 1171 1172### constructor 1173 1174constructor() 1175 1176Duplex的构造函数。 1177 1178**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1179 1180**系统能力:** SystemCapability.Utils.Lang 1181 1182**示例:** 1183 1184```ts 1185let duplex = new stream.Duplex(); 1186``` 1187 1188### write 1189 1190write(chunk?: string | Uint8Array, encoding?: string, callback?: Function): boolean 1191 1192将数据写入流的缓冲区中。使用callback异步回调。 1193 1194**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1195 1196**系统能力:** SystemCapability.Utils.Lang 1197 1198**参数:** 1199 1200| 参数名 | 类型 | 必填 | 说明 | 1201| ------ | ------ | ---- | -------------------------- | 1202| chunk | string \| Uint8Array | 否 | 需要写入的数据。当前版本不支持输入null、undefined和空字符串。 | 1203| encoding | string | 否 | 字符编码类型。默认值是'utf8',当前版本支持'utf8'、'gb18030'、'gbk'以及'gb2312'。| 1204| callback | Function | 否 | 回调函数。默认不调用。 | 1205 1206**返回值:** 1207 1208| 类型 | 说明 | 1209| ------ | ---------------------- | 1210| boolean | 可写流的缓冲区中是否还有空间。true表示缓冲区还有空间,false表示流的内部缓冲区已满。 | 1211 1212**错误码:** 1213 1214以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 1215 1216| 错误码ID | 错误信息 | 1217| -------- | -------- | 1218| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1219| 10200036 | The stream has been ended. | 1220| 10200037 | The callback is invoked multiple times consecutively. | 1221| 10200039 | The doTransform method has not been implemented for a class that inherits from Transform. | 1222 1223**示例:** 1224 1225```ts 1226class TestDuplex extends stream.Duplex { 1227 constructor() { 1228 super(); 1229 } 1230 1231 doRead(size: number) { 1232 } 1233 1234 doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) { 1235 console.info("duplexStream chunk is", chunk); // duplexStream chunk is test 1236 callback(); 1237 } 1238} 1239 1240let duplexStream = new TestDuplex(); 1241let result = duplexStream.write('test', 'utf8'); 1242console.info("duplexStream result", result); // duplexStream result true 1243``` 1244 1245### end 1246 1247end(chunk?: string | Uint8Array, encoding?: string, callback?: Function): Writable 1248 1249结束双工流的写入操作。如果传入chunk参数,则根据实际运行情况,通过write或者doWrite将其作为最后一块数据写入。其中通过doWrite写入时,encoding参数的合法性检查依赖doWrite。end单独使用(不使用write)并传入chunk参数的情况下,必然通过doWrite写入。使用callback异步回调。 1250 1251**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1252 1253**系统能力:** SystemCapability.Utils.Lang 1254 1255**参数:** 1256 1257| 参数名 | 类型 | 必填 | 说明 | 1258| ------ | ------ | ---- | -------------------------- | 1259| chunk | string \| Uint8Array | 否 | 需要写入的数据。默认为undefined。 | 1260| encoding | string | 否 | 字符编码类型。默认值是'utf8',当前版本支持'utf8'、'gb18030'、'gbk'以及'gb2312'。| 1261| callback | Function | 否 | 回调函数。默认不调用。 | 1262 1263**返回值:** 1264 1265| 类型 | 说明 | 1266| ------ | ---------------------- | 1267| [Writable](#writable) | 返回可写流对象。 | 1268 1269**错误码:** 1270 1271以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 1272 1273| 错误码ID | 错误信息 | 1274| -------- | -------- | 1275| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1276| 10200039 | The doTransform method has not been implemented for a class that inherits from Transform. | 1277 1278**示例:** 1279 1280```ts 1281class TestDuplex extends stream.Duplex { 1282 constructor() { 1283 super(); 1284 } 1285 1286 doRead(size: number) { 1287 } 1288 1289 doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) { 1290 console.info("Duplex chunk is", chunk); // Duplex chunk is test 1291 callback(); 1292 } 1293} 1294 1295let duplexStream = new TestDuplex(); 1296duplexStream.end('test', 'utf8', () => { 1297 console.info("Duplex is end"); // Duplex is end 1298}); 1299``` 1300 1301### setDefaultEncoding 1302 1303setDefaultEncoding(encoding?: string): boolean 1304 1305设置双工流的默认字符编码,以便在读取数据时正确解析字符。 1306 1307**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1308 1309**系统能力:** SystemCapability.Utils.Lang 1310 1311**参数:** 1312 1313| 参数名 | 类型 | 必填 | 说明 | 1314| -------- | -------- | -------- | -------- | 1315| encoding | string | 否 | 需要设置的默认字符编码。默认值是'utf8',当前版本支持'utf8'、'gb18030'、'gbk'以及'gb2312'。| 1316 1317**返回值:** 1318 1319| 类型 | 说明 | 1320| -------- | -------- | 1321| boolean | 返回是否设置成功。true表示设置成功,false表示设置失败。 | 1322 1323**错误码:** 1324 1325以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1326 1327| 错误码ID | 错误信息 | 1328| -------- | -------- | 1329| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1330 1331**示例:** 1332 1333```ts 1334class TestDuplex extends stream.Duplex { 1335 constructor() { 1336 super(); 1337 } 1338 1339 doRead(size: number) { 1340 } 1341 1342 doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) { 1343 callback(); 1344 } 1345} 1346 1347let duplexStream = new TestDuplex(); 1348let result = duplexStream.setDefaultEncoding('utf8'); 1349console.info("duplexStream is result", result); // duplexStream is result true 1350``` 1351 1352### cork 1353 1354cork(): boolean 1355 1356将写入的数据强制写入缓冲区暂存,用来优化连续写入操作的性能。 1357 1358**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1359 1360**系统能力:** SystemCapability.Utils.Lang 1361 1362**返回值:** 1363 1364| 类型 | 说明 | 1365| -------- | -------- | 1366| boolean | 返回设置cork状态是否成功。true表示设置成功,false表示设置失败。 | 1367 1368**示例:** 1369 1370```ts 1371let duplexStream = new stream.Duplex(); 1372let result = duplexStream.cork(); 1373console.info("duplexStream cork result", result); // duplexStream cork result true 1374``` 1375 1376### uncork 1377 1378uncork(): boolean 1379 1380解除cork状态,将缓冲区中的数据全部刷新,并将其写入目标位置。 1381 1382**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1383 1384**系统能力:** SystemCapability.Utils.Lang 1385 1386**返回值:** 1387 1388| 类型 | 说明 | 1389| -------- | -------- | 1390| boolean | 返回解除cork状态是否成功。true表示成功,false表示失败。 | 1391 1392**示例:** 1393 1394```ts 1395class TestDuplex extends stream.Duplex { 1396 constructor() { 1397 super(); 1398 } 1399 1400 doRead(size: number) { 1401 } 1402 1403 doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) { 1404 dataWritten += chunk; 1405 callback(); 1406 } 1407} 1408 1409let dataWritten = ''; 1410let duplexStream = new TestDuplex(); 1411duplexStream.cork(); 1412duplexStream.write('a'); 1413duplexStream.write('b'); 1414duplexStream.uncork(); 1415console.info("Duplex test uncork", dataWritten); // Duplex test uncork ab 1416``` 1417 1418### doWrite 1419 1420doWrite(chunk: string | Uint8Array, encoding: string, callback: Function): void 1421 1422数据写出接口是一个由使用者实现的函数,在数据被写出时自动调用,而不需要用户手动调用。使用callback异步回调。 1423 1424**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1425 1426**系统能力:** SystemCapability.Utils.Lang 1427 1428**参数:** 1429 1430| 参数名 | 类型 | 必填 | 说明 | 1431| ------ | ------ | ---- | -------------------------- | 1432| chunk | string \| Uint8Array | 是 | 要写出的数据。 | 1433| encoding | string | 是 | 字符编码类型。当前版本支持'utf8'、'gb18030'、'gbk'以及'gb2312'。| 1434| callback | Function | 是 | 回调函数。 | 1435 1436**错误码:** 1437 1438以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1439 1440| 错误码ID | 错误信息 | 1441| -------- | -------- | 1442| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1443 1444**示例:** 1445 1446```ts 1447class TestDuplex extends stream.Duplex { 1448 constructor() { 1449 super(); 1450 } 1451 1452 doRead(size: number) { 1453 } 1454 1455 doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) { 1456 console.info("duplexStream chunk is", chunk); // duplexStream chunk is data 1457 callback(); 1458 } 1459} 1460 1461let duplexStream = new TestDuplex(); 1462duplexStream.write('data', 'utf8'); 1463``` 1464 1465### doWritev 1466 1467doWritev(chunks: string[] | Uint8Array[], callback: Function): void 1468 1469数据分批写出接口是一个由使用者实现的函数,在数据被写出时自动调用,而不需要用户手动调用。使用callback异步回调。 1470 1471**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1472 1473**系统能力:** SystemCapability.Utils.Lang 1474 1475**参数:** 1476 1477| 参数名 | 类型 | 必填 | 说明 | 1478| -------- | -------- | -------- | -------- | 1479| chunks | string[] \| Uint8Array[] | 是 | 被批量写出的数据数组。 | 1480| callback | Function | 是 | 回调函数。 | 1481 1482**错误码:** 1483 1484以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1485 1486| 错误码ID | 错误信息 | 1487| -------- | -------- | 1488| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1489 1490**示例:** 1491 1492```ts 1493class TestDuplex extends stream.Duplex { 1494 constructor() { 1495 super(); 1496 } 1497 1498 doRead(size: number) { 1499 } 1500 1501 doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) { 1502 callback(); 1503 } 1504 1505 doWritev(chunks: string[] | Uint8Array[], callback: Function) { 1506 console.info("duplexStream chunk", chunks[0]); // duplexStream chunk data1 1507 callback(); 1508 } 1509} 1510 1511let duplexStream = new TestDuplex(); 1512duplexStream.cork(); 1513duplexStream.write('data1', 'utf8'); 1514duplexStream.write('data2', 'utf8'); 1515duplexStream.uncork(); 1516duplexStream.end(); 1517``` 1518 1519## Transform 1520 1521转换流是一个特殊的双工流,支持可读和可写能力的流,可以对数据进行转换并输出结果。Transform类继承[Duplex](#duplex),支持Duplex中所有的方法。 1522 1523### constructor 1524 1525constructor() 1526 1527Transform的构造函数。 1528 1529**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1530 1531**系统能力:** SystemCapability.Utils.Lang 1532 1533**示例:** 1534 1535```ts 1536let transform = new stream.Transform(); 1537``` 1538 1539### doTransform 1540 1541doTransform(chunk: string, encoding: string, callback: Function): void 1542 1543对输入的数据块进行转换或处理操作,并通过回调函数通知处理完成。 1544 1545**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1546 1547**系统能力:** SystemCapability.Utils.Lang 1548 1549**参数:** 1550 1551| 参数名 | 类型 | 必填 | 说明 | 1552| -------- | -------- | -------- | -------- | 1553| chunk | string | 是 | 需要写入的数据。 | 1554| encoding | string | 是 | 字符编码类型。当前版本支持'utf8'、'gb18030'、'gbk'以及'gb2312'。 | 1555| callback | Function | 是 | 回调函数。 | 1556 1557**错误码:** 1558 1559以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1560 1561| 错误码ID | 错误信息 | 1562| -------- | -------- | 1563| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1564 1565**示例:** 1566 1567```ts 1568class TestTransform extends stream.Transform { 1569 constructor() { 1570 super(); 1571 } 1572 1573 doTransform(chunk: string, encoding: string, callback: Function) { 1574 let stringChunk = chunk.toString().toUpperCase(); 1575 console.info("Transform test doTransform", stringChunk); // Transform test doTransform HELLO 1576 tr.push(stringChunk); 1577 callback(); 1578 } 1579} 1580 1581let tr = new TestTransform(); 1582tr.write("hello"); 1583``` 1584 1585### doFlush 1586 1587doFlush(callback: Function): void 1588 1589该函数会在流结束时被调用,用于处理剩余的数据。使用callback异步回调。 1590 1591**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1592 1593**系统能力:** SystemCapability.Utils.Lang 1594 1595**参数:** 1596 1597| 参数名 | 类型 | 必填 | 说明 | 1598| -------- | -------- | -------- | -------- | 1599| callback | Function | 是 | 回调函数。 | 1600 1601**错误码:** 1602 1603以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1604 1605| 错误码ID | 错误信息 | 1606| -------- | -------- | 1607| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1608 1609**示例:** 1610 1611```ts 1612class TestTransform extends stream.Transform { 1613 constructor() { 1614 super(); 1615 } 1616 1617 doTransform(chunk: string, encoding: string, callback: Function) { 1618 callback(); 1619 } 1620 1621 doFlush(callback: Function) { 1622 callback(null, 'test'); 1623 } 1624} 1625 1626let transform = new TestTransform(); 1627transform.end('my test'); 1628transform.on('data', (data) => { 1629 console.info("data is", data.data); // data is test 1630}); 1631```