Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Seek #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions src/CombinationStream-Net20/CombinationStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ internal class CombinationStream : Stream
{
private readonly IList<Stream> _streams;
private readonly IList<int> _streamsToDispose;
private readonly IList<long> _streamsStartPos;
private int _currentStreamIndex;
private Stream _currentStream;
private long _length = -1;
Expand All @@ -52,6 +53,14 @@ public CombinationStream(IList<Stream> streams, IList<int> streamsToDispose)
_streamsToDispose = streamsToDispose;
if (streams.Count > 0)
_currentStream = streams[_currentStreamIndex++];

_streamsStartPos = new List<long>(streams.Count);
long pos = 0;
foreach (var strm in streams)
{
_streamsStartPos.Add(pos);
pos += strm.Length;
}
}

public IList<Stream> InternalStreams { get { return _streams; } }
Expand All @@ -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)
Expand Down Expand Up @@ -286,7 +323,7 @@ public override bool CanRead

public override bool CanSeek
{
get { return false; }
get { return true; }
}

public override bool CanWrite
Expand Down