你能解释一下流的概念吗? [英] Can you explain the concept of streams?

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

问题描述

我理解流是字节序列的表示.每个流都提供了将字节读取和写入其给定后备存储的方法.但是流的重点是什么?为什么后备存储本身不是我们与之交互的?

I understand that a stream is a representation of a sequence of bytes. Each stream provides means for reading and writing bytes to its given backing store. But what is the point of the stream? Why isn't the backing store itself what we interact with?

无论出于何种原因,这个概念对我来说都没有点击.我读了一堆文章,但我想我需要一个类比什么的.

For whatever reason this concept just isn't clicking for me. I've read a bunch of articles, but I think I need an analogy or something.

推荐答案

选择流"这个词是因为它代表(在现实生活中)与我们在使用时想要传达的意思非常相似.

The word "stream" has been chosen because it represents (in real life) a very similar meaning to what we want to convey when we use it.

>

让我们暂时忘记后备存储,并开始考虑与水流的类比.您会收到源源不断的数据,就像河流中不断流淌的水一样.您不一定知道数据来自哪里,而且大多数情况下您不需要知道;无论是来自文件、套接字还是任何其他来源,它都(不应该)真正重要.这与接收水流非常相似,您无需知道它来自哪里;无论是来自湖泊、喷泉还是任何其他来源,都没有(不应该)真正重要.

Let's forget about the backing store for a little, and 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.

也就是说,一旦你开始认为你只关心获取你需要的数据,而不管它来自哪里,其他人谈论的抽象就会变得更加清晰.你开始认为你可以包装流,你的方法仍然可以完美地工作.例如,您可以这样做:

That said, once you start thinking that you only care about getting the data you need, regardless of where it comes from, the abstractions other people talked about become clearer. You start thinking that you can wrap streams, and your methods will still work perfectly. For example, you could do this:

int ReadInt(StreamReader reader) { return Int32.Parse(reader.ReadLine()); }

// in another method:
Stream fileStream = new FileStream("My Data.dat");
Stream zipStream = new ZipDecompressorStream(fileStream);
Stream decryptedStream = new DecryptionStream(zipStream);
StreamReader reader = new StreamReader(decryptedStream);

int x = ReadInt(reader);

如您所见,在不更改处理逻辑的情况下更改输入源变得非常容易.例如,要从网络套接字而不是文件中读取数据:

As you see, it becomes very easy to change your input source without changing your processing logic. For example, to read your data from a network socket instead of a file:

Stream stream = new NetworkStream(mySocket);
StreamReader reader = new StreamReader(stream);
int x = ReadInt(reader);

尽可能简单.美还在继续,因为您可以使用任何类型的输入源,只要您可以为其构建流包装器".你甚至可以这样做:

As easy as it can be. And the beauty continues, as you can use any kind of input source, as long as you can build a stream "wrapper" for it. You could even do this:

public class RandomNumbersStreamReader : StreamReader {
    private Random random = new Random();

    public String ReadLine() { return random.Next().ToString(); }
}

// and to call it:
int x = ReadInt(new RandomNumbersStreamReader());

看到了吗?只要您的方法不关心输入源是什么,您就可以通过各种方式自定义源.抽象允许您以非常优雅的方式将输入与处理逻辑分离.

See? As long as your method doesn't care what the input source is, you can customize your source in various ways. The abstraction allows you to decouple input from processing logic in a very elegant way.

请注意,我们自己创建的流没有后备存储,但它仍然可以完美地满足我们的目的.

Note that the stream we created ourselves does not have a backing store, but it still serves our purposes perfectly.

所以,总而言之,流只是一个输入源,隐藏(抽象)另一个源.只要不打破抽象,你的代码就会非常灵活.

So, to summarize, a stream is just a source of input, hiding away (abstracting) another source. As long as you don't break the abstraction, your code will be very flexible.

这篇关于你能解释一下流的概念吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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