NodeJS:双工流和转换流有什么区别? [英] NodeJS: What's the difference between a Duplex stream and a Transform stream?

查看:30
本文介绍了NodeJS:双工流和转换流有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Stream 文档 声明 双工流是同时实现可读和可写接口的流"和转换流是双工流,其中输出以某种方式从输入计算."不幸的是,文档没有描述转换流在双工流之外提供了什么.

两者之间有什么区别吗?你什么时候会使用一个?

解决方案

双工流可以被认为是可读流和可写流.两者都是独立的,每个都有单独的内部缓冲区.读取和写入事件独立发生.

<块引用>

 双流------------------|阅读 <----- 外部源你------------------|写 ----->外部水槽------------------|你没有得到你所写的.它被发送到另一个源.

转换流是一种双工,其中读写以因果方式发生.双工流的端点通过某种转换链接.读取需要写入发生.

<块引用>

 转换流--------------|--------------你写---->---->读你--------------|--------------你写了一些东西,它被转化了,然后你读了一些东西.

The Stream docs state that Duplex Streams "are streams that implement both the Readable and Writable interfaces" and Transform Streams "are Duplex streams where the output is in some way computed from the input." Unfortunately, the docs do not describe what Transform streams provide above and beyond Duplex streams.

Are there any differences between the two? When would you use one over the other?

解决方案

A Duplex stream can be thought of a readable stream with a writable stream. Both are independent and each have separate internal buffer. The reads and writes events happen independently.

                             Duplex Stream
                          ------------------|
                    Read  <-----               External Source
            You           ------------------|   
                    Write ----->               External Sink
                          ------------------|
            You don't get what you write. It is sent to another source.

A Transform stream is a duplex where the read and write takes place in a causal way. The end points of the duplex stream are linked via some transform. Read requires write to have occurred.

                                 Transform Stream
                           --------------|--------------
            You     Write  ---->                   ---->  Read  You
                           --------------|--------------
            You write something, it is transformed, then you read something.

这篇关于NodeJS:双工流和转换流有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