我理解流是字节序列的表示。每个流都提供了将字节读写到其给定的后备存储的方法。但溪流的意义何在?为什么后台存储本身不是我们交互的对象?

不知什么原因,我就是不喜欢这个概念。我读了很多文章,但我觉得我需要一个类比。


当前回答

A stream is a highly abstracted metaphor and a strict contract. It means that you can manipulate objects in sequence without concern about gaps. That is to say, a stream must have no vacuum or gaps. Objects in it are arranged in sequence one by one continuously. As a result, we don't have to worry about encountering a vacuum abruptly in the midst of processing a stream, or we can't leave a vacuum deliberately when producing a stream. In other words, we don't have to consider the case of a void in processing or producing a stream. There is no way we can come across it or produce it on purpose. If you are constructing a stream, you must not leave any gaps in the stream.

换句话说,如果有一个缺口,它一定不是一个流。当您将序列称为流时,要么您被保证其中没有空白,要么您必须信守您所生成的序列中没有空白的承诺。

回顾一下,想象一条水流。它最大的特点是什么?

连续!

溪流的抽象精神就是关于它的。

其他回答

当我第一次听说流媒体时,是在网络摄像头直播的背景下。所以,一个主机播放视频内容,另一个主机接收视频内容。这是流媒体吗?嗯…是的……但直播是一个具体的概念,我认为这个问题指的是流媒体这个抽象的概念。参见https://en.wikipedia.org/wiki/Live_streaming

让我们继续。


视频并不是唯一可以流媒体的资源。音频也可以流式传输。我们现在谈论的是流媒体。见https://en.wikipedia.org/wiki/Streaming_media。音频可以通过多种方式从源传输到目标。因此,让我们比较一些数据传递方法。

经典文件下载 传统的文件下载并不是实时的。在使用该文件之前,您必须等待下载完成。

渐进式下载 渐进式下载块将数据从流媒体文件下载到临时缓冲区。该缓冲区中的数据是可行的:缓冲区中的音频-视频数据是可播放的。因为用户可以在下载的同时观看/收听流媒体文件。快进和倒带是可能的,当然是在缓冲区内。不管怎样,渐进式下载并不是直播。

流媒体 实时发生,大量数据。流媒体在直播中实现。正在收听广播的客户端不能快进或倒带。在视频流中,数据在回放后被丢弃。

流服务器与客户端保持双向连接,而Web服务器在服务器响应后关闭连接。


音频和视频并不是唯一可以流媒体的东西。让我们看看PHP手册中的流的概念。

流是显示可流行为的资源对象。那 是,它可以以线性方式读取或写入,并且可能是 能够fseek()到流中的任意位置。 链接:https://www.php.net/manual/en/intro.stream.php

在PHP中,资源是对外部源(如文件、数据库连接)的引用。换句话说,流是一个可以读取或写入的源。因此,如果你使用了fopen(),那么你已经使用了流。

一个文本文件被流式处理的例子:

// Let's say that cheese.txt is a file that contains this content: 
// I like cheese, a lot! My favorite cheese brand is Leerdammer.
$fp = fopen('cheese.txt', 'r');

$str8 = fread($fp, 8); // read first 8 characters from stream. 

fseek($fp, 21); // set position indicator from stream at the 21th position (0 = first position)
$str30 = fread($fp, 30); // read 30 characters from stream

echo $str8; // Output: I like c 
echo $str30; // Output: My favorite cheese brand is L

Zip文件也可以流式传输。最重要的是,流媒体并不局限于文件。HTTP, FTP, SSH连接和输入/输出也可以流式传输。


维基百科对流媒体的概念是怎么说的?

在计算机科学中,流是数据元素的序列 随时间推移可用。流可以看作是传送带上的物品 皮带一次加工一个,而不是大批量加工。

参见:https://en.wikipedia.org/wiki/Stream_%28computing%29。

