Stream wrapper to make Stream seekable?

后端 未结 5 775
一整个雨季
一整个雨季 2020-12-05 10:37

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 do

5条回答
  •  一生所求
    2020-12-05 11:07

    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
    }
    

    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.

提交回复
热议问题