From 51dcb71e4e473b0ee042b37a00d0a2f4ed4d0a9d Mon Sep 17 00:00:00 2001 From: sbx064 Date: Fri, 4 Sep 2015 14:21:47 +0200 Subject: [PATCH] Support Seek --- .../CombinationStream.cs | 41 ++++++++++++++++++- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/src/CombinationStream-Net20/CombinationStream.cs b/src/CombinationStream-Net20/CombinationStream.cs index e39ea87..4059851 100644 --- a/src/CombinationStream-Net20/CombinationStream.cs +++ b/src/CombinationStream-Net20/CombinationStream.cs @@ -33,6 +33,7 @@ internal class CombinationStream : Stream { private readonly IList _streams; private readonly IList _streamsToDispose; + private readonly IList _streamsStartPos; private int _currentStreamIndex; private Stream _currentStream; private long _length = -1; @@ -52,6 +53,14 @@ public CombinationStream(IList streams, IList streamsToDispose) _streamsToDispose = streamsToDispose; if (streams.Count > 0) _currentStream = streams[_currentStreamIndex++]; + + _streamsStartPos = new List(streams.Count); + long pos = 0; + foreach (var strm in streams) + { + _streamsStartPos.Add(pos); + pos += strm.Length; + } } public IList InternalStreams { get { return _streams; } } @@ -64,7 +73,35 @@ public override void Flush() public override long Seek(long offset, SeekOrigin origin) { - throw new InvalidOperationException("Stream is not seekable."); + long pos = 0; + switch (origin) + { + case SeekOrigin.Begin: + pos = offset; + break; + case SeekOrigin.Current: + pos = this.Position + offset; + break; + case SeekOrigin.End: + pos = Length + offset; + break; + } + int idx = 0; + while (idx+1 < _streamsStartPos.Count) + { + if (_streamsStartPos[idx + 1] > pos) + { + break; + } + idx++; + } + + _currentStreamIndex = idx; + _currentStream = _streams[_currentStreamIndex]; + _currentStream.Seek(pos - _streamsStartPos[idx], SeekOrigin.Begin); + _postion = pos; + return _postion; + //throw new InvalidOperationException("Stream is not seekable."); } public override void SetLength(long value) @@ -286,7 +323,7 @@ public override bool CanRead public override bool CanSeek { - get { return false; } + get { return true; } } public override bool CanWrite