如何在 .NET 中拆分(复制)流? [英] How can I split (copy) a Stream in .NET?

查看:20
本文介绍了如何在 .NET 中拆分(复制)流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道我在哪里可以找到流拆分器的实现?

Does anyone know where I can find a Stream splitter implementation?

我正在寻找一个流,并获得两个独立的流,它们可以独立读取和关闭而不会相互影响.这些流都应该返回与原始流相同的二进制数据.无需实现 Position 或 Seek 之类的......仅转发.

I'm looking to take a Stream, and obtain two separate streams that can be independently read and closed without impacting each other. These streams should each return the same binary data that the original stream would. No need to implement Position or Seek and such... Forward only.

我更喜欢它不只是将整个流复制到内存中并多次提供它,这对于自己实现来说非常简单.

I'd prefer if it didn't just copy the whole stream into memory and serve it up multiple times, which would be fairly simple enough to implement myself.

有什么可以做到这一点的吗?

Is there anything out there that could do this?

推荐答案

不是开箱即用的.

您需要以 FIFO 方式缓冲原始流中的数据,仅丢弃所有读取器"流已读取的数据.

You'll need to buffer the data from the original stream in a FIFO manner, discarding only data which has been read by all "reader" streams.

我会使用:

  • 一个管理"对象,其中包含某种字节 [] 队列,其中包含要缓冲的块并在需要时从源流中读取其他数据
  • 一些读取器"实例知道它们正在读取的位置和缓冲区,并且从管理"请求下一个块并在它们不再使用块时通知它,以便它可以从队列

这篇关于如何在 .NET 中拆分(复制)流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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