1# @ohos.util.stream (Stream Base Class) 2 3The stream module provides APIs to process basic types of streams. With streams, data is read or written by chunk, instead of being loaded to the memory at a time. 4 5There are four fundamental stream types: writable streams ([Writable](#writable)), readable streams ([Readable](#readable)), duplex streams ([Duplex](#duplex)), and transform streams ([Transform](#transform)). 6 7> **NOTE** 8> 9> The initial APIs of this module are supported since API version 12. Newly added APIs will be marked with a superscript to indicate their earliest API version. 10 11## Modules to Import 12 13```ts 14import { stream } from '@kit.ArkTS'; 15``` 16 17## Writable 18 19Stream to which data can be written. A writable stream allows data to be written to a target, which can be a file, an HTTP response, a standard output, another stream, or the like. 20 21### Attributes 22 23**Atomic service API**: This API can be used in atomic services since API version 12. 24 25**System capability**: SystemCapability.Utils.Lang 26 27| Name | Type | Read-Only| Optional | Description | 28| ------- | -------- | ------ | ------ | ----------- | 29| writableObjectMode | boolean | Yes | No| Whether the writable stream works in object mode. The value **true** means that the stream is configured in object mode, and **false** means the opposite. Currently, only raw data (string and Uint8Array) is supported, and the return value is **false**.| 30| writableHighWatermark | number | Yes| No | High watermark for the data volume in the buffer. This value is not customizable currently. When you call [write()](#write), if the data volume in the buffer reaches this watermark, [write()](#write) will return **false**. The default value is 16 x 1024, in bytes.| 31| writable | boolean | Yes| No | Whether the writable stream is currently writable. The value **true** means that the stream is currently writable, and **false** means that the stream does not accept write operations.| 32| writableLength | number | Yes| No | Number of bytes to be written in the buffer of the writable stream.| 33| writableCorked | number | Yes | No| Count of cork states of the writable stream. If the value is greater than 0, the writable stream buffers all writes. If the value is **0**, buffering stops. You can call [cork()](#cork) to increment the count by one, call [uncork()](#uncork) to decrement the count by one, and call [end()](#end) to clear the count.| 34| writableEnded | boolean | Yes | No| Whether [end()](#end) has been called for the writable stream. This property does not specify whether the data has been flushed. The value **true** means that [end()](#end) has been called, and **false** means the opposite.| 35| writableFinished | boolean | Yes | No| Whether data in the writable stream has been flushed. The value **true** means that data in the stream has been flushed, and **false** means the opposite.| 36 37### constructor 38 39constructor() 40 41A constructor used to create a **Writable** object. 42 43**Atomic service API**: This API can be used in atomic services since API version 12. 44 45**System capability**: SystemCapability.Utils.Lang 46 47**Example** 48 49```ts 50let writableStream = new stream.Writable(); 51``` 52 53### write 54 55write(chunk?: string | Uint8Array, encoding?: string, callback?: Function): boolean 56 57Writes data to the buffer of the stream. This API uses an asynchronous callback to return the result. 58 59**Atomic service API**: This API can be used in atomic services since API version 12. 60 61**System capability**: SystemCapability.Utils.Lang 62 63**Parameters** 64 65| Name| Type | Mandatory| Description | 66| ------ | ------ | ---- | -------------------------- | 67| chunk | string \| Uint8Array | No| Data to write. It cannot be **null**, **undefined**, or an empty string.| 68| encoding | string | No | Encoding format. The default value is **'utf8'**. Currently, **'utf8'**, **'gb18030'**, **'gbk'**, and **'gb2312'** are supported.| 69| callback | Function | No | Callback used to return the result. It is not called by default.| 70 71**Return value** 72 73| Type | Description | 74| ------ | ---------------------- | 75| boolean | Whether there is space in the buffer of the writable stream. The value **true** means that there is still space in the buffer. The value **false** means that the buffer is full, and you are not advised to continue writing data.| 76 77**Error codes** 78 79For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 80 81| ID| Error Message| 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**Example** 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 110Ends the writing process in a writable stream. This API uses an asynchronous callback to return the result. 111 112If the value of **writableCorked** is greater than 0, the value is set to **0** and the remaining data in the buffer is output. 113 114If the **chunk** parameter is passed, it is treated as the final data chunk and written using either the **write** or **doWrite** API, based on the current execution context. 115 116If **doWrite** is used for writing, the validity check of the **encoding** parameter depends on **doWrite**. 117 118If **end** is used alone (without **write**) and the **chunk** parameter is passed, the data is written through **doWrite**. 119 120**Atomic service API**: This API can be used in atomic services since API version 12. 121 122**System capability**: SystemCapability.Utils.Lang 123 124**Parameters** 125 126| Name| Type | Mandatory| Description | 127| ------ | ------ | ---- | -------------------------- | 128| chunk | string \| Uint8Array | No| Data to write. The default value is **undefined**.| 129| encoding | string | No | Encoding format. The default value is **'utf8'**. Currently, **'utf8'**, **'gb18030'**, **'gbk'**, and **'gb2312'** are supported.| 130| callback | Function | No | Callback used to return the result.| 131 132**Return value** 133 134| Type | Description | 135| ------ | ---------------------- | 136| [Writable](#writable) | Current **Writable** object.| 137 138**Error codes** 139 140For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 141 142| ID| Error Message| 143| -------- | -------- | 144| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 145| 10200035 | The doWrite method has not been implemented. | 146 147**Example** 148 149```ts 150class TestWritable extends stream.Writable { 151 constructor() { 152 super(); 153 } 154 155 doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) { 156 console.info("Writable chunk is", chunk); 157 callback(); 158 } 159/** 160 * Writable chunk is test 161 * Writable chunk is finish 162 * */ 163} 164 165let writableStream = new TestWritable(); 166writableStream.write('test', 'utf8'); 167writableStream.end('finish', 'utf8', () => { 168 console.info("Writable is end"); // Writable is end 169}); 170``` 171 172### setDefaultEncoding 173 174setDefaultEncoding(encoding?: string): boolean 175 176Sets the default encoding format for the writable stream. 177 178**Atomic service API**: This API can be used in atomic services since API version 12. 179 180**System capability**: SystemCapability.Utils.Lang 181 182**Parameters** 183 184| Name| Type| Mandatory| Description| 185| -------- | -------- | -------- | -------- | 186| encoding | string | No| Default encoding format. The default value is **'utf8'**. Currently, **'utf8'**, **'gb18030'**, **'gbk'**, and **'gb2312'** are supported.| 187 188**Return value** 189 190| Type| Description| 191| -------- | -------- | 192| boolean | Whether the setting is successful. The value **true** means that the setting is successful, and **false** means the opposite.| 193 194**Error codes** 195 196For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 197 198| ID| Error Message| 199| -------- | -------- | 200| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 201 202**Example** 203 204```ts 205class TestWritable extends stream.Writable { 206 constructor() { 207 super(); 208 } 209 210 doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) { 211 callback(); 212 } 213} 214 215let writableStream = new TestWritable(); 216let result = writableStream.setDefaultEncoding('utf8'); 217console.info("Writable is result", result); // Writable is result true 218``` 219 220### cork 221 222cork(): boolean 223 224Forces all written data to be buffered in memory. This API is called to optimize the performance of continuous write operations. 225 226After this API is called, the value of **writableCorked** is incremented by one. It is recommended that this API be used in pair with [uncork()](#uncork). 227 228**Atomic service API**: This API can be used in atomic services since API version 12. 229 230**System capability**: SystemCapability.Utils.Lang 231 232**Return value** 233 234| Type| Description| 235| -------- | -------- | 236| boolean | Whether the corked status is successfully set. The value **true** means that the setting is successful, and **false** means the opposite.| 237 238**Example** 239 240```ts 241class TestWritable extends stream.Writable { 242 constructor() { 243 super(); 244 } 245 246 doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) { 247 callback(); 248 } 249} 250 251let writableStream = new TestWritable(); 252let result = writableStream.cork(); 253console.info("Writable cork result", result); // Writable cork result true 254``` 255 256### uncork 257 258uncork(): boolean 259 260Flushes all data buffered, and writes the data to the target. 261 262After this API is called, the value of **writableCorked** is decremented by one. If the value reaches **0**, the stream is no longer in the cork state. Otherwise, the stream is still in the cork state. It is recommended that this API be used in pair with [cork()](#cork). 263 264**Atomic service API**: This API can be used in atomic services since API version 12. 265 266**System capability**: SystemCapability.Utils.Lang 267 268**Return value** 269 270| Type| Description| 271| -------- | -------- | 272| boolean | Whether the corked status is successfully removed. The value **true** means that the corked status is successfully removed, and **false** means the opposite.| 273 274**Example** 275 276```ts 277class TestWritable extends stream.Writable { 278 constructor() { 279 super(); 280 } 281 282 doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) { 283 callback(); 284 } 285} 286 287let writableStream = new TestWritable(); 288writableStream.cork(); 289writableStream.write('data1', 'utf8'); 290writableStream.write('data2', 'utf8'); 291writableStream.uncork(); 292writableStream.end(); 293writableStream.on('finish', () => { 294 console.info("all Data is End"); // all Data is End 295}); 296``` 297 298### on 299 300on(event: string, callback: Callback<emitter.EventData>): void 301 302Registers an event processing callback to listen for different events on the writable stream. 303 304**Atomic service API**: This API can be used in atomic services since API version 12. 305 306**System capability**: SystemCapability.Utils.Lang 307 308**Parameters** 309 310| Name| Type| Mandatory| Description| 311| -------- | -------- | -------- | -------- | 312| event | string | Yes| Type of the event. The following events are supported:| `'drain' `\|`'error'` \| <br>\- **'close'**: triggered when the call of [end()](#end) is complete and the write operation ends.<br>\- **'drain'**: triggered when the data in the buffer of the writable stream is cleared.<br>\- **'error'**: triggered when an exception occurs in the writable stream.<br>\- **'finish'**: triggered when all data in the buffer is written to the target.| 313| callback | Callback\<[emitter.EventData](../apis-basic-services-kit/js-apis-emitter.md#eventdata)\> | Yes| Callback function used to return the event data.| 314 315**Error codes** 316 317For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 318 319| ID| Error Message| 320| -------- | -------- | 321| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 322 323**Example** 324 325```ts 326class TestWritable extends stream.Writable { 327 constructor() { 328 super(); 329 } 330 331 doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) { 332 callback(new Error()); 333 } 334} 335 336let callbackCalled = false; 337let writable = new TestWritable(); 338writable.on('error', () => { 339 console.info("Writable event test", callbackCalled.toString()); // Writable event test false 340}); 341writable.write('hello', 'utf8', () => { 342}); 343``` 344 345### off 346 347off(event: string, callback?: Callback<emitter.EventData>): void 348 349Unregisters an event processing callback used to listen for different events on the writable stream. 350 351**Atomic service API**: This API can be used in atomic services since API version 12. 352 353**System capability**: SystemCapability.Utils.Lang 354 355**Parameters** 356 357| Name| Type| Mandatory| Description| 358| -------- | -------- | -------- | -------- | 359| event | string | Yes| Type of the event. The following events are supported:| `'drain' `\|`'error'` \| <br>\- **'close'**: triggered when the call of [end()](#end) is complete and the write operation ends.<br>\- **'drain'**: triggered when the data in the buffer of the writable stream is cleared.<br>\- **'error'**: triggered when an exception occurs in the writable stream.<br>\- **'finish'**: triggered when all data in the buffer is written to the target.| 360| callback | Callback\<[emitter.EventData](../apis-basic-services-kit/js-apis-emitter.md#eventdata)\> | No| Callback function.| 361 362**Error codes** 363 364For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 365 366| ID| Error Message| 367| -------- | -------- | 368| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 369 370**Example** 371 372```ts 373class TestWritable extends stream.Writable { 374 constructor() { 375 super(); 376 } 377 378 doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) { 379 callback(); 380 } 381} 382 383let writableStream = new TestWritable(); 384let testListenerCalled = false; 385let testListener = () => { 386 testListenerCalled = true; 387}; 388writableStream.on('finish', testListener); 389writableStream.off('finish'); 390writableStream.write('test'); 391writableStream.end(); 392setTimeout(() => { 393 console.info("Writable off test", testListenerCalled.toString()); // Writable off test false 394}, 0); 395``` 396 397### doInitialize 398 399doInitialize(callback: Function): void 400 401You need to implement this API but do not call it directly. It is automatically called during the initialization of the writable stream. This API uses an asynchronous callback to return the result. 402 403**Atomic service API**: This API can be used in atomic services since API version 12. 404 405**System capability**: SystemCapability.Utils.Lang 406 407**Parameters** 408 409| Name | Type | Mandatory | Description| 410| -------- | -------- | -------- | -------- | 411| callback | Function | Yes| Callback function.| 412 413**Error codes** 414 415For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 416 417| ID| Error Message| 418| -------- | -------- | 419| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 420 421**Example** 422 423```ts 424class MyWritable extends stream.Writable { 425 doInitialize(callback: Function) { 426 super.doInitialize(callback); 427 console.info("Writable doInitialize"); // Writable doInitialize 428 } 429 430 doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) { 431 super.doWrite(chunk, encoding, callback); 432 } 433} 434 435new MyWritable(); 436``` 437 438### doWrite 439 440doWrite(chunk: string | Uint8Array, encoding: string, callback: Function): void 441 442A data write API. You need to implement this API but do not call it directly. This API is automatically called when data is written. This API uses an asynchronous callback to return the result. 443 444**Atomic service API**: This API can be used in atomic services since API version 12. 445 446**System capability**: SystemCapability.Utils.Lang 447 448**Parameters** 449 450| Name| Type | Mandatory| Description | 451| ------ | ------ | ---- | -------------------------- | 452| chunk | string \| Uint8Array | Yes| Data to write.| 453| encoding | string | Yes | Encoding format. Currently, **'utf8'**, **'gb18030'**, **'gbk'**, and **'gb2312'** are supported.| 454| callback | Function | Yes | Callback function.| 455 456**Error codes** 457 458For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 459 460| ID| Error Message| 461| -------- | -------- | 462| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 463 464**Example** 465 466```ts 467class TestWritable extends stream.Writable { 468 constructor() { 469 super(); 470 } 471 472 doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) { 473 console.info("Writable chunk is", chunk); // Writable chunk is data 474 callback(); 475 } 476} 477 478let writableStream = new TestWritable(); 479writableStream.write('data', 'utf8'); 480``` 481 482### doWritev 483 484doWritev(chunks: string[] | Uint8Array[], callback: Function): void 485 486A batch data write API. You need to implement this API but do not call it directly. This API is automatically called when data is written. This API uses an asynchronous callback to return the result. 487 488**Atomic service API**: This API can be used in atomic services since API version 12. 489 490**System capability**: SystemCapability.Utils.Lang 491 492**Parameters** 493 494| Name| Type| Mandatory| Description| 495| -------- | -------- | -------- | -------- | 496| chunks | string[] \| Uint8Array[] | Yes| Data arrays to write in batches.| 497| callback | Function | Yes| Callback function.| 498 499**Error codes** 500 501For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 502 503| ID| Error Message| 504| -------- | -------- | 505| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 506 507**Example** 508 509```ts 510class TestWritable extends stream.Writable { 511 constructor() { 512 super(); 513 } 514 515 doWritev(chunks: string[] | Uint8Array[], callback: Function) { 516 console.info("Writable chunk", chunks); 517 callback(); 518 } 519/** 520 * Writable chunk data1 521 * Writable chunk data2 522* */ 523} 524 525let writableStream = new TestWritable(); 526writableStream.write('data1', 'utf8'); 527writableStream.write('data2', 'utf8'); 528writableStream.uncork(); 529writableStream.end(); 530``` 531 532## ReadableOptions 533 534Describes the options used in the **Readable** constructor. 535 536**Atomic service API**: This API can be used in atomic services since API version 12. 537 538**System capability**: SystemCapability.Utils.Lang 539 540| Name| Type| Mandatory| Description| 541| ---- | -------- | ---- | -------------- | 542| encoding | string | No| Encoding format. If an invalid string is input, an exception is thrown in the **Readable** constructor.<br>The following formats are supported: 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, and utf-16le.<br>The default value is **'utf-8'**.| 543 544## Readable 545 546Stream from which data can be read. A readable stream is used to read data from a source, such as a file or a network socket. 547 548### Attributes 549 550**Atomic service API**: This API can be used in atomic services since API version 12. 551 552**System capability**: SystemCapability.Utils.Lang 553 554| Name | Type | Read-Only| Optional | Description | 555| ------- | -------- | ------ | ------ | ----------- | 556| readableObjectMode | boolean | Yes | No| Whether the readable stream works in object mode. The value **true** means that the stream is configured in object mode, and **false** means the opposite. Currently, only raw data (string and Uint8Array) is supported, and the return value is **false**.| 557| readable | boolean | Yes| No | Whether the readable stream is currently readable. The value **true** means that the stream is currently readable, and **false** means that no data is available to read from the stream.| 558| readableHighWatermark | number | Yes| No | Maximum amount of data that can be stored in the buffer. The default value is 16 x 1024, in bytes.| 559| readableFlowing | boolean \| null | Yes| No | Whether the readable stream is flowing. The value **true** means that the stream is flowing, and **false** means the opposite.| 560| readableLength | number | Yes| No | Number of bytes in the buffer.| 561| readableEncoding | string \| null | Yes| No | Encoding format. Currently, **'utf8'**, **'gb18030'**, **'gbk'**, and **'gb2312'** are supported.| 562| readableEnded | boolean | Yes | No| Whether the readable stream ends. The value **true** means that the stream has no more data to read, and **false** means the opposite.| 563 564### constructor 565 566constructor() 567 568A constructor used to create a **Readable** object. 569 570**Atomic service API**: This API can be used in atomic services since API version 12. 571 572**System capability**: SystemCapability.Utils.Lang 573 574**Example** 575 576```ts 577let readableStream = new stream.Readable(); 578``` 579 580### constructor 581 582constructor(options: ReadableOptions) 583 584A constructor used to create a **Readable** object. 585 586**Atomic service API**: This API can be used in atomic services since API version 12. 587 588**System capability**: SystemCapability.Utils.Lang 589 590**Parameters** 591 592| Name | Type| Mandatory| Description| 593| ------ | -------- | -------- | -------- | 594| options | [ReadableOptions](#readableoptions) | Yes| Options in the **Readable** constructor.| 595 596**Error codes** 597 598For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 599 600| ID| Error Message| 601| -------- | -------- | 602| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 603 604**Example** 605 606```ts 607let option : stream.ReadableOptions = { 608 encoding : 'utf-8' 609}; 610let readableStream = new stream.Readable(option); 611``` 612 613### read 614 615read(size?: number): string | null 616 617Reads data from the buffer of the readable stream and returns the read data. If no data is read, **null** is returned. 618 619**Atomic service API**: This API can be used in atomic services since API version 12. 620 621**System capability**: SystemCapability.Utils.Lang 622 623**Parameters** 624 625| Name | Type| Mandatory| Description| 626| ------ | -------- | -------- | -------- | 627| size | number | No| Number of bytes to read. The default value is **undefined**.| 628 629**Return value** 630 631| Type | Description | 632| ------ | ---------------------- | 633| string \| null | Data read from the readable stream.| 634 635**Error codes** 636 637For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 638 639| ID| Error Message| 640| -------- | -------- | 641| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 642| 10200038 | The doRead method has not been implemented. | 643 644**Example** 645 646```ts 647class TestReadable extends stream.Readable { 648 constructor() { 649 super(); 650 } 651 652 doRead(size: number) { 653 } 654} 655 656let readableStream = new TestReadable(); 657readableStream.push('test'); 658readableStream.pause(); 659let dataChunk = readableStream.read(); 660console.info('Readable data is', dataChunk); // Readable data is test 661``` 662 663### resume 664 665resume(): Readable 666 667Resumes an explicitly paused readable stream. You can use **isPaused** to check whether the stream is in flowing mode. 668 669**Atomic service API**: This API can be used in atomic services since API version 12. 670 671**System capability**: SystemCapability.Utils.Lang 672 673**Return value** 674 675| Type | Description | 676| ------ | ---------------------- | 677| [Readable](#readable) | Current **Readable** object.| 678 679**Example** 680 681```ts 682class TestReadable extends stream.Readable { 683 constructor() { 684 super(); 685 } 686 687 doRead(size: number) { 688 } 689} 690 691let readableStream = new TestReadable(); 692readableStream.resume(); 693console.info("Readable test resume", !readableStream.isPaused()); // After a successful switching, the log "Readable test resume true" is displayed. 694``` 695 696### pause 697 698pause(): Readable 699 700Pauses the readable stream in flowing mode. You can use **isPaused** to check whether the stream is paused. 701 702**Atomic service API**: This API can be used in atomic services since API version 12. 703 704**System capability**: SystemCapability.Utils.Lang 705 706**Return value** 707 708| Type | Description | 709| ------ | ---------------------- | 710| [Readable](#readable) | Current **Readable** object.| 711 712**Example** 713 714```ts 715class TestReadable extends stream.Readable { 716 constructor() { 717 super(); 718 } 719 720 doRead(size: number) { 721 } 722} 723 724let readableStream = new TestReadable(); 725readableStream.pause(); 726console.info("Readable test pause", readableStream.isPaused()); // Readable test pause true 727``` 728 729### setEncoding 730 731setEncoding(encoding?: string): boolean 732 733Sets an encoding format for the readable stream. 734 735**Atomic service API**: This API can be used in atomic services since API version 12. 736 737**System capability**: SystemCapability.Utils.Lang 738 739**Parameters** 740 741| Name| Type| Mandatory| Description| 742| -------- | -------- | -------- | -------- | 743| encoding | string | No| Encoding format. The default value is **'utf8'**. Currently, **'utf8'**, **'gb18030'**, **'gbk'**, and **'gb2312'** are supported.| 744 745**Return value** 746 747| Type| Description| 748| -------- | -------- | 749| boolean | Whether the setting is successful. The value **true** means that the setting is successful, and **false** means the opposite.| 750 751**Error codes** 752 753For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 754 755| ID| Error Message| 756| -------- | -------- | 757| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 758 759**Example** 760 761```ts 762class TestReadable extends stream.Readable { 763 constructor() { 764 super(); 765 } 766 767 doRead(size: number) { 768 } 769} 770 771let readableStream = new TestReadable(); 772let result = readableStream.setEncoding('utf8'); 773console.info("Readable result", result); // Readable result true 774``` 775 776### isPaused 777 778isPaused(): boolean 779 780Checks whether the readable stream is paused. The stream is paused after [pause()](#pause) is called and resumes from the paused state after [resume()](#resume) is called. 781 782**Atomic service API**: This API can be used in atomic services since API version 12. 783 784**System capability**: SystemCapability.Utils.Lang 785 786**Return value** 787 788| Type| Description| 789| -------- | -------- | 790| boolean | Whether the stream is paused. The value **true** means that the stream is paused, and **false** means the opposite.| 791 792**Example** 793 794```ts 795class TestReadable extends stream.Readable { 796 constructor() { 797 super(); 798 } 799 800 doRead(size: number) { 801 } 802} 803 804let readableStream = new TestReadable(); 805console.info("Readable isPaused", readableStream.isPaused()); // Readable isPaused false 806readableStream.pause(); 807console.info("Readable isPaused", readableStream.isPaused()); // Readable isPaused true 808``` 809 810### pipe 811 812pipe(destination: Writable, options?: Object): Writable 813 814Attaches a writable stream to the readable stream to implement automatic data transmission. 815 816**Atomic service API**: This API can be used in atomic services since API version 12. 817 818**System capability**: SystemCapability.Utils.Lang 819 820**Parameters** 821 822| Name| Type| Mandatory| Description| 823| -------- | -------- | -------- | -------- | 824| destination | [Writable](#writable) | Yes| Writable stream that receives data.| 825| options | Object | No| Reserved.| 826 827**Return value** 828 829| Type| Description| 830| -------- | -------- | 831| [Writable](#writable) | Current **Writable** object.| 832 833**Error codes** 834 835For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 836 837| ID| Error Message| 838| -------- | -------- | 839| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 840 841**Example** 842 843```ts 844class TestReadable extends stream.Readable { 845 constructor() { 846 super(); 847 } 848 849 doRead(size: number) { 850 readable.push('test'); 851 readable.push(null); 852 } 853} 854 855class TestWritable extends stream.Writable { 856 constructor() { 857 super(); 858 } 859 860 doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) { 861 console.info("Readable test pipe", chunk); // Readable test pipe test 862 callback(); 863 } 864} 865 866let readable = new TestReadable(); 867let writable = new TestWritable(); 868readable.pipe(writable); 869``` 870 871### unpipe 872 873unpipe(destination?: Writable): Readable 874 875Detaches a writable stream previously attached to the readable stream. 876 877**Atomic service API**: This API can be used in atomic services since API version 12. 878 879**System capability**: SystemCapability.Utils.Lang 880 881**Parameters** 882 883| Name| Type| Mandatory| Description| 884| -------- | -------- | -------- | -------- | 885| destination | [Writable](#writable) | No| Writable stream to detach. The default value is **undefined**.| 886 887**Return value** 888 889| Type| Description| 890| -------- | -------- | 891| [Readable](#readable) | Current **Readable** object.| 892 893**Error codes** 894 895For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 896 897| ID| Error Message| 898| -------- | -------- | 899| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 900 901**Example** 902 903```ts 904class TestReadable extends stream.Readable { 905 constructor() { 906 super(); 907 } 908 909 doRead(size: number) { 910 readable.push('test'); 911 readable.push(null); 912 } 913} 914 915class TestWritable extends stream.Writable { 916 constructor() { 917 super(); 918 } 919 920 doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) { 921 callback(); 922 } 923} 924 925let readable = new TestReadable(); 926let writable = new TestWritable(); 927readable.pipe(writable); 928readable.unpipe(writable); 929readable.on('data', () => { 930 console.info("Readable test unpipe data event called"); 931}); 932// After successful detaching, the data event is not triggered and "Readable test unpipe data event called" is not printed. 933``` 934 935### on 936 937on(event: string, callback: Callback<emitter.EventData>): void 938 939Registers an event processing callback to listen for different events on the readable stream. 940 941**Atomic service API**: This API can be used in atomic services since API version 12. 942 943**System capability**: SystemCapability.Utils.Lang 944 945**Parameters** 946 947| Name| Type| Mandatory| Description| 948| -------- | -------- | -------- | -------- | 949| event | string | Yes| Type of the event. The following events are supported:| `'data' `\|`'end'` \| `'error'`\|`'readable'`\|`'pause'`\| <br>\- **'close'**: triggered when [push()](#push) is called, with **null** passed in.<br>\- **'data'**: triggered when a data chunk is transferred to a consumer.<br>\- **'end'**: triggered when [push()](#push) is called, with **null** passed in.<br>\- **'error'**: triggered when an exception occurs in the stream.<br>\- **'readable'**: triggered when there is data available to be read from the stream.<br>\- **'pause'**: triggered when [pause()](#pause) is called.<br>\- **'resume'**: triggered when [resume()](#resume) is called.| 950| callback | Callback\<[emitter.EventData](../apis-basic-services-kit/js-apis-emitter.md#eventdata)\> | Yes| Callback function used to return the event data.| 951 952**Error codes** 953 954For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 955 956| ID| Error Message| 957| -------- | -------- | 958| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 959 960**Example** 961 962```ts 963class TestReadable extends stream.Readable { 964 constructor() { 965 super(); 966 } 967 968 doRead(size: number) { 969 throw new Error('Simulated error'); 970 } 971} 972 973let readable = new TestReadable(); 974readable.push('test'); 975readable.on('error', () => { 976 console.info("error event called"); // error event called 977}); 978``` 979 980### off 981 982off(event: string, callback?: Callback<emitter.EventData>): void 983 984Unregisters an event processing callback used to listen for different events on the readable stream. 985 986**Atomic service API**: This API can be used in atomic services since API version 12. 987 988**System capability**: SystemCapability.Utils.Lang 989 990**Parameters** 991 992| Name| Type| Mandatory| Description| 993| -------- | -------- | -------- | -------- | 994| event | string | Yes| Type of the event. The following events are supported:| `'data' `\|`'end'` \| `'error'`\|`'readable'`\|`'pause'`\| <br>\- **'close'**: triggered when [push()](#push) is called, with **null** passed in.<br>\- **'data'**: triggered when a data chunk is transferred to a consumer.<br>\- **'end'**: triggered when [push()](#push) is called, with **null** passed in.<br>\- **'error'**: triggered when an exception occurs in the stream.<br>\- **'readable'**: triggered when there is data available to be read from the stream.<br>\- **'pause'**: triggered when [pause()](#pause) is called.<br>\- **'resume'**: triggered when [resume()](#resume) is called.| 995| callback | Callback\<[emitter.EventData](../apis-basic-services-kit/js-apis-emitter.md#eventdata)\> | No| Callback function.| 996 997**Error codes** 998 999For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1000 1001| ID| Error Message| 1002| -------- | -------- | 1003| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1004 1005**Example** 1006 1007```ts 1008class TestReadable extends stream.Readable { 1009 constructor() { 1010 super(); 1011 } 1012 1013 doRead(size: number) { 1014 } 1015} 1016 1017let readable = new TestReadable(); 1018 1019function read() { 1020 console.info("read() called"); 1021} 1022 1023readable.setEncoding('utf8'); 1024readable.on('readable', read); 1025readable.off('readable'); 1026readable.push('test'); 1027// After off is used to unregister the listening of the readable stream events, the read function is not called and "read() called" is not printed. 1028``` 1029 1030### doInitialize 1031 1032doInitialize(callback: Function): void 1033 1034You need to implement this API. It is called when the readable stream calls [on](#on-1) for the first time. This API uses an asynchronous callback to return the result. 1035 1036**Atomic service API**: This API can be used in atomic services since API version 12. 1037 1038**System capability**: SystemCapability.Utils.Lang 1039 1040**Parameters** 1041 1042| Name | Type | Mandatory | Description| 1043| -------- | -------- | -------- | -------- | 1044| callback | Function | Yes| Callback function.| 1045 1046**Error codes** 1047 1048For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1049 1050| ID| Error Message| 1051| -------- | -------- | 1052| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1053 1054**Example** 1055 1056```ts 1057class MyReadable extends stream.Readable { 1058 doInitialize(callback: Function) { 1059 super.doInitialize(callback); 1060 console.info("Readable doInitialize"); // Readable doInitialize 1061} 1062 1063 doRead(size: number) { 1064 } 1065} 1066 1067let myReadable = new MyReadable(); 1068myReadable.on('data', () => { 1069}); 1070``` 1071 1072### doRead 1073 1074doRead(size: number): void 1075 1076A data read API that needs to be implemented in child classes. 1077 1078**Atomic service API**: This API can be used in atomic services since API version 12. 1079 1080**System capability**: SystemCapability.Utils.Lang 1081 1082**Parameters** 1083 1084| Name | Type | Mandatory | Description| 1085| -------- | -------- | -------- | -------- | 1086| size | number | Yes| Number of bytes to read.| 1087 1088**Error codes** 1089 1090For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1091 1092| ID| Error Message| 1093| -------- | -------- | 1094| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1095 1096**Example** 1097 1098```ts 1099class TestReadable extends stream.Readable { 1100 constructor() { 1101 super(); 1102 } 1103 1104 doRead(size: number) { 1105 console.info("doRead called"); // doRead called 1106 } 1107} 1108 1109let readable = new TestReadable(); 1110readable.on('data', () => { 1111}); 1112``` 1113 1114### push 1115 1116push(chunk: Uint8Array | string | null, encoding?: string): boolean 1117 1118Pushes data into the buffer of the readable stream. 1119 1120**Atomic service API**: This API can be used in atomic services since API version 12. 1121 1122**System capability**: SystemCapability.Utils.Lang 1123 1124**Parameters** 1125 1126| Name | Type | Mandatory | Description| 1127| -------- | -------- | -------- | -------- | 1128| chunk | Uint8Array \| string \| null | Yes| Data to read.| 1129| encoding | string | No| Encoding format. The default value is **'utf8'**. Currently, **'utf8'**, **'gb18030'**, **'gbk'**, and **'gb2312'** are supported.| 1130 1131**Return value** 1132 1133| Type| Description| 1134| -------- | -------- | 1135| boolean | Whether there is space in the buffer of the readable stream. The value **true** means that there is still space in the buffer, and **false** means that the buffer is full. If **null** is passed, **false** is always returned, indicating that no data chunk is available for pushing.| 1136 1137**Error codes** 1138 1139For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1140 1141| ID| Error Message| 1142| -------- | -------- | 1143| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1144 1145**Example** 1146 1147```ts 1148class TestReadable extends stream.Readable { 1149 constructor() { 1150 super(); 1151 } 1152 1153 doRead(size: number) { 1154 } 1155} 1156 1157let readable = new TestReadable(); 1158let testData = 'Hello world'; 1159readable.push(testData); 1160console.info("Readable push test", readable.readableLength); // Readable push test 11 1161``` 1162 1163## Duplex 1164 1165A stream that is both readable and writable. A duplex stream allows data to be transmitted in two directions, that is, data can be read and written. 1166The **Duplex** class inherits from [Readable](#readable) and supports all the APIs in **Readable**. 1167 1168### Attributes 1169 1170**Atomic service API**: This API can be used in atomic services since API version 12. 1171 1172**System capability**: SystemCapability.Utils.Lang 1173 1174| Name | Type | Read-Only| Optional | Description | 1175| ------- | -------- | ------ | ------ | ----------- | 1176| writableObjectMode | boolean | Yes | No| Whether the writable side of the duplex stream works in object mode. The value **true** means that the writable side of the stream is configured in object mode, and **false** means the opposite. Currently, only raw data (string and Uint8Array) is supported, and the return value is **false**.| 1177| writableHighWatermark | number | Yes| No | High watermark for the data volume in the buffer of the duplex stream in write mode. This value is not customizable currently. When you call [write()](#write-1), if the data volume in the buffer reaches this watermark, [write()](#write-1) will return **false**. The default value is 16 x 1024, in bytes.| 1178| writable | boolean | Yes| No | Whether the duplex stream is currently writable. The value **true** means that the stream is currently writable, and **false** means that the stream does not accept write operations.| 1179| writableLength | number | Yes| No | Number of bytes to be written in the buffer of the duplex stream.| 1180| writableCorked | number | Yes | No| Count of cork states of the duplex stream. If the value is greater than 0, the duplex stream buffers all writes. If the value is **0**, buffering stops. You can call [cork()](#cork-1) to increment the count by one, call [uncork()](#uncork-1) to decrement the count by one, and call [end()](#end-1) to clear the count.| 1181| writableEnded | boolean | Yes | No| Whether [end()](#end-1) has been called for the duplex stream. This property does not specify whether the data has been flushed. The value **true** means that [end()](#end-1) has been called, and **false** means the opposite.| 1182| writableFinished | boolean | Yes | No| Whether data in the duplex stream has been flushed. The value **true** means that data in the stream has been flushed, and **false** means the opposite.| 1183 1184### constructor 1185 1186constructor() 1187 1188A constructor used to create a **Duplex** object. 1189 1190**Atomic service API**: This API can be used in atomic services since API version 12. 1191 1192**System capability**: SystemCapability.Utils.Lang 1193 1194**Example** 1195 1196```ts 1197let duplex = new stream.Duplex(); 1198``` 1199 1200### write 1201 1202write(chunk?: string | Uint8Array, encoding?: string, callback?: Function): boolean 1203 1204Writes data to the buffer of the stream. This API uses an asynchronous callback to return the result. 1205 1206**Atomic service API**: This API can be used in atomic services since API version 12. 1207 1208**System capability**: SystemCapability.Utils.Lang 1209 1210**Parameters** 1211 1212| Name| Type | Mandatory| Description | 1213| ------ | ------ | ---- | -------------------------- | 1214| chunk | string \| Uint8Array | No| Data to write. It cannot be **null**, **undefined**, or an empty string.| 1215| encoding | string | No | Encoding format. The default value is **'utf8'**. Currently, **'utf8'**, **'gb18030'**, **'gbk'**, and **'gb2312'** are supported.| 1216| callback | Function | No | Callback used to return the result. It is not called by default.| 1217 1218**Return value** 1219 1220| Type | Description | 1221| ------ | ---------------------- | 1222| boolean | Whether there is space in the buffer of the writable stream. The value **true** means that there is still space in the buffer. The value **false** means that the buffer is full, and you are not advised to continue writing data.| 1223 1224**Error codes** 1225 1226For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 1227 1228| ID| Error Message| 1229| -------- | -------- | 1230| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1231| 10200036 | The stream has been ended. | 1232| 10200037 | The callback is invoked multiple times consecutively. | 1233| 10200039 | The doTransform method has not been implemented for a class that inherits from Transform. | 1234 1235**Example** 1236 1237```ts 1238class TestDuplex extends stream.Duplex { 1239 constructor() { 1240 super(); 1241 } 1242 1243 doRead(size: number) { 1244 } 1245 1246 doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) { 1247 console.info("duplexStream chunk is", chunk); // duplexStream chunk is test 1248 callback(); 1249 } 1250} 1251 1252let duplexStream = new TestDuplex(); 1253let result = duplexStream.write('test', 'utf8'); 1254console.info("duplexStream result", result); // duplexStream result true 1255``` 1256 1257### end 1258 1259end(chunk?: string | Uint8Array, encoding?: string, callback?: Function): Writable 1260 1261Ends the writing process in a duplex stream. This API uses an asynchronous callback to return the result. 1262 1263If the value of **writableCorked** is greater than 0, the value is set to **0** and the remaining data in the buffer is output. 1264 1265If the **chunk** parameter is passed, it is treated as the final data chunk and written using either the **write** or **doWrite** API, based on the current execution context. 1266 1267If **doWrite** is used for writing, the validity check of the **encoding** parameter depends on **doWrite**. 1268 1269If **end** is used alone (without **write**) and the **chunk** parameter is passed, the data is written through **doWrite**. 1270 1271**Atomic service API**: This API can be used in atomic services since API version 12. 1272 1273**System capability**: SystemCapability.Utils.Lang 1274 1275**Parameters** 1276 1277| Name| Type | Mandatory| Description | 1278| ------ | ------ | ---- | -------------------------- | 1279| chunk | string \| Uint8Array | No| Data to write. The default value is **undefined**.| 1280| encoding | string | No | Encoding format. The default value is **'utf8'**. Currently, **'utf8'**, **'gb18030'**, **'gbk'**, and **'gb2312'** are supported.| 1281| callback | Function | No | Callback used to return the result. It is not called by default.| 1282 1283**Return value** 1284 1285| Type | Description | 1286| ------ | ---------------------- | 1287| [Writable](#writable) | Current **Duplex** object.| 1288 1289**Error codes** 1290 1291For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 1292 1293| ID| Error Message| 1294| -------- | -------- | 1295| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1296| 10200039 | The doTransform method has not been implemented for a class that inherits from Transform. | 1297 1298**Example** 1299 1300```ts 1301class TestDuplex extends stream.Duplex { 1302 constructor() { 1303 super(); 1304 } 1305 1306 doRead(size: number) { 1307 } 1308 1309 doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) { 1310 console.info("Duplex chunk is", chunk); // Duplex chunk is test 1311 callback(); 1312 } 1313} 1314 1315let duplexStream = new TestDuplex(); 1316duplexStream.end('test', 'utf8', () => { 1317 console.info("Duplex is end"); // Duplex is end 1318}); 1319``` 1320 1321### setDefaultEncoding 1322 1323setDefaultEncoding(encoding?: string): boolean 1324 1325Sets the default encoding format for the duplex stream so that characters can be correctly parsed when data is read. 1326 1327**Atomic service API**: This API can be used in atomic services since API version 12. 1328 1329**System capability**: SystemCapability.Utils.Lang 1330 1331**Parameters** 1332 1333| Name| Type| Mandatory| Description| 1334| -------- | -------- | -------- | -------- | 1335| encoding | string | No| Encoding format. The default value is **'utf8'**. Currently, **'utf8'**, **'gb18030'**, **'gbk'**, and **'gb2312'** are supported.| 1336 1337**Return value** 1338 1339| Type| Description| 1340| -------- | -------- | 1341| boolean | Whether the setting is successful. The value **true** means that the setting is successful, and **false** means the opposite.| 1342 1343**Error codes** 1344 1345For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1346 1347| ID| Error Message| 1348| -------- | -------- | 1349| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1350 1351**Example** 1352 1353```ts 1354class TestDuplex extends stream.Duplex { 1355 constructor() { 1356 super(); 1357 } 1358 1359 doRead(size: number) { 1360 } 1361 1362 doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) { 1363 callback(); 1364 } 1365} 1366 1367let duplexStream = new TestDuplex(); 1368let result = duplexStream.setDefaultEncoding('utf8'); 1369console.info("duplexStream is result", result); // duplexStream is result true 1370``` 1371 1372### cork 1373 1374cork(): boolean 1375 1376Forces all written data to be buffered in memory. This API is called to optimize the performance of continuous write operations. 1377 1378After this API is called, the value of **writableCorked** is incremented by one. It is recommended that this API be used in pair with [uncork()](#uncork-1). 1379 1380**Atomic service API**: This API can be used in atomic services since API version 12. 1381 1382**System capability**: SystemCapability.Utils.Lang 1383 1384**Return value** 1385 1386| Type| Description| 1387| -------- | -------- | 1388| boolean | Whether the corked status is successfully set. The value **true** means that the setting is successful, and **false** means the opposite.| 1389 1390**Example** 1391 1392```ts 1393let duplexStream = new stream.Duplex(); 1394let result = duplexStream.cork(); 1395console.info("duplexStream cork result", result); // duplexStream cork result true 1396``` 1397 1398### uncork 1399 1400uncork(): boolean 1401 1402Flushes all data buffered, and writes the data to the target. 1403 1404After this API is called, the value of **writableCorked** is decremented by one. If the value reaches **0**, the stream is no longer in the cork state. Otherwise, the stream is still in the cork state. It is recommended that this API be used in pair with [cork()](#cork-1). 1405 1406**Atomic service API**: This API can be used in atomic services since API version 12. 1407 1408**System capability**: SystemCapability.Utils.Lang 1409 1410**Return value** 1411 1412| Type| Description| 1413| -------- | -------- | 1414| boolean | Whether the corked status is successfully removed. The value **true** means that the corked status is successfully removed, and **false** means the opposite.| 1415 1416**Example** 1417 1418```ts 1419class TestDuplex extends stream.Duplex { 1420 constructor() { 1421 super(); 1422 } 1423 1424 doRead(size: number) { 1425 } 1426 1427 doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) { 1428 dataWritten += chunk; 1429 callback(); 1430 } 1431} 1432 1433let dataWritten = ''; 1434let duplexStream = new TestDuplex(); 1435duplexStream.cork(); 1436duplexStream.write('a'); 1437duplexStream.write('b'); 1438duplexStream.uncork(); 1439console.info("Duplex test uncork", dataWritten); // Duplex test uncork ab 1440``` 1441 1442### doWrite 1443 1444doWrite(chunk: string | Uint8Array, encoding: string, callback: Function): void 1445 1446A data write API. You need to implement this API but do not call it directly. This API is automatically called when data is written. This API uses an asynchronous callback to return the result. 1447 1448**Atomic service API**: This API can be used in atomic services since API version 12. 1449 1450**System capability**: SystemCapability.Utils.Lang 1451 1452**Parameters** 1453 1454| Name| Type | Mandatory| Description | 1455| ------ | ------ | ---- | -------------------------- | 1456| chunk | string \| Uint8Array | Yes| Data to write.| 1457| encoding | string | Yes | Encoding format. Currently, **'utf8'**, **'gb18030'**, **'gbk'**, and **'gb2312'** are supported.| 1458| callback | Function | Yes | Callback function.| 1459 1460**Error codes** 1461 1462For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1463 1464| ID| Error Message| 1465| -------- | -------- | 1466| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1467 1468**Example** 1469 1470```ts 1471class TestDuplex extends stream.Duplex { 1472 constructor() { 1473 super(); 1474 } 1475 1476 doRead(size: number) { 1477 } 1478 1479 doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) { 1480 console.info("duplexStream chunk is", chunk); // duplexStream chunk is data 1481 callback(); 1482 } 1483} 1484 1485let duplexStream = new TestDuplex(); 1486duplexStream.write('data', 'utf8'); 1487``` 1488 1489### doWritev 1490 1491doWritev(chunks: string[] | Uint8Array[], callback: Function): void 1492 1493A batch data write API. You need to implement this API but do not call it directly. This API is automatically called when data is written. This API uses an asynchronous callback to return the result. 1494 1495**Atomic service API**: This API can be used in atomic services since API version 12. 1496 1497**System capability**: SystemCapability.Utils.Lang 1498 1499**Parameters** 1500 1501| Name| Type| Mandatory| Description| 1502| -------- | -------- | -------- | -------- | 1503| chunks | string[] \| Uint8Array[] | Yes| Data arrays to write in batches.| 1504| callback | Function | Yes| Callback function.| 1505 1506**Error codes** 1507 1508For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1509 1510| ID| Error Message| 1511| -------- | -------- | 1512| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1513 1514**Example** 1515 1516```ts 1517class TestDuplex extends stream.Duplex { 1518 constructor() { 1519 super(); 1520 } 1521 1522 doRead(size: number) { 1523 } 1524 1525 doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) { 1526 callback(); 1527 } 1528 1529 doWritev(chunks: string[] | Uint8Array[], callback: Function) { 1530 console.info("duplexStream chunk", chunks[0]); // duplexStream chunk data1 1531 callback(); 1532 } 1533} 1534 1535let duplexStream = new TestDuplex(); 1536duplexStream.cork(); 1537duplexStream.write('data1', 'utf8'); 1538duplexStream.write('data2', 'utf8'); 1539duplexStream.uncork(); 1540duplexStream.end(); 1541``` 1542 1543## Transform 1544 1545A special duplex stream that supports data conversion and result output. The **Transform** class inherits from [Duplex](#duplex) and supports all the APIs in **Duplex**. 1546 1547### constructor 1548 1549constructor() 1550 1551A constructor used to create a **Transform** object. 1552 1553**Atomic service API**: This API can be used in atomic services since API version 12. 1554 1555**System capability**: SystemCapability.Utils.Lang 1556 1557**Example** 1558 1559```ts 1560let transform = new stream.Transform(); 1561``` 1562 1563### doTransform 1564 1565doTransform(chunk: string, encoding: string, callback: Function): void 1566 1567Converts or processes input data chunks and uses a callback to notify that the processing is complete. 1568 1569**Atomic service API**: This API can be used in atomic services since API version 12. 1570 1571**System capability**: SystemCapability.Utils.Lang 1572 1573**Parameters** 1574 1575| Name | Type | Mandatory | Description| 1576| -------- | -------- | -------- | -------- | 1577| chunk | string | Yes| Data to write.| 1578| encoding | string | Yes | Encoding format. Currently, **'utf8'**, **'gb18030'**, **'gbk'**, and **'gb2312'** are supported.| 1579| callback | Function | Yes | Callback function.| 1580 1581**Error codes** 1582 1583For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1584 1585| ID| Error Message| 1586| -------- | -------- | 1587| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1588 1589**Example** 1590 1591```ts 1592class TestTransform extends stream.Transform { 1593 constructor() { 1594 super(); 1595 } 1596 1597 doTransform(chunk: string, encoding: string, callback: Function) { 1598 let stringChunk = chunk.toString().toUpperCase(); 1599 console.info("Transform test doTransform", stringChunk); // Transform test doTransform HELLO 1600 tr.push(stringChunk); 1601 callback(); 1602 } 1603} 1604 1605let tr = new TestTransform(); 1606tr.write("hello"); 1607``` 1608 1609### doFlush 1610 1611doFlush(callback: Function): void 1612 1613Called at the end of the stream to process the remaining data. This API uses an asynchronous callback to return the result. 1614 1615**Atomic service API**: This API can be used in atomic services since API version 12. 1616 1617**System capability**: SystemCapability.Utils.Lang 1618 1619**Parameters** 1620 1621| Name | Type | Mandatory | Description| 1622| -------- | -------- | -------- | -------- | 1623| callback | Function | Yes | Callback function.| 1624 1625**Error codes** 1626 1627For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1628 1629| ID| Error Message| 1630| -------- | -------- | 1631| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1632 1633**Example** 1634 1635```ts 1636class TestTransform extends stream.Transform { 1637 constructor() { 1638 super(); 1639 } 1640 1641 doTransform(chunk: string, encoding: string, callback: Function) { 1642 callback(); 1643 } 1644 1645 doFlush(callback: Function) { 1646 callback(null, 'test'); 1647 } 1648} 1649 1650let transform = new TestTransform(); 1651transform.end('my test'); 1652transform.on('data', (data) => { 1653 console.info("data is", data.data); // data is test 1654}); 1655``` 1656