Stream wrapper to make Stream seekable?(流包装器使流可查找?)
问题描述
我有一个不可搜索的只读 System.IO.Stream
实现(并且它的 Position
总是返回 0).我需要将它发送给在流上执行一些 Seek
操作(也就是设置位置)的使用者.这不是一个巨大的搜索 - 例如从当前位置 +/- 100.是否有现有的 Stream
包装器可以为流添加缓冲能力以进行简单的 Seek 操作?
I have a readonly System.IO.Stream
implementation that is not seekable (and its Position
always returns 0). I need to send it to a consumer that does some Seek
operations (aka, sets the Position) on the stream. It's not a huge seek -- say +/- 100 from the current position. Is there an existing Stream
wrapper that will add a buffering ability to the stream for simple Seek operations?
更新:我应该补充一点,我的消费者是 NAudio Mp3FileReader.我真的只需要一种方法来播放(缓慢且无限期地)流式 MP3.我认为这是 NAudio 希望能够随意寻找他们的数据源的错误.
Update: I should add that my consumer is the NAudio Mp3FileReader. I really just need a way to play a (slowly and indefinitely) streaming MP3. I think it's a bug that NAudio expects to be able to seek their data source at will.
推荐答案
向前查找很容易(只需阅读),但如果不进行缓冲,则无法向后查找.也许只是:
Seeking forwards is easy enough (just read), but you can't seek backwards without buffering. Maybe just:
using(var ms = new MemoryStream()) {
otherStream.CopyTo(ms);
ms.Position = 0;
// now work with ms
}
然而,这仅适用于小到中等的流(不是 GB),已知会结束(哪些流不需要这样做).如果您需要更大的流,可以使用 FileStream
到临时文件,但 IO 密集程度要高得多.
This, however, is only suitable for small-to-moderate streams (not GB), that are known to end (which streams are not requires to do). If you need a larger stream, a FileStream
to a temp-file would work, but is significantly more IO-intensive.
这篇关于流包装器使流可查找?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:流包装器使流可查找?
基础教程推荐
- 使用 SED 在 XML 标签之间提取值 2022-01-01
- C# - 将浮点数转换为整数...并根据余数更改整数 2022-01-01
- C# - 如何列出发布到 ASPX 页面的变量名称和值 2022-01-01
- 从 VB6 迁移到 .NET/.NET Core 的最佳策略或工具 2022-01-01
- Page.OnAppearing 中的 Xamarin.Forms Page.DisplayAlert 2022-01-01
- 覆盖 Json.Net 中的默认原始类型处理 2022-01-01
- 我什么时候应该使用 GC.SuppressFinalize()? 2022-01-01
- 当键值未知时反序列化 JSON 2022-01-01
- 创建属性设置器委托 2022-01-01
- 如何使用OpenXML SDK将Excel转换为CSV? 2022-01-01