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