private long _Transfer(byte[] Buffer, long Offset, long Count, Func<byte[], long, long, long> Method)
{
if (Count == 0) return 0;
_PrepareCurrentStream();
if (_CurrentStream == null) throw (new InvalidOperationException(String.Format("Invalid/Unmapped MapStream position {0}", Position)));
var AvailableCount = GetAvailableBytesOnCurrentStream();
if (Count > AvailableCount)
{
// Read from current Stream.
var Readed1 = _Transfer(Buffer, Offset, AvailableCount, Method);
if (Readed1 == 0)
{
return 0;
}
else
{
// Try to read from the next Stream.
var Readed2 = _Transfer(Buffer, Offset + AvailableCount, Count - AvailableCount, Method);
return Readed1 + Readed2;
}
}
else
{
long ActualReaded = 0;
//_PrepareCurrentStream();
_CurrentStream.PreservePositionAndLock(() =>
{
_CurrentStream.Position = _CurrentStreamPosition;
ActualReaded = Method(Buffer, Offset, Count);
});
Position += ActualReaded;
return ActualReaded;
}
}