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