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 | 是 | 否 | 定义可写流缓冲区数据量的水位线大小。当前版本不支持开发者自定义修改水位线大小。调用[write()](#write)写入数据后,若缓冲区数据量达到该值,[write()](#write)会返回false。默认值为16 * 1024字节。| 31| writable | boolean | 是 | 否 | 表示可写流是否处于可写状态。true表示流当前是可写的,false表示流当前不再接受写入操作。| 32| writableLength | number | 是 | 否 | 表示可写流缓冲区中待写入的字节数。| 33| writableCorked | number | 是 | 否 | 表示可写流cork状态计数。值大于0时,可写流处于强制写入缓冲区状态;值为0时,该状态解除。使用[cork()](#cork)方法时计数加一,使用[uncork()](#uncork)方法时计数减一,使用[end()](#end)方法时计数清零。| 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结束可写流的写入操作。如果属性writableCorked的值大于0,会置零该值并输出缓冲区剩余数据。如果传入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使后续写入的数据强制写入缓冲区,优化连续写入操作的性能。使用后属性writableCorked的值会加一。建议和[uncork()](#uncork)成对使用。 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状态,解除后刷新缓冲区数据并写入目标位置。使用后属性writableCorked的值会减一,如果该值降为0,则解除cork状态,否则流依然处于cork状态。建议和[cork()](#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表示流处于非流动模式。默认值是true。| 548| readableLength | number | 是 | 否 | 表示缓冲区的当前字节数。| 549| readableEncoding | string \| null | 是 | 否 | 被解码成字符串时所使用的字符编码。默认值是'utf8',当前版本支持'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当缓冲区有数据时,不允许设置字符编码,返回值为false。 723 724**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 725 726**系统能力:** SystemCapability.Utils.Lang 727 728**参数:** 729 730| 参数名 | 类型 | 必填 | 说明 | 731| -------- | -------- | -------- | -------- | 732| encoding | string | 否 | 需要设置的字符编码。默认值是'utf8',当前版本支持'utf8'、'gb18030'、'gbk'以及'gb2312'。 | 733 734**返回值:** 735 736| 类型 | 说明 | 737| -------- | -------- | 738| boolean | 返回是否设置成功。true表示设置成功,false表示设置失败。 | 739 740**错误码:** 741 742以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 743 744| 错误码ID | 错误信息 | 745| -------- | -------- | 746| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 747 748**示例:** 749 750```ts 751class TestReadable extends stream.Readable { 752 constructor() { 753 super(); 754 } 755 756 doRead(size: number) { 757 } 758} 759 760let readableStream = new TestReadable(); 761let result = readableStream.setEncoding('utf8'); 762console.info("Readable result", result); // Readable result true 763``` 764 765### isPaused 766 767isPaused(): boolean 768 769检查流是否处于暂停模式,调用[pause()](#pause)后,返回值为true;调用[resume()](#resume)后,返回值为false。 770 771**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 772 773**系统能力:** SystemCapability.Utils.Lang 774 775**返回值:** 776 777| 类型 | 说明 | 778| -------- | -------- | 779| boolean | 返回流是否处于暂停模式。true表示流处于暂停模式,false表示流未处于暂停模式。 | 780 781**示例:** 782 783```ts 784class TestReadable extends stream.Readable { 785 constructor() { 786 super(); 787 } 788 789 doRead(size: number) { 790 } 791} 792 793let readableStream = new TestReadable(); 794console.info("Readable isPaused", readableStream.isPaused()); // Readable isPaused false 795readableStream.pause(); 796console.info("Readable isPaused", readableStream.isPaused()); // Readable isPaused true 797``` 798 799### pipe 800 801pipe(destination: Writable, options?: Object): Writable 802 803将一个可读流与一个可写流连接起来,实现数据的自动传输。 804 805**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 806 807**系统能力:** SystemCapability.Utils.Lang 808 809**参数:** 810 811| 参数名 | 类型 | 必填 | 说明 | 812| -------- | -------- | -------- | -------- | 813| destination | [Writable](#writable) | 是 | 接收数据的可写流。| 814| options | Object | 否 | 预留字段,暂不支持使用。 | 815 816**返回值:** 817 818| 类型 | 说明 | 819| -------- | -------- | 820| [Writable](#writable) | 返回当前可写流对象。 | 821 822**错误码:** 823 824以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 825 826| 错误码ID | 错误信息 | 827| -------- | -------- | 828| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 829 830**示例:** 831 832```ts 833class TestReadable extends stream.Readable { 834 constructor() { 835 super(); 836 } 837 838 doRead(size: number) { 839 readable.push('test'); 840 readable.push(null); 841 } 842} 843 844class TestWritable extends stream.Writable { 845 constructor() { 846 super(); 847 } 848 849 doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) { 850 console.info("Readable test pipe", chunk); // Readable test pipe test 851 callback(); 852 } 853} 854 855let readable = new TestReadable(); 856let writable = new TestWritable(); 857readable.pipe(writable); 858``` 859 860### unpipe 861 862unpipe(destination?: Writable): Readable 863 864从可写流中移除所有或指定的已连接的可读流。 865 866**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 867 868**系统能力:** SystemCapability.Utils.Lang 869 870**参数:** 871 872| 参数名 | 类型 | 必填 | 说明 | 873| -------- | -------- | -------- | -------- | 874| destination | [Writable](#writable) | 否 | 从当前可写流中移除指定的这个可读流。默认为undefined。| 875 876**返回值:** 877 878| 类型 | 说明 | 879| -------- | -------- | 880| [Readable](#readable) | 返回当前可读流对象。 | 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; 3. Parameter verification failed. | 889 890**示例:** 891 892```ts 893class TestReadable extends stream.Readable { 894 constructor() { 895 super(); 896 } 897 898 doRead(size: number) { 899 readable.push('test'); 900 readable.push(null); 901 } 902} 903 904class TestWritable extends stream.Writable { 905 constructor() { 906 super(); 907 } 908 909 doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) { 910 callback(); 911 } 912} 913 914let readable = new TestReadable(); 915let writable = new TestWritable(); 916readable.pipe(writable); 917readable.unpipe(writable); 918readable.on('data', () => { 919 console.info("Readable test unpipe data event called"); 920}); 921// unpipe成功断开连接之后,data事件将不会触发,不会打印"Readable test unpipe data event called" 922``` 923 924### on 925 926on(event: string, callback: Callback<emitter.EventData>): void 927 928注册事件处理函数来监听可读流上的不同事件。 929 930**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 931 932**系统能力:** SystemCapability.Utils.Lang 933 934**参数:** 935 936| 参数名 | 类型 | 必填 | 说明 | 937| -------- | -------- | -------- | -------- | 938| 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)调用,触发该事件。 | 939| callback | Callback\<[emitter.EventData](../apis-basic-services-kit/js-apis-emitter.md#eventdata)\> | 是 | 回调函数,返回事件数据。 | 940 941**错误码:** 942 943以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 944 945| 错误码ID | 错误信息 | 946| -------- | -------- | 947| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 948 949**示例:** 950 951```ts 952class TestReadable extends stream.Readable { 953 constructor() { 954 super(); 955 } 956 957 doRead(size: number) { 958 throw new Error('Simulated error'); 959 } 960} 961 962let readable = new TestReadable(); 963readable.push('test'); 964readable.on('error', () => { 965 console.info("error event called"); // error event called 966}); 967``` 968 969### off 970 971off(event: string, callback?: Callback<emitter.EventData>): void 972 973移除通过[on](#on)注册的事件处理函数。 974 975**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 976 977**系统能力:** SystemCapability.Utils.Lang 978 979**参数:** 980 981| 参数名 | 类型 | 必填 | 说明 | 982| -------- | -------- | -------- | -------- | 983| 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)调用,触发该事件。 | 984| callback | Callback\<[emitter.EventData](../apis-basic-services-kit/js-apis-emitter.md#eventdata)\> | 否 | 回调函数。 | 985 986**错误码:** 987 988以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 989 990| 错误码ID | 错误信息 | 991| -------- | -------- | 992| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 993 994**示例:** 995 996```ts 997class TestReadable extends stream.Readable { 998 constructor() { 999 super(); 1000 } 1001 1002 doRead(size: number) { 1003 } 1004} 1005 1006let readable = new TestReadable(); 1007 1008function read() { 1009 console.info("read() called"); 1010} 1011 1012readable.setEncoding('utf8'); 1013readable.on('readable', read); 1014readable.off('readable'); 1015readable.push('test'); 1016// off注销对readable事件的监听后,read函数不会被调用,"read() called"也不会被打印 1017``` 1018 1019### doInitialize 1020 1021doInitialize(callback: Function): void 1022 1023使用者实现这个函数,这个函数在可读流第一次使用[on](#on-1)监听时被调用。使用callback异步回调。 1024 1025**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1026 1027**系统能力:** SystemCapability.Utils.Lang 1028 1029**参数:** 1030 1031| 参数名 | 类型 | 必填 | 说明 | 1032| -------- | -------- | -------- | -------- | 1033| callback | Function | 是 | 回调函数。 | 1034 1035**错误码:** 1036 1037以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1038 1039| 错误码ID | 错误信息 | 1040| -------- | -------- | 1041| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1042 1043**示例:** 1044 1045```ts 1046class MyReadable extends stream.Readable { 1047 doInitialize(callback: Function) { 1048 super.doInitialize(callback); 1049 console.info("Readable doInitialize"); // Readable doInitialize 1050} 1051 1052 doRead(size: number) { 1053 } 1054} 1055 1056let myReadable = new MyReadable(); 1057myReadable.on('data', () => { 1058}); 1059``` 1060 1061### doRead 1062 1063doRead(size: number): void 1064 1065数据读取接口,需要在子类中被实现。 1066 1067**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1068 1069**系统能力:** SystemCapability.Utils.Lang 1070 1071**参数:** 1072 1073| 参数名 | 类型 | 必填 | 说明 | 1074| -------- | -------- | -------- | -------- | 1075| size | number | 是 | 读取数据的字节数。 取值范围:0 <= size <= Number.MAX_VALUE。| 1076 1077**错误码:** 1078 1079以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1080 1081| 错误码ID | 错误信息 | 1082| -------- | -------- | 1083| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1084 1085**示例:** 1086 1087```ts 1088class TestReadable extends stream.Readable { 1089 constructor() { 1090 super(); 1091 } 1092 1093 doRead(size: number) { 1094 console.info("doRead called"); // doRead called 1095 } 1096} 1097 1098let readable = new TestReadable(); 1099readable.on('data', () => { 1100}); 1101``` 1102 1103### push 1104 1105push(chunk: Uint8Array | string | null, encoding?: string): boolean 1106 1107将数据推送到可读流缓冲区中。 1108 1109**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1110 1111**系统能力:** SystemCapability.Utils.Lang 1112 1113**参数:** 1114 1115| 参数名 | 类型 | 必填 | 说明 | 1116| -------- | -------- | -------- | -------- | 1117| chunk | Uint8Array \| string \| null | 是 | 读取的数据。 | 1118| encoding | string | 否 | 数据的编码格式。默认值是'utf8',当前版本支持'utf8'、'gb18030'、'gbk'以及'gb2312'。| 1119 1120**返回值:** 1121 1122| 类型 | 说明 | 1123| -------- | -------- | 1124| boolean | 可读流的缓冲区中是否还有空间。true表示缓冲区还有空间,false表示流的内部缓冲区已满。输入null时,固定返回false表示推送结束,没有数据块可推送。 | 1125 1126**错误码:** 1127 1128以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1129 1130| 错误码ID | 错误信息 | 1131| -------- | -------- | 1132| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1133 1134**示例:** 1135 1136```ts 1137class TestReadable extends stream.Readable { 1138 constructor() { 1139 super(); 1140 } 1141 1142 doRead(size: number) { 1143 } 1144} 1145 1146let readable = new TestReadable(); 1147let testData = 'Hello world'; 1148readable.push(testData); 1149console.info("Readable push test", readable.readableLength); // Readable push test 11 1150``` 1151 1152## Duplex 1153 1154双工流是一个同时支持可读和可写能力的流。双工流允许数据在两个方向上进行传输,既可以读取数据,又可以写入数据。 1155Duplex类继承[Readable](#readable),支持Readable中所有的方法。 1156 1157### 属性 1158 1159**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1160 1161**系统能力:** SystemCapability.Utils.Lang 1162 1163| 名称 | 类型 | 只读 | 可选 | 说明 | 1164| ------- | -------- | ------ | ------ | ----------- | 1165| writableObjectMode | boolean | 是 | 否 | 用于指定双工流的写模式是否以对象模式工作。true表示流的写模式被配置为对象模式,false表示流的写模式处于非对象模式。当前版本只支持原始数据(字符串和Uint8Array),返回值为false。 | 1166| writableHighWatermark | number | 是 | 否 | 定义双工流的写模式下缓冲区数据量的水位线大小。当前版本不支持开发者自定义修改设置水位线大小。调用[write()](#write-1)写入后,若缓冲区数据量达到该值,[write()](#write-1)会返回false。默认值为16 * 1024字节。| 1167| writable | boolean | 是 | 否 | 表示双工流是否处于可写状态。true表示当前流是可写的,false表示流当前不再接受写入操作。| 1168| writableLength | number | 是 | 否 | 表示双工流缓冲区中待写入的字节数。| 1169| writableCorked | number | 是 | 否 | 表示双工流cork状态计数。值大于0时,双工流处于强制写入缓冲区状态,值为0时,该状态解除。使用[cork()](#cork-1)方法时计数加一,使用[uncork()](#uncork-1)方法时计数减一,使用[end()](#end-1)方法时计数清零。| 1170| writableEnded | boolean | 是 | 否 | 表示当前双工流的[end()](#end-1)是否被调用,该状态不代表数据已经全部写入。true表示[end()](#end-1)已被调用,false表示[end()](#end-1)未被调用。| 1171| writableFinished | boolean | 是 | 否 | 表示当前双工流是否处于写入完成状态。true表示当前流已处于写入完成状态,false表示当前流的写入操作可能还在进行中。| 1172 1173### constructor 1174 1175constructor() 1176 1177Duplex的构造函数。 1178 1179**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1180 1181**系统能力:** SystemCapability.Utils.Lang 1182 1183**示例:** 1184 1185```ts 1186let duplex = new stream.Duplex(); 1187``` 1188 1189### write 1190 1191write(chunk?: string | Uint8Array, encoding?: string, callback?: Function): boolean 1192 1193将数据写入流的缓冲区中。使用callback异步回调。 1194 1195**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1196 1197**系统能力:** SystemCapability.Utils.Lang 1198 1199**参数:** 1200 1201| 参数名 | 类型 | 必填 | 说明 | 1202| ------ | ------ | ---- | -------------------------- | 1203| chunk | string \| Uint8Array | 否 | 需要写入的数据。当前版本不支持null、undefined和空字符串。 | 1204| encoding | string | 否 | 字符编码类型。默认值是'utf8',当前版本支持'utf8'、'gb18030'、'gbk'以及'gb2312'。| 1205| callback | Function | 否 | 回调函数。默认不调用。 | 1206 1207**返回值:** 1208 1209| 类型 | 说明 | 1210| ------ | ---------------------- | 1211| boolean | 可写流的缓冲区中是否还有空间。true表示缓冲区还有空间,false表示流的内部缓冲区数据量已达到设定水位线,不建议继续写入。 | 1212 1213**错误码:** 1214 1215以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 1216 1217| 错误码ID | 错误信息 | 1218| -------- | -------- | 1219| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1220| 10200036 | The stream has been ended. | 1221| 10200037 | The callback is invoked multiple times consecutively. | 1222| 10200039 | The doTransform method has not been implemented for a class that inherits from Transform. | 1223 1224**示例:** 1225 1226```ts 1227class TestDuplex extends stream.Duplex { 1228 constructor() { 1229 super(); 1230 } 1231 1232 doRead(size: number) { 1233 } 1234 1235 doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) { 1236 console.info("duplexStream chunk is", chunk); // duplexStream chunk is test 1237 callback(); 1238 } 1239} 1240 1241let duplexStream = new TestDuplex(); 1242let result = duplexStream.write('test', 'utf8'); 1243console.info("duplexStream result", result); // duplexStream result true 1244``` 1245 1246### end 1247 1248end(chunk?: string | Uint8Array, encoding?: string, callback?: Function): Writable 1249 1250结束双工流的写入操作。如果属性writableCorked的值大于0,会置零该值并输出缓冲区剩余数据。如果传入chunk参数,则根据实际运行情况,通过write或者doWrite将其作为最后一块数据写入。其中通过doWrite写入时,encoding参数的合法性检查依赖doWrite。end单独使用(不使用write)并传入chunk参数的情况下,必然通过doWrite写入。使用callback异步回调。 1251 1252**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1253 1254**系统能力:** SystemCapability.Utils.Lang 1255 1256**参数:** 1257 1258| 参数名 | 类型 | 必填 | 说明 | 1259| ------ | ------ | ---- | -------------------------- | 1260| chunk | string \| Uint8Array | 否 | 需要写入的数据。默认为undefined。 | 1261| encoding | string | 否 | 字符编码类型。默认值是'utf8',当前版本支持'utf8'、'gb18030'、'gbk'以及'gb2312'。| 1262| callback | Function | 否 | 回调函数。默认不调用。 | 1263 1264**返回值:** 1265 1266| 类型 | 说明 | 1267| ------ | ---------------------- | 1268| [Writable](#writable) | 返回可写流对象。 | 1269 1270**错误码:** 1271 1272以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 1273 1274| 错误码ID | 错误信息 | 1275| -------- | -------- | 1276| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1277| 10200039 | The doTransform method has not been implemented for a class that inherits from Transform. | 1278 1279**示例:** 1280 1281```ts 1282class TestDuplex extends stream.Duplex { 1283 constructor() { 1284 super(); 1285 } 1286 1287 doRead(size: number) { 1288 } 1289 1290 doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) { 1291 console.info("Duplex chunk is", chunk); // Duplex chunk is test 1292 callback(); 1293 } 1294} 1295 1296let duplexStream = new TestDuplex(); 1297duplexStream.end('test', 'utf8', () => { 1298 console.info("Duplex is end"); // Duplex is end 1299}); 1300``` 1301 1302### setDefaultEncoding 1303 1304setDefaultEncoding(encoding?: string): boolean 1305 1306设置双工流的默认字符编码,确保在读取数据时正确解析字符。 1307 1308**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1309 1310**系统能力:** SystemCapability.Utils.Lang 1311 1312**参数:** 1313 1314| 参数名 | 类型 | 必填 | 说明 | 1315| -------- | -------- | -------- | -------- | 1316| encoding | string | 否 | 需要设置的默认字符编码。默认值是'utf8',当前版本支持'utf8'、'gb18030'、'gbk'以及'gb2312'。| 1317 1318**返回值:** 1319 1320| 类型 | 说明 | 1321| -------- | -------- | 1322| boolean | 返回是否设置成功。true表示设置成功,false表示设置失败。 | 1323 1324**错误码:** 1325 1326以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1327 1328| 错误码ID | 错误信息 | 1329| -------- | -------- | 1330| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1331 1332**示例:** 1333 1334```ts 1335class TestDuplex extends stream.Duplex { 1336 constructor() { 1337 super(); 1338 } 1339 1340 doRead(size: number) { 1341 } 1342 1343 doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) { 1344 callback(); 1345 } 1346} 1347 1348let duplexStream = new TestDuplex(); 1349let result = duplexStream.setDefaultEncoding('utf8'); 1350console.info("duplexStream is result", result); // duplexStream is result true 1351``` 1352 1353### cork 1354 1355cork(): boolean 1356 1357将写入的数据强制写入缓冲区暂存,用来优化连续写入操作的性能。使用后属性writableCorked的值会加一。建议和[uncork()](#uncork-1)成对使用。 1358 1359**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1360 1361**系统能力:** SystemCapability.Utils.Lang 1362 1363**返回值:** 1364 1365| 类型 | 说明 | 1366| -------- | -------- | 1367| boolean | 返回设置cork状态是否成功。true表示设置成功,false表示设置失败。 | 1368 1369**示例:** 1370 1371```ts 1372let duplexStream = new stream.Duplex(); 1373let result = duplexStream.cork(); 1374console.info("duplexStream cork result", result); // duplexStream cork result true 1375``` 1376 1377### uncork 1378 1379uncork(): boolean 1380 1381解除cork状态,解除后将缓冲区中的数据全部刷新,并将其写入目标位置。使用后属性writableCorked的值会减一,如果该值降为0,则解除cork状态,否则流依然处于cork状态。建议和[cork()](#cork-1)成对使用。 1382 1383**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1384 1385**系统能力:** SystemCapability.Utils.Lang 1386 1387**返回值:** 1388 1389| 类型 | 说明 | 1390| -------- | -------- | 1391| boolean | 返回解除cork状态是否成功。true表示成功,false表示失败。 | 1392 1393**示例:** 1394 1395```ts 1396class TestDuplex extends stream.Duplex { 1397 constructor() { 1398 super(); 1399 } 1400 1401 doRead(size: number) { 1402 } 1403 1404 doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) { 1405 dataWritten += chunk; 1406 callback(); 1407 } 1408} 1409 1410let dataWritten = ''; 1411let duplexStream = new TestDuplex(); 1412duplexStream.cork(); 1413duplexStream.write('a'); 1414duplexStream.write('b'); 1415duplexStream.uncork(); 1416console.info("Duplex test uncork", dataWritten); // Duplex test uncork ab 1417``` 1418 1419### doWrite 1420 1421doWrite(chunk: string | Uint8Array, encoding: string, callback: Function): void 1422 1423数据写出接口是一个由使用者实现的函数,在数据被写出时自动调用,而不需要用户手动调用。使用callback异步回调。 1424 1425**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1426 1427**系统能力:** SystemCapability.Utils.Lang 1428 1429**参数:** 1430 1431| 参数名 | 类型 | 必填 | 说明 | 1432| ------ | ------ | ---- | -------------------------- | 1433| chunk | string \| Uint8Array | 是 | 要写出的数据。 | 1434| encoding | string | 是 | 字符编码类型。当前版本支持'utf8'、'gb18030'、'gbk'以及'gb2312'。| 1435| callback | Function | 是 | 回调函数。 | 1436 1437**错误码:** 1438 1439以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1440 1441| 错误码ID | 错误信息 | 1442| -------- | -------- | 1443| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1444 1445**示例:** 1446 1447```ts 1448class TestDuplex extends stream.Duplex { 1449 constructor() { 1450 super(); 1451 } 1452 1453 doRead(size: number) { 1454 } 1455 1456 doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) { 1457 console.info("duplexStream chunk is", chunk); // duplexStream chunk is data 1458 callback(); 1459 } 1460} 1461 1462let duplexStream = new TestDuplex(); 1463duplexStream.write('data', 'utf8'); 1464``` 1465 1466### doWritev 1467 1468doWritev(chunks: string[] | Uint8Array[], callback: Function): void 1469 1470数据分批写出接口是一个由使用者实现的函数,在数据被写出时自动调用,而不需要用户手动调用。使用callback异步回调。 1471 1472**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1473 1474**系统能力:** SystemCapability.Utils.Lang 1475 1476**参数:** 1477 1478| 参数名 | 类型 | 必填 | 说明 | 1479| -------- | -------- | -------- | -------- | 1480| chunks | string[] \| Uint8Array[] | 是 | 被批量写出的数据数组。 | 1481| callback | Function | 是 | 回调函数。 | 1482 1483**错误码:** 1484 1485以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1486 1487| 错误码ID | 错误信息 | 1488| -------- | -------- | 1489| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1490 1491**示例:** 1492 1493```ts 1494class TestDuplex extends stream.Duplex { 1495 constructor() { 1496 super(); 1497 } 1498 1499 doRead(size: number) { 1500 } 1501 1502 doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) { 1503 callback(); 1504 } 1505 1506 doWritev(chunks: string[] | Uint8Array[], callback: Function) { 1507 console.info("duplexStream chunk", chunks[0]); // duplexStream chunk data1 1508 callback(); 1509 } 1510} 1511 1512let duplexStream = new TestDuplex(); 1513duplexStream.cork(); 1514duplexStream.write('data1', 'utf8'); 1515duplexStream.write('data2', 'utf8'); 1516duplexStream.uncork(); 1517duplexStream.end(); 1518``` 1519 1520## Transform 1521 1522转换流是一个特殊的双工流,支持可读和可写能力的流,可以对数据进行转换并输出结果。Transform类继承[Duplex](#duplex),支持Duplex中所有的方法。 1523 1524### constructor 1525 1526constructor() 1527 1528Transform的构造函数。 1529 1530**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1531 1532**系统能力:** SystemCapability.Utils.Lang 1533 1534**示例:** 1535 1536```ts 1537let transform = new stream.Transform(); 1538``` 1539 1540### doTransform 1541 1542doTransform(chunk: string, encoding: string, callback: Function): void 1543 1544对输入的数据块进行转换或处理,并通过回调函数通知操作完成。 1545 1546**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1547 1548**系统能力:** SystemCapability.Utils.Lang 1549 1550**参数:** 1551 1552| 参数名 | 类型 | 必填 | 说明 | 1553| -------- | -------- | -------- | -------- | 1554| chunk | string | 是 | 需要写入的数据。 | 1555| encoding | string | 是 | 字符编码类型。当前版本支持'utf8'、'gb18030'、'gbk'以及'gb2312'。 | 1556| callback | Function | 是 | 回调函数。 | 1557 1558**错误码:** 1559 1560以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1561 1562| 错误码ID | 错误信息 | 1563| -------- | -------- | 1564| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1565 1566**示例:** 1567 1568```ts 1569class TestTransform extends stream.Transform { 1570 constructor() { 1571 super(); 1572 } 1573 1574 doTransform(chunk: string, encoding: string, callback: Function) { 1575 let stringChunk = chunk.toString().toUpperCase(); 1576 console.info("Transform test doTransform", stringChunk); // Transform test doTransform HELLO 1577 tr.push(stringChunk); 1578 callback(); 1579 } 1580} 1581 1582let tr = new TestTransform(); 1583tr.write("hello"); 1584``` 1585 1586### doFlush 1587 1588doFlush(callback: Function): void 1589 1590该函数会在流结束时被调用,用于处理剩余的数据。使用callback异步回调。 1591 1592**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1593 1594**系统能力:** SystemCapability.Utils.Lang 1595 1596**参数:** 1597 1598| 参数名 | 类型 | 必填 | 说明 | 1599| -------- | -------- | -------- | -------- | 1600| callback | Function | 是 | 回调函数。 | 1601 1602**错误码:** 1603 1604以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1605 1606| 错误码ID | 错误信息 | 1607| -------- | -------- | 1608| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1609 1610**示例:** 1611 1612```ts 1613class TestTransform extends stream.Transform { 1614 constructor() { 1615 super(); 1616 } 1617 1618 doTransform(chunk: string, encoding: string, callback: Function) { 1619 callback(); 1620 } 1621 1622 doFlush(callback: Function) { 1623 callback(null, 'test'); 1624 } 1625} 1626 1627let transform = new TestTransform(); 1628transform.end('my test'); 1629transform.on('data', (data) => { 1630 console.info("data is", data.data); // data is test 1631}); 1632```