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.
这篇关于流包装器使流可查找?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:流包装器使流可查找?


- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
- 带问号的 nvarchar 列结果 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
- 使用 rss + c# 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
- 在 C# 中异步处理项目队列 2022-01-01