流包装,使流可查找? [英] Stream wrapper to make Stream seekable?
问题描述
我有一个只读的System.IO.Stream
的实现,是不是可查找(和它的位置始终返回0)。我需要将其发送给消费者,做一些寻找
操作(即,将位置设置)在流。这是不是一个巨大的寻求 - 从当前位置说+/- 100。是否有一个现有的流
包装,将增加一个缓冲能力,流于简单查找操作?
I have a readonly System.IO.Stream
implementation that is not seekable (and it's 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?
更新:我要补充一点,我的消费者是n音讯Mp3FileReader。我真的只是需要一种方法来发挥(慢,并且无限期地)流的MP3。我认为这是n音讯希望能够寻求随意的数据源中的错误。
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
}
此,但是,只适合小至中度流(未标),已知会结束(其流是不需要做)。如果你需要一个更大的流,的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.
这篇关于流包装,使流可查找?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!