Stream wrapper to make Stream seekable?

后端 未结 5 780
一整个雨季
一整个雨季 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:08

    Another solution might be to create your own stream class which wraps the other stream. Implement Seek as a NOP.

    class MyStream : Stream
    {
        public MyStream(Stream baseStream) { this.baseStream = baseStream; }
        private Stream baseStream;
    
        // Delegate all operations except Seek/CanSeek to baseStream
    
        public override bool CanSeek { get { return true; } }
        public override long Seek(long offset, SeekOrigin origin) { return baseStream.Position; }
    }
    

    If the player is seeking for no good reason, this might just work.

提交回复
热议问题