维基百科的链接是:https://srfi.schemers.org/srfi-41/srfi-41.html 关于流,作者是这样说的:

流,有时称为惰性列表,是一种顺序数据结构 只包含按需计算的元素。流要么为空 或者是cdr中有一个流的pair。因为流的元素是 仅在访问时计算,流可以是无限的。

流实际上是一种数据结构。


我的结论是:流是一种包含数据的源,可以按顺序读取或写入数据。流不会一次读取源包含的所有内容,而是按顺序读取/写入。


有用的链接:

http://www.slideshare.net/auroraeosrose/writing-and-using-php-streams-and-sockets-zendcon-2011 Provides a very clear presentation https://www.sk89q.com/2010/04/introduction-to-php-streams/ http://www.netlingo.com/word/stream-or-streaming.php http://www.brainbell.com/tutorials/php/Using_PHP_Streams.htm http://www.sitepoint.com/php-streaming-output-buffering-explained/ http://php.net/manual/en/wrappers.php http://www.digidata-lb.com/streaming/Streaming_Proposal.pdf http://www.webopedia.com/TERM/S/streaming.html https://en.wikipedia.org/wiki/Stream_%28computing%29 https://srfi.schemers.org/srfi-41/srfi-41.html

A stream is a highly abstracted metaphor and a strict contract. It means that you can manipulate objects in sequence without concern about gaps. That is to say, a stream must have no vacuum or gaps. Objects in it are arranged in sequence one by one continuously. As a result, we don't have to worry about encountering a vacuum abruptly in the midst of processing a stream, or we can't leave a vacuum deliberately when producing a stream. In other words, we don't have to consider the case of a void in processing or producing a stream. There is no way we can come across it or produce it on purpose. If you are constructing a stream, you must not leave any gaps in the stream.

换句话说,如果有一个缺口,它一定不是一个流。当您将序列称为流时,要么您被保证其中没有空白,要么您必须信守您所生成的序列中没有空白的承诺。

回顾一下,想象一条水流。它最大的特点是什么?

连续!

溪流的抽象精神就是关于它的。

之所以选择“流”这个词,是因为它(在现实生活中)与我们使用它时想要传达的意思非常相似。

Start thinking about the analogy to a water stream. You receive a continuous flow of data, just like water continuously flows in a river. You don't necessarily know where the data is coming from, and most often you don't need to; be it from a file, a socket, or any other source, it doesn't (shouldn't) really matter. This is very similar to receiving a stream of water, whereby you don't need to know where it is coming from; be it from a lake, a fountain, or any other source, it doesn't (shouldn't) really matter. source

另一点(对于读取文件的情况):

流可以允许您在完成读取文件的所有内容之前执行其他操作。 可以节省内存,因为不需要一次加载所有文件内容。

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

read one byte. Next time you read, you'll get the next byte, and so on. read several bytes from the stream into an array seek (move your current position in the stream, so that next time you read you get bytes from the new position) write one byte write several bytes from an array into the stream skip bytes from the stream (this is like read, but you ignore the data. Or if you prefer it's like seek but can only go forwards.) push back bytes into an input stream (this is like "undo" for read - you shove a few bytes back up the stream, so that next time you read that's what you'll see. It's occasionally useful for parsers, as is: peek (look at bytes without reading them, so that they're still there in the stream to be read later)

一个特定的流可能支持读(在这种情况下,它是一个“输入流”),写(“输出流”)或两者都支持。并不是所有的溪流都是可搜索的。

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 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.

我能想到的最好的比喻是,溪流是一条传送带,向你走来或离开你(有时两者兼而有之)。你从输入流中取出东西,你把东西放到输出流中。有些传送带你可以认为是从墙上的一个洞里出来的——它们是不可寻找的,阅读或写作是一次性的交易。一些传送带就摆在你面前,你可以在溪流中选择你想读/写的位置——这就是寻找。

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 :-)