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