什么是流? [英] What is a stream?

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

问题描述

编程世界中的流是什么?为什么我们需要它?

What is a stream in the programming world? Why do we need it?

如果可能,请用类比解释.

Kindly explain with the help of an analogy, if possible.

推荐答案

一个流表示一个对象序列(通常是字节,但不一定是字节),可以按顺序访问这些对象.流上的典型操作:

A stream represents a sequence of objects (usually bytes, but not necessarily so), which can be accessed in sequential order. Typical operations on a stream:

  • 读取一个字节.下次阅读时,您将获得下一个字节,依此类推.
  • 从流中读取几个字节到一个数组中
  • seek(移动您在流中的当前位置,以便下次读取时从新位置获取字节)
  • 写入一个字节
  • 将数组中的几个字节写入流中
  • 从流中跳过字节(这就像读取,但您忽略了数据.或者,如果您愿意,它就像搜索,但只能前进.)
  • 将字节推回输入流(这就像读取的撤消" - 您将几个字节推回流中,以便下次读取时您会看到.它偶尔对解析器有用,如是:
  • peek(查看字节而不读取它们,以便它们仍然存在于流中以便稍后读取)

一个特定的流可能支持读取(在这种情况下它是输入流")、写入(输出流")或两者.并非所有流都是可查找的.

A particular stream might support reading (in which case it is an "input stream"), writing ("output stream") or both. Not all streams are seekable.

推回是相当少见的,但您始终可以通过将实际输入流包装在另一个包含内部缓冲区的输入流中来将其添加到流中.读取来自缓冲区,如果您推回,则数据将放置在缓冲区中.如果缓冲区中没有任何内容,则推回流将从实际流中读取.这是流适配器"的一个简单示例:它位于输入流的末端",它本身就是一个输入流,并且它做了一些原始流没有做的额外工作.

Push back is fairly rare, but you can always add it to a stream by wrapping the real input stream in another input stream that holds an internal buffer. Reads come from the buffer, and if you push back then data is placed in the buffer. If there's nothing in the buffer then the push back stream reads from the real stream. This is a simple example of a "stream adaptor": it sits on the "end" of an input stream, it is an input stream itself, and it does something extra that the original stream didn't.

Stream 是一个有用的抽象,因为它可以描述文件(实际上是数组,因此seek 很简单),也可以描述终端输入/输出(除非缓冲,否则不可搜索)、套接字、串行端口等.所以你可以写代码表示我想要一些数据,我不在乎它来自哪里或如何到达这里",或者我会产生一些数据,这完全取决于我的调用者会发生什么".前者采用输入流参数,后者采用输出流参数.

Stream is a useful abstraction because it can describe files (which are really arrays, hence seek is straightforward) but also terminal input/output (which is not seekable unless buffered), sockets, serial ports, etc. So you can write code which says either "I want some data, and I don't care where it comes from or how it got here", or "I'll produce some data, and it's entirely up to my caller what happens to it". The former takes an input stream parameter, the latter takes an output stream parameter.

我能想到的最好的比喻是,一条小溪是一条向你走来或远离你的传送带(有时两者兼而有之).你从输入流中取出东西,你把东西放在输出流上.您可以将某些传送带视为从墙上的一个洞里出来——它们是不可查找的,读取或写入是一次性交易.一些传送带布置在您的面前,您可以选择要读/写的流中的行踪 - 这就是寻找.

Best analogy I can think of is that a stream is a conveyor belt coming towards you or leading away from you (or sometimes both). You take stuff off an input stream, you put stuff on an output stream. Some conveyors you can think of as coming out of a hole in the wall - they aren't seekable, reading or writing is a one-time-only deal. Some conveyors are laid out in front of you, and you can move along choosing whereabouts in the stream you want to read/write - that's seeking.

不过,正如 IRBMe 所说,最好根据流提供的操作(这因实现而异,但有很多共同点)而不是物理类比来考虑流.流是你可以读或写的东西".当你开始连接上游适配器时,你可以把它们想象成一个盒子,里面有一个传送带,一个传送带出去,你连接到其他流,然后这个盒子对数据执行一些转换(压缩它,或者改变 UNIX 换行符)DOS 的,或其他).管道是对这个比喻的另一个彻底测试:在那里你创建一对流,这样你写入一个流的任何东西都可以从另一个流中读出.想想虫洞:-)

As IRBMe says, though, it's best to think of a stream in terms of the operations it offers (which vary from implementation to implementation, but have a lot in common) rather than by a physical analogy. Streams are "things you can read or write". When you start connecting up stream adaptors, you can think of them as a box with a conveyor in, and a conveyor out, that you connect to other streams and then the box performs some transformation on the data (zipping it, or changing UNIX linefeeds to DOS ones, or whatever). Pipes are another thorough test of the metaphor: that's where you create a pair of streams such that anything you write into one can be read out of the other. Think wormholes :-)

这篇关于什么是流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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