流包装器使流可查找?

时间:2023-02-04
本文介绍了流包装器使流可查找?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个不可搜索的只读 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#从服务器下载大文件,内存消耗更少 下一篇:如何安全地拦截自定义 Owin 中间件中的响应流

相关文章

最新文章