C# BufferedStream 与MemoryStream 的区别

来源:互联网 发布:dota2反和谐号淘宝 编辑:程序博客网 时间:2024/05/17 19:19

BufferedStream is just a buffer over an existing stream. MemoryStream is a buffer for the whole stream - it isn't chained to another one. You can ask it to write itself to another stream at any time, but that's not the same thing.

One of the principle reasons for buffering is to avoid frequent writes to expensive resources. However, that doesn't mean you want to buffer all the data in memory - just enough to avoid very small writes. For example, if FileStream didn't have its own buffering strategy, then wrapping it in BufferedStream could end up with a buffer of only 8K even if you write megabytes of data. As pointed out in the comments though, FileStream has enough buffering that using BufferedStream in conjunction with it is pointless.

 

大概的意思是,两个都是缓冲区,BufferedStream并不是将所有内容都存放到内存中,而MemoryStream则是。BufferedStream必须跟其他流如FileStream结合使用,而MemoryStream则不用,虽然可以把MemoryStream转换为其他流。