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 | Maximum amount of data that can be stored in the buffer. 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 readable stream.|
33| writableCorked | number | Yes | No| Number of times the **uncork()** API needs to be called in order to fully uncork the writable stream.|
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. The default value is **undefined**.|
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, and **false** means that the buffer is full.|
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 writable stream. If the **chunk** parameter is passed in, it is written as the last data chunk. This API uses an asynchronous callback to return the result.
111
112**Atomic service API**: This API can be used in atomic services since API version 12.
113
114**System capability**: SystemCapability.Utils.Lang
115
116**Parameters**
117
118| Name| Type  | Mandatory| Description                      |
119| ------ | ------ | ---- | -------------------------- |
120| chunk  | string \| Uint8Array | No| Data to write. The default value is **undefined**.|
121| encoding  | string | No  | Encoding format. The default value is **'utf8'**. Currently, **'utf8'**, **'gb18030'**, **'gbk'**, and **'gb2312'** are supported.|
122| callback  | Function | No  | Callback used to return the result.|
123
124**Return value**
125
126| Type  | Description                  |
127| ------ | ---------------------- |
128| [Writable](#writable) | Current **Writable** object.|
129
130**Error codes**
131
132For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
133
134| ID| Error Message|
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**Example**
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
168Sets the default encoding format for the writable stream.
169
170**Atomic service API**: This API can be used in atomic services since API version 12.
171
172**System capability**: SystemCapability.Utils.Lang
173
174**Parameters**
175
176| Name| Type| Mandatory| Description|
177| -------- | -------- | -------- | -------- |
178| encoding | string | No| Default encoding format. The default value is **'utf8'**. Currently, **'utf8'**, **'gb18030'**, **'gbk'**, and **'gb2312'** are supported.|
179
180**Return value**
181
182| Type| Description|
183| -------- | -------- |
184| boolean | Whether the setting is successful. The value **true** means that the setting is successful, and **false** means the opposite.|
185
186**Error codes**
187
188For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
189
190| ID| Error Message|
191| -------- | -------- |
192| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
193
194**Example**
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
216Forces all written data to be buffered in memory. This API is called to optimize the performance of continuous write operations.
217
218**Atomic service API**: This API can be used in atomic services since API version 12.
219
220**System capability**: SystemCapability.Utils.Lang
221
222**Return value**
223
224| Type| Description|
225| -------- | -------- |
226| boolean | Whether the corked status is successfully set. The value **true** means that the setting is successful, and **false** means the opposite.|
227
228**Example**
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
250Flushes all data buffered, and writes the data to the target.
251
252**Atomic service API**: This API can be used in atomic services since API version 12.
253
254**System capability**: SystemCapability.Utils.Lang
255
256**Return value**
257
258| Type| Description|
259| -------- | -------- |
260| boolean | Whether the corked status is successfully removed. The value **true** means that the corked status is successfully removed, and **false** means the opposite.|
261
262**Example**
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
290Registers an event processing callback to listen for different events on the writable stream.
291
292**Atomic service API**: This API can be used in atomic services since API version 12.
293
294**System capability**: SystemCapability.Utils.Lang
295
296**Parameters**
297
298| Name| Type| Mandatory| Description|
299| -------- | -------- | -------- | -------- |
300| 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 reaches **writableHighWatermark**.<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.|
301| callback | Callback\<[emitter.EventData](../apis-basic-services-kit/js-apis-emitter.md#eventdata)\> | Yes| Callback function used to return the event data.|
302
303**Error codes**
304
305For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
306
307| ID| Error Message|
308| -------- | -------- |
309| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
310
311**Example**
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
337Unregisters an event processing callback used to listen for different events on the writable stream.
338
339**Atomic service API**: This API can be used in atomic services since API version 12.
340
341**System capability**: SystemCapability.Utils.Lang
342
343**Parameters**
344
345| Name| Type| Mandatory| Description|
346| -------- | -------- | -------- | -------- |
347| 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 reaches **writableHighWatermark**.<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.|
348| callback | string   | No| Callback function.|
349
350**Error codes**
351
352For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
353
354| ID| Error Message|
355| -------- | -------- |
356| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
357
358**Example**
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
389You 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.
390
391**Atomic service API**: This API can be used in atomic services since API version 12.
392
393**System capability**: SystemCapability.Utils.Lang
394
395**Parameters**
396
397| Name   | Type    | Mandatory    | Description|
398| -------- | -------- | -------- | -------- |
399| callback | Function | Yes| Callback function.|
400
401**Error codes**
402
403For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
404
405| ID| Error Message|
406| -------- | -------- |
407| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
408
409**Example**
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
430A 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.
431
432**Atomic service API**: This API can be used in atomic services since API version 12.
433
434**System capability**: SystemCapability.Utils.Lang
435
436**Parameters**
437
438| Name| Type  | Mandatory| Description                      |
439| ------ | ------ | ---- | -------------------------- |
440| chunk  | string \| Uint8Array | Yes| Data to write.|
441| encoding  | string | Yes  | Encoding format. Currently, **'utf8'**, **'gb18030'**, **'gbk'**, and **'gb2312'** are supported.|
442| callback  | Function | Yes  | Callback function.|
443
444**Error codes**
445
446For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
447
448| ID| Error Message|
449| -------- | -------- |
450| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
451
452**Example**
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
474A 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.
475
476**Atomic service API**: This API can be used in atomic services since API version 12.
477
478**System capability**: SystemCapability.Utils.Lang
479
480**Parameters**
481
482| Name| Type| Mandatory| Description|
483| -------- | -------- | -------- | -------- |
484| chunks    | string[] \|  Uint8Array[] | Yes| Data arrays to write in batches.|
485| callback  | Function | Yes| Callback function.|
486
487**Error codes**
488
489For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
490
491| ID| Error Message|
492| -------- | -------- |
493| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
494
495**Example**
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
522Describes the options used in the **Readable** constructor.
523
524**Atomic service API**: This API can be used in atomic services since API version 12.
525
526**System capability**: SystemCapability.Utils.Lang
527
528| Name| Type| Mandatory| Description|
529| ---- | -------- | ---- | -------------- |
530| 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'**.|
531
532## Readable
533
534Stream 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.
535
536### Attributes
537
538**Atomic service API**: This API can be used in atomic services since API version 12.
539
540**System capability**: SystemCapability.Utils.Lang
541
542| Name   | Type     | Read-Only| Optional | Description       |
543| ------- | -------- | ------ | ------ | ----------- |
544| 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**.|
545| 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.|
546| readableHighWatermark | number | Yes| No | Maximum amount of data that can be stored in the buffer. The default value is 16 x 1024, in bytes.|
547| 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.|
548| readableLength | number | Yes| No | Number of bytes in the buffer.|
549| readableEncoding | string \| null | Yes| No | Encoding format. Currently, **'utf8'**, **'gb18030'**, **'gbk'**, and **'gb2312'** are supported.|
550| 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.|
551
552### constructor
553
554constructor()
555
556A constructor used to create a **Readable** object.
557
558**Atomic service API**: This API can be used in atomic services since API version 12.
559
560**System capability**: SystemCapability.Utils.Lang
561
562**Example**
563
564```ts
565let readableStream = new stream.Readable();
566```
567
568### constructor
569
570constructor(options: ReadableOptions)
571
572A constructor used to create a **Readable** object.
573
574**Atomic service API**: This API can be used in atomic services since API version 12.
575
576**System capability**: SystemCapability.Utils.Lang
577
578**Parameters**
579
580| Name | Type| Mandatory| Description|
581| ------ | -------- | -------- | -------- |
582| options   | [ReadableOptions](#readableoptions)   | Yes| Options in the **Readable** constructor.|
583
584**Error codes**
585
586For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
587
588| ID| Error Message|
589| -------- | -------- |
590| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
591
592**Example**
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
605Reads data from the buffer of the readable stream and returns the read data. If no data is read, **null** is returned.
606
607**Atomic service API**: This API can be used in atomic services since API version 12.
608
609**System capability**: SystemCapability.Utils.Lang
610
611**Parameters**
612
613| Name | Type| Mandatory| Description|
614| ------ | -------- | -------- | -------- |
615| size   | number   | No| Number of bytes to read. The default value is **undefined**.|
616
617**Return value**
618
619| Type  | Description                  |
620| ------ | ---------------------- |
621| string \| null | Data read from the readable stream.|
622
623**Error codes**
624
625For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
626
627| ID| Error Message|
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**Example**
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
655Resumes an explicitly paused readable stream.
656
657**Atomic service API**: This API can be used in atomic services since API version 12.
658
659**System capability**: SystemCapability.Utils.Lang
660
661**Return value**
662
663| Type  | Description                  |
664| ------ | ---------------------- |
665| [Readable](#readable) | Current **Readable** object.|
666
667**Example**
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 false
682```
683
684### pause
685
686pause(): Readable
687
688Pauses the readable stream in flowing mode.
689
690**Atomic service API**: This API can be used in atomic services since API version 12.
691
692**System capability**: SystemCapability.Utils.Lang
693
694**Return value**
695
696| Type  | Description                  |
697| ------ | ---------------------- |
698| [Readable](#readable) | Current **Readable** object.|
699
700**Example**
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
721Sets an encoding format for the readable stream.
722
723**Atomic service API**: This API can be used in atomic services since API version 12.
724
725**System capability**: SystemCapability.Utils.Lang
726
727**Parameters**
728
729| Name| Type| Mandatory| Description|
730| -------- | -------- | -------- | -------- |
731| encoding | string | No| Encoding format. The default value is **'utf8'**. Currently, **'utf8'**, **'gb18030'**, **'gbk'**, and **'gb2312'** are supported.|
732
733**Return value**
734
735| Type| Description|
736| -------- | -------- |
737| boolean | Whether the setting is successful. The value **true** means that the setting is successful, and **false** means the opposite.|
738
739**Error codes**
740
741For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
742
743| ID| Error Message|
744| -------- | -------- |
745| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
746
747**Example**
748
749```ts
750class TestReadable extends stream.Readable {
751  constructor() {
752    super();
753  }
754
755  doRead(size: number) {
756  }
757}
758
759let readableStream = new TestReadable();
760let result = readableStream.setEncoding('utf8');
761console.info("Readable result", result); // Readable result true
762```
763
764### isPaused
765
766isPaused(): boolean
767
768Checks 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.
769
770**Atomic service API**: This API can be used in atomic services since API version 12.
771
772**System capability**: SystemCapability.Utils.Lang
773
774**Return value**
775
776| Type| Description|
777| -------- | -------- |
778| boolean | Whether the stream is paused. The value **true** means that the stream is paused, and **false** means the opposite.|
779
780**Example**
781
782```ts
783class TestReadable extends stream.Readable {
784  constructor() {
785    super();
786  }
787
788  doRead(size: number) {
789  }
790}
791
792let readableStream = new TestReadable();
793console.info("Readable isPaused", readableStream.isPaused()); // Readable isPaused false
794readableStream.pause();
795console.info("Readable isPaused", readableStream.isPaused()); // Readable isPaused true
796```
797
798### pipe
799
800pipe(destination: Writable, options?: Object): Writable
801
802Attaches a writable stream to the readable stream to implement automatic data transmission.
803
804**Atomic service API**: This API can be used in atomic services since API version 12.
805
806**System capability**: SystemCapability.Utils.Lang
807
808**Parameters**
809
810| Name| Type| Mandatory| Description|
811| -------- | -------- | -------- | -------- |
812| destination | [Writable](#writable) | Yes| Writable stream that receives data.|
813| options     | Object | No| Reserved.|
814
815**Return value**
816
817| Type| Description|
818| -------- | -------- |
819| [Writable](#writable) | Current **Writable** object.|
820
821**Error codes**
822
823For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
824
825| ID| Error Message|
826| -------- | -------- |
827| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
828
829**Example**
830
831```ts
832class TestReadable extends stream.Readable {
833  constructor() {
834    super();
835  }
836
837  doRead(size: number) {
838    readable.push('test');
839    readable.push(null);
840  }
841}
842
843class TestWritable extends stream.Writable {
844  constructor() {
845    super();
846  }
847
848  doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) {
849    console.info("Readable test pipe", chunk); // Readable test pipe test
850    callback();
851  }
852}
853
854let readable = new TestReadable();
855let writable = new TestWritable();
856readable.pipe(writable);
857```
858
859### unpipe
860
861unpipe(destination?: Writable): Readable
862
863Detaches a writable stream previously attached to the readable stream.
864
865**Atomic service API**: This API can be used in atomic services since API version 12.
866
867**System capability**: SystemCapability.Utils.Lang
868
869**Parameters**
870
871| Name| Type| Mandatory| Description|
872| -------- | -------- | -------- | -------- |
873| destination | [Writable](#writable) | No| Writable stream to detach. The default value is **undefined**.|
874
875**Return value**
876
877| Type| Description|
878| -------- | -------- |
879| [Readable](#readable) | Current **Readable** object.|
880
881**Error codes**
882
883For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
884
885| ID| Error Message|
886| -------- | -------- |
887| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
888
889**Example**
890
891```ts
892class TestReadable extends stream.Readable {
893  constructor() {
894    super();
895  }
896
897  doRead(size: number) {
898    readable.push('test');
899    readable.push(null);
900  }
901}
902
903class TestWritable extends stream.Writable {
904  constructor() {
905    super();
906  }
907
908  doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) {
909    callback();
910  }
911}
912
913let readable = new TestReadable();
914let writable = new TestWritable();
915readable.pipe(writable);
916readable.unpipe(writable);
917readable.on('data', () => {
918  console.info("Readable test unpipe data event called");
919});
920```
921
922### on
923
924on(event: string, callback: Callback<emitter.EventData>): void
925
926Registers an event processing callback to listen for different events on the readable stream.
927
928**Atomic service API**: This API can be used in atomic services since API version 12.
929
930**System capability**: SystemCapability.Utils.Lang
931
932**Parameters**
933
934| Name| Type| Mandatory| Description|
935| -------- | -------- | -------- | -------- |
936| 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.|
937| callback | Callback\<[emitter.EventData](../apis-basic-services-kit/js-apis-emitter.md#eventdata)\> | Yes| Callback function used to return the event data.|
938
939**Error codes**
940
941For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
942
943| ID| Error Message|
944| -------- | -------- |
945| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
946
947**Example**
948
949```ts
950class TestReadable extends stream.Readable {
951  constructor() {
952    super();
953  }
954
955  doRead(size: number) {
956    throw new Error('Simulated error');
957  }
958}
959
960let readable = new TestReadable();
961readable.push('test');
962readable.on('error', () => {
963  console.info("error event called"); // error event called
964});
965```
966
967### off
968
969off(event: string, callback?: Callback<emitter.EventData>): void
970
971Unregisters an event processing callback used to listen for different events on the writable stream.
972
973**Atomic service API**: This API can be used in atomic services since API version 12.
974
975**System capability**: SystemCapability.Utils.Lang
976
977**Parameters**
978
979| Name| Type| Mandatory| Description|
980| -------- | -------- | -------- | -------- |
981| 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.|
982| callback | string   | No| Callback function.|
983
984**Error codes**
985
986For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
987
988| ID| Error Message|
989| -------- | -------- |
990| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
991
992**Example**
993
994```ts
995class TestReadable extends stream.Readable {
996  constructor() {
997    super();
998  }
999
1000  doRead(size: number) {
1001  }
1002}
1003
1004let readable = new TestReadable();
1005
1006function read() {
1007  console.info("read() called");
1008}
1009
1010readable.setEncoding('utf8');
1011readable.on('readable', read);
1012readable.off('readable');
1013readable.push('test');
1014```
1015
1016### doInitialize
1017
1018doInitialize(callback: Function): void
1019
1020You 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.
1021
1022**Atomic service API**: This API can be used in atomic services since API version 12.
1023
1024**System capability**: SystemCapability.Utils.Lang
1025
1026**Parameters**
1027
1028| Name   | Type    | Mandatory    | Description|
1029| -------- | -------- | -------- | -------- |
1030| callback | Function | Yes| Callback function.|
1031
1032**Error codes**
1033
1034For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
1035
1036| ID| Error Message|
1037| -------- | -------- |
1038| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
1039
1040**Example**
1041
1042```ts
1043class MyReadable extends stream.Readable {
1044  doInitialize(callback: Function) {
1045    super.doInitialize(callback);
1046    console.info("Readable doInitialize"); // Readable doInitialize
1047}
1048
1049  doRead(size: number) {
1050  }
1051}
1052
1053let myReadable = new MyReadable();
1054myReadable.on('data', () => {
1055});
1056```
1057
1058### doRead
1059
1060doRead(size: number): void
1061
1062A data read API that needs to be implemented in child classes.
1063
1064**Atomic service API**: This API can be used in atomic services since API version 12.
1065
1066**System capability**: SystemCapability.Utils.Lang
1067
1068**Parameters**
1069
1070| Name   | Type    | Mandatory    | Description|
1071| -------- | -------- | -------- | -------- |
1072| size | number | Yes| Number of bytes to read.|
1073
1074**Error codes**
1075
1076For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
1077
1078| ID| Error Message|
1079| -------- | -------- |
1080| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
1081
1082**Example**
1083
1084```ts
1085class TestReadable extends stream.Readable {
1086  constructor() {
1087    super();
1088  }
1089
1090  doRead(size: number) {
1091    console.info("doRead called"); // doRead called
1092  }
1093}
1094
1095let readable = new TestReadable();
1096readable.on('data', () => {
1097});
1098```
1099
1100### push
1101
1102push(chunk:  Uint8Array | string | null, encoding?: string): boolean
1103
1104Pushes data into the buffer of the readable stream.
1105
1106**Atomic service API**: This API can be used in atomic services since API version 12.
1107
1108**System capability**: SystemCapability.Utils.Lang
1109
1110**Parameters**
1111
1112| Name   | Type    | Mandatory    | Description|
1113| -------- | -------- | -------- | -------- |
1114| chunk | Uint8Array \| string  \| null | Yes| Data to read.|
1115| encoding | string | No| Encoding format. The default value is **'utf8'**. Currently, **'utf8'**, **'gb18030'**, **'gbk'**, and **'gb2312'** are supported.|
1116
1117**Return value**
1118
1119| Type| Description|
1120| -------- | -------- |
1121| 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.|
1122
1123**Error codes**
1124
1125For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
1126
1127| ID| Error Message|
1128| -------- | -------- |
1129| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
1130
1131**Example**
1132
1133```ts
1134class TestReadable extends stream.Readable {
1135  constructor() {
1136    super();
1137  }
1138
1139  doRead(size: number) {
1140  }
1141}
1142
1143let readable = new TestReadable();
1144let testData = 'Hello world';
1145readable.push(testData);
1146console.info("Readable push test", readable.readableLength); // Readable push test 11
1147```
1148
1149## Duplex
1150
1151A 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.
1152The **Duplex** class inherits from [Readable](# readable) and supports all the APIs in **Readable**.
1153
1154### Attributes
1155
1156**Atomic service API**: This API can be used in atomic services since API version 12.
1157
1158**System capability**: SystemCapability.Utils.Lang
1159
1160| Name   | Type     | Read-Only| Optional | Description       |
1161| ------- | -------- | ------ | ------ | ----------- |
1162| 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**.|
1163| writableHighWatermark | number | Yes| No | Maximum amount of data that can be stored in the buffer in the writable side of the duplex stream. The default value is 16 x 1024, in bytes.|
1164| 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.|
1165| writableLength | number | Yes| No | Number of bytes to be written in the buffer of the duplex stream.|
1166| writableCorked | number | Yes | No| Number of times the **uncork()** API needs to be called in order to fully uncork the duplex stream.|
1167| writableEnded | boolean | Yes | No| Whether [end()](#end) 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) has been called, and **false** means the opposite.|
1168| 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.|
1169
1170### constructor
1171
1172constructor()
1173
1174A constructor used to create a **Duplex** object.
1175
1176**Atomic service API**: This API can be used in atomic services since API version 12.
1177
1178**System capability**: SystemCapability.Utils.Lang
1179
1180**Example**
1181
1182```ts
1183let duplex = new stream.Duplex();
1184```
1185
1186### write
1187
1188write(chunk?: string | Uint8Array, encoding?: string, callback?: Function): boolean
1189
1190Writes data to the buffer of the stream. This API uses an asynchronous callback to return the result.
1191
1192**Atomic service API**: This API can be used in atomic services since API version 12.
1193
1194**System capability**: SystemCapability.Utils.Lang
1195
1196**Parameters**
1197
1198| Name| Type  | Mandatory| Description                      |
1199| ------ | ------ | ---- | -------------------------- |
1200| chunk  | string \| Uint8Array | No| Data to write. The default value is **undefined**.|
1201| encoding  | string | No  | Encoding format. The default value is **'utf8'**. Currently, **'utf8'**, **'gb18030'**, **'gbk'**, and **'gb2312'** are supported.|
1202| callback  | Function | No  | Callback used to return the result. It is not called by default.|
1203
1204**Return value**
1205
1206| Type  | Description                  |
1207| ------ | ---------------------- |
1208| boolean | Whether there is space in the buffer of the writable stream. The value **true** means that there is still space in the buffer, and **false** means that the buffer is full.|
1209
1210**Error codes**
1211
1212For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
1213
1214| ID| Error Message|
1215| -------- | -------- |
1216| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
1217| 10200036 | The stream has been ended. |
1218| 10200037 | The callback is invoked multiple times consecutively. |
1219| 10200039 | The doTransform method has not been implemented for a class that inherits from Transform. |
1220
1221**Example**
1222
1223```ts
1224class TestDuplex extends stream.Duplex {
1225  constructor() {
1226    super();
1227  }
1228
1229  doRead(size: number) {
1230  }
1231
1232  doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) {
1233    console.info("duplexStream chunk is", chunk); // duplexStream chunk is test
1234    callback();
1235  }
1236}
1237
1238let duplexStream = new TestDuplex();
1239let result = duplexStream.write('test', 'utf8');
1240console.info("duplexStream result", result); // duplexStream result true
1241```
1242
1243### end
1244
1245end(chunk?: string | Uint8Array, encoding?: string, callback?: Function): Writable
1246
1247Ends the duplex stream. If the **chunk** parameter is passed in, it is written as the last data chunk. This API uses an asynchronous callback to return the result.
1248
1249**Atomic service API**: This API can be used in atomic services since API version 12.
1250
1251**System capability**: SystemCapability.Utils.Lang
1252
1253**Parameters**
1254
1255| Name| Type  | Mandatory| Description                      |
1256| ------ | ------ | ---- | -------------------------- |
1257| chunk  | string \| Uint8Array | No| Data to write. The default value is **undefined**.|
1258| encoding  | string | No  | Encoding format. The default value is **'utf8'**. Currently, **'utf8'**, **'gb18030'**, **'gbk'**, and **'gb2312'** are supported.|
1259| callback  | Function | No  | Callback used to return the result. It is not called by default.|
1260
1261**Return value**
1262
1263| Type  | Description                  |
1264| ------ | ---------------------- |
1265| [Writable](#writable) | Current **Duplex** object.|
1266
1267**Error codes**
1268
1269For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
1270
1271| ID| Error Message|
1272| -------- | -------- |
1273| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
1274| 10200039 | The doTransform method has not been implemented for a class that inherits from Transform. |
1275
1276**Example**
1277
1278```ts
1279class TestDuplex extends stream.Duplex {
1280  constructor() {
1281    super();
1282  }
1283
1284  doRead(size: number) {
1285  }
1286
1287  doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) {
1288  console.info("Duplex chunk is", chunk); // Duplex chunk is test
1289  callback();
1290  }
1291}
1292
1293let duplexStream = new TestDuplex();
1294duplexStream.end('test', 'utf8', () => {
1295  console.info("Duplex is end"); // Duplex is end
1296});
1297```
1298
1299### setDefaultEncoding
1300
1301setDefaultEncoding(encoding?: string): boolean
1302
1303Sets the default encoding format for the duplex stream so that characters can be correctly parsed when data is read.
1304
1305**Atomic service API**: This API can be used in atomic services since API version 12.
1306
1307**System capability**: SystemCapability.Utils.Lang
1308
1309**Parameters**
1310
1311| Name| Type| Mandatory| Description|
1312| -------- | -------- | -------- | -------- |
1313| encoding | string | No| Encoding format. The default value is **'utf8'**. Currently, **'utf8'**, **'gb18030'**, **'gbk'**, and **'gb2312'** are supported.|
1314
1315**Return value**
1316
1317| Type| Description|
1318| -------- | -------- |
1319| boolean | Whether the setting is successful. The value **true** means that the setting is successful, and **false** means the opposite.|
1320
1321**Error codes**
1322
1323For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
1324
1325| ID| Error Message|
1326| -------- | -------- |
1327| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
1328
1329**Example**
1330
1331```ts
1332class TestDuplex extends stream.Duplex {
1333  constructor() {
1334    super();
1335  }
1336
1337  doRead(size: number) {
1338  }
1339
1340  doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) {
1341    callback();
1342  }
1343}
1344
1345let duplexStream = new TestDuplex();
1346let result = duplexStream.setDefaultEncoding('utf8');
1347console.info("duplexStream is result", result); // duplexStream is result true
1348```
1349
1350### cork
1351
1352cork(): boolean
1353
1354Forces all written data to be buffered in memory. This API is called to optimize the performance of continuous write operations.
1355
1356**Atomic service API**: This API can be used in atomic services since API version 12.
1357
1358**System capability**: SystemCapability.Utils.Lang
1359
1360**Return value**
1361
1362| Type| Description|
1363| -------- | -------- |
1364| boolean | Whether the corked status is successfully set. The value **true** means that the setting is successful, and **false** means the opposite.|
1365
1366**Example**
1367
1368```ts
1369let duplexStream = new stream.Duplex();
1370let result = duplexStream.cork();
1371console.info("duplexStream cork result", result); // duplexStream cork result true
1372```
1373
1374### uncork
1375
1376uncork(): boolean
1377
1378Flushes all data buffered, and writes the data to the target.
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 removed. The value **true** means that the corked status is successfully removed, and **false** means the opposite.|
1389
1390**Example**
1391
1392```ts
1393class TestDuplex extends stream.Duplex {
1394  constructor() {
1395    super();
1396  }
1397
1398  doRead(size: number) {
1399  }
1400
1401  doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) {
1402    dataWritten += chunk;
1403    callback();
1404  }
1405}
1406
1407let dataWritten = '';
1408let duplexStream = new TestDuplex();
1409duplexStream.cork();
1410duplexStream.write('a');
1411duplexStream.write('b');
1412duplexStream.uncork();
1413console.info("Duplex test uncork", dataWritten); // Duplex test uncork ab
1414```
1415
1416### doWrite
1417
1418doWrite(chunk: string | Uint8Array, encoding: string, callback: Function): void
1419
1420A 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.
1421
1422**Atomic service API**: This API can be used in atomic services since API version 12.
1423
1424**System capability**: SystemCapability.Utils.Lang
1425
1426**Parameters**
1427
1428| Name| Type  | Mandatory| Description                      |
1429| ------ | ------ | ---- | -------------------------- |
1430| chunk  | string \| Uint8Array | Yes| Data to write.|
1431| encoding  | string | Yes  | Encoding format. Currently, **'utf8'**, **'gb18030'**, **'gbk'**, and **'gb2312'** are supported.|
1432| callback  | Function | Yes  | Callback function.|
1433
1434**Error codes**
1435
1436For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
1437
1438| ID| Error Message|
1439| -------- | -------- |
1440| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
1441
1442**Example**
1443
1444```ts
1445class TestDuplex extends stream.Duplex {
1446  constructor() {
1447    super();
1448  }
1449
1450  doRead(size: number) {
1451  }
1452
1453  doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) {
1454    console.info("duplexStream chunk is", chunk); // duplexStream chunk is data
1455    callback();
1456  }
1457}
1458
1459let duplexStream = new TestDuplex();
1460duplexStream.write('data', 'utf8');
1461```
1462
1463### doWritev
1464
1465doWritev(chunks: string[] | Uint8Array[], callback: Function): void
1466
1467A 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.
1468
1469**Atomic service API**: This API can be used in atomic services since API version 12.
1470
1471**System capability**: SystemCapability.Utils.Lang
1472
1473**Parameters**
1474
1475| Name| Type| Mandatory| Description|
1476| -------- | -------- | -------- | -------- |
1477| chunks    | string[] \| Uint8Array[] | Yes| Data arrays to write in batches.|
1478| callback  | Function | Yes| Callback function.|
1479
1480**Error codes**
1481
1482For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
1483
1484| ID| Error Message|
1485| -------- | -------- |
1486| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
1487
1488**Example**
1489
1490```ts
1491class TestDuplex extends stream.Duplex {
1492  constructor() {
1493    super();
1494  }
1495
1496  doRead(size: number) {
1497  }
1498
1499  doWrite(chunk: string | Uint8Array, encoding: string, callback: Function) {
1500    callback();
1501  }
1502
1503  doWritev(chunks: string[] | Uint8Array[], callback: Function) {
1504    console.info("duplexStream chunk", chunks[0]); // duplexStream chunk data1
1505    callback();
1506  }
1507}
1508
1509let duplexStream = new TestDuplex();
1510duplexStream.cork();
1511duplexStream.write('data1', 'utf8');
1512duplexStream.write('data2', 'utf8');
1513duplexStream.uncork();
1514duplexStream.end();
1515```
1516
1517## Transform
1518
1519A special duplex stream that supports data conversion and result output. The **Transform** class inherits from [Duplex](#duplex) and supports all the APIs in **Duplex**.
1520
1521### constructor
1522
1523constructor()
1524
1525A constructor used to create a **Transform** object.
1526
1527**Atomic service API**: This API can be used in atomic services since API version 12.
1528
1529**System capability**: SystemCapability.Utils.Lang
1530
1531**Example**
1532
1533```ts
1534let transform = new stream.Transform();
1535```
1536
1537### doTransform
1538
1539doTransform(chunk: string, encoding: string, callback: Function): void
1540
1541Converts or processes input data chunks and uses a callback to notify that the processing is complete.
1542
1543**Atomic service API**: This API can be used in atomic services since API version 12.
1544
1545**System capability**: SystemCapability.Utils.Lang
1546
1547**Parameters**
1548
1549| Name   | Type    | Mandatory    | Description|
1550| -------- | -------- | -------- | -------- |
1551| chunk  | string | Yes| Data to write.|
1552| encoding  | string | Yes  | Encoding format. Currently, **'utf8'**, **'gb18030'**, **'gbk'**, and **'gb2312'** are supported.|
1553| callback  | Function | Yes  | Callback function.|
1554
1555**Error codes**
1556
1557For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
1558
1559| ID| Error Message|
1560| -------- | -------- |
1561| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
1562
1563**Example**
1564
1565```ts
1566class TestTransform extends stream.Transform {
1567  constructor() {
1568    super();
1569  }
1570
1571  doTransform(chunk: string, encoding: string, callback: Function) {
1572    let stringChunk = chunk.toString().toUpperCase();
1573    console.info("Transform test doTransform", stringChunk); // Transform test doTransform HELLO
1574    tr.push(stringChunk);
1575    callback();
1576  }
1577}
1578
1579let tr = new TestTransform();
1580tr.write("hello");
1581```
1582
1583### doFlush
1584
1585doFlush(callback: Function): void
1586
1587Called at the end of the stream to process the remaining data. This API uses an asynchronous callback to return the result.
1588
1589**Atomic service API**: This API can be used in atomic services since API version 12.
1590
1591**System capability**: SystemCapability.Utils.Lang
1592
1593**Parameters**
1594
1595| Name   | Type    | Mandatory    | Description|
1596| -------- | -------- | -------- | -------- |
1597| callback  | Function | Yes  | Callback function.|
1598
1599**Error codes**
1600
1601For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
1602
1603| ID| Error Message|
1604| -------- | -------- |
1605| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
1606
1607**Example**
1608
1609```ts
1610class TestTransform extends stream.Transform {
1611  constructor() {
1612    super();
1613  }
1614
1615  doTransform(chunk: string, encoding: string, callback: Function) {
1616    callback();
1617  }
1618
1619  doFlush(callback: Function) {
1620    callback(null, 'test');
1621  }
1622}
1623
1624let transform = new TestTransform();
1625transform.end('my test');
1626transform.on('data', (data) => {
1627  console.info("data is", data.data); // data is test
1628});
1629```
1630