What is the stream?

1Can you explain the concept of streams?

你能解释一下流的概念吗?

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

我知道流一个一系列字节的代表,每一个流提供将字节读或者写进它被给的返回存取罐的很多方法。

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

//让我们忘记那个返回的存取罐,开始思考来做一个类似水流的对比,你收到一个持续的水流数据,就像水持续的流进一条河流一样,你没必要找到数据来源于哪里,大部分时候你没必要知道,它来自一个文件,socket还是其他的资源,真的没关系。这个非常类似收到一个水流,那儿你不需要知道水来自己哪里,它来自己湖畔还是山川或者其他资源,真的不重要。

4、

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.

就是说,一旦你开始思考你唯一关系的的事你需要的诗句,而不是它来自己来自于哪里,这样人们谈论的抽象的流就会变得清晰。你可以想你能包裹流,你的方法仍能工作

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

// in another method:

Stream fileStream = new FileStream("My Data.dat");

//创建一个zip文件

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:

正如上面说的,它变得非常容易来改变你的输入源,但并没有改变处理的罗辑,举例来说,从一个network socket读取数据代替一个文件。

Stream stream = new NetworkStream(mySocket);

StreamReader reader = new StreamReader(stream);

int x = ReadInt(reader);

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

明白没?只要你的方法不关心输入源是什么,你都能以各种方式来定制源,AAAAAAA

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

AAAAA

8So, 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.

所以,总结一下,一个流是一个输入源,隐藏了其他一个抽象的源,只要你没有打断这个抽象,你的码将会非常流畅。

AAAAA

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值