public override int Read(byte[] buffer, int offset, int count)
{
int readed = 0;
// Just the second stream.
if (Position >= this.Stream1.Length)
{
Stream2.PreservePositionAndLock(() =>
{
Stream2.Position = Position - this.Stream1.Length;
readed += Stream2.Read(buffer, offset + readed, count);
});
Position += readed;
}
// On the first stream, and maybe a part of the second.
else
{
if (Position + count > this.Stream1.Length)
{
int count1 = (int)(this.Stream1.Length - Position);
readed += Read(buffer, offset + readed, count1);
readed += Read(buffer, offset + readed, count - count1);
}
else
{
Stream1.PreservePositionAndLock(() =>
{
Stream1.Position = Position;
readed += Stream1.Read(buffer, offset + readed, count);
});
Position += readed;
}
}
return readed;
}