@@ -138,6 +138,140 @@ rr.on('end', () => {
138138
139139调用 stream.resume()并且readsFlowing不为true时,将发出resume事件。
140140
141+ ## stream.Readable 类方法
141142
143+ stream.Readable类包含以下常用的方法:
144+
145+ 1 . destroy
146+
147+ readable.destroy([ error] ) 方法用于销毁流,并触发 error 事件 和 close 事件. 调用后,可读流将释放所有的内部资源,且忽视后续的push()调用。
148+
149+ 实现流时不应该重写这个方法,而是重写readable._ destroy()
150+
151+ 2 . isPaused
152+
153+ readable.isPaused()方法用于返回可读流当前的操纵状态。主要用于 readable.pipe() 底层的机制,大多数情况下无须直接使用该方法
154+
155+ ``` js
156+ const readable = new stream.Readable ();
157+
158+ readable .isPaused (); // === false
159+ readable .pause ();
160+ readable .isPaused (); // === true
161+ readable .resume ();
162+ readable .isPaused (); // ==== false
163+ ```
164+
165+ 3 . pause 与 resume
166+
167+ readable.pause() 方法使流动模式的流停止触发 data 事件,并切换到流动模式。任何可用的数据都会保留在内部缓存中。
168+
169+ 相对的, readable.resume() 将被暂停⏸️的可读流恢复触发data事件,并将流切换到流动模式。
170+
171+ ``` js
172+ const fs = require (' fs' );
173+
174+ const readable = fs .createReadStream (' data.txt' );
175+ readable .on (' data' , (chunk ) => {
176+ console .log (` 接收到${ chunk .length } 字节的数据` );
177+ // 暂停
178+ readable .pause ();
179+
180+ console .log (' 暂停一秒' );
181+ setTimeout (() => {
182+ console .log (' 数据重新开始流动' );
183+ // 继续
184+ readable .resume ();
185+ },1000 );
186+ });
187+
188+ readable .on (' end' , () => {
189+ console .log (' 结束' );
190+ });
191+ ```
192+
193+ 4 . pipe
194+
195+ readable.pipe(destination[ , options] ) 方法用于绑定 可写流 到 可读流,将 可读流 自动切换到流动模式,并将可读流的所有数据推送到绑定的可写流。
196+
197+ 数据流会被自动管理,所以即使可读流更快,目标可写流也不会超负荷。
198+
199+ 将可读流的所有数据通过管道推送到 write-data.txt 文件
200+
201+ ``` js
202+ const fs = require (' fs' )
203+
204+ const readable = fs .createReadStream (' data.txt' );
205+
206+ const writable = fs .createWriteStream (' write-data.txt' );
207+
208+ // readable 的所有数据都推送到 write-data.txt
209+ readable .pipe (writable);
210+ ```
211+
212+ 可以在单个可读流上绑定多个可写流。
213+
214+ ``` js
215+ readable .pipe () // 会返回目标流的引用,这样就可以对流进行链式的管道操作。
216+
217+ const fs = require (' fs' );
218+ const zlib = require (' zlib' );
219+
220+ const readable = fs .createReadStream (' data.txt' );
221+ const gzip = zlib .createGzip ();
222+ const writable2 = fs .createWriteStream (' write-data.txt.gz' );
223+
224+ // 在单个可读流上绑定多个可写流
225+ readable .pipe (gzip).pipe (writable2);
226+ ```
227+
228+ 默认情况下,当来源可读流触发 end 事件时,目标可写流 也会调用 stream.end() 结束写入。
229+
230+ 若要禁用这种默认行为,end选项应设为 false ,这样目标流就回保持打开。
231+
232+ ``` js
233+ reader .pipe (writer, { end: false });
234+ reader .on (' end' , () => {
235+ writer .end (' 结束' );
236+ });
237+ ```
238+
239+ 如果可读流发生错误🙅🙅♂️,目标可写流不会自动关闭,需要手动关闭所有流以避免内存泄漏。
240+
241+ process.stderr 和 process.stdout 可写的流在 Node.js 进程退出之前永远不会关闭,无论指定的选项如何。
242+
243+ 5 . read
244+
245+ readable.read([ size] ) 方法用于从内部缓冲拉取并返回数据。其中,size 指定要读取的数据的字节数。如果没有指定 size 参数,则返回内部缓冲中的所有数据。
246+
247+ 该方法如果没有可读的数据,则返回null。默认情况下,readable.read() 返回的数据是 Buffer对象,除非使用 readable.setEncoding() 指定字符编码或流 处于对象模式。
248+
249+ 如果可读的数据不足size 个字节,则返回内部缓冲剩余的数据,如果流已经结束则 返回null
250+
251+ readable.read() 应该只对 处于暂停模式的可读流调用。在流动模式中, readable.read() 会自动调用直到内部缓冲的数据完全耗尽。
252+
253+ 如果 readable.read() 返回一个数据块,则data 事件也会触发。
254+
255+ end事件触发后再调用 stream.read([ size] ) 会返回null,不会抛出错误。
256+
257+ ``` js
258+ const fs = require (' fs' );
259+ const readable = fs .createReadStream (' data.txt' );
260+
261+ // 设置字符编码
262+ readable .setEncoding (' utf-8' );
263+
264+ // 读取数据
265+ readable .on (' readable' , () => {
266+ let chunk;
267+ while (null !== (chunk = readable .read (10 ))) {
268+ console .log
269+ }
270+ })
271+
272+ readable .on (' end' , () => {
273+ console .log ()
274+ })
275+ ```
142276
143277
0 commit comments