You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Zero/ZeroLevel/Services/Memory/MMFViewAccessor.cs

49 lines
1.3 KiB

using System;
using System.IO;
using System.IO.MemoryMappedFiles;
namespace ZeroLevel.Services.Memory
{
internal class MMFViewAccessor
: IViewAccessor
{
private readonly MemoryMappedViewStream _accessor;
private readonly long _absoluteOffset;
public MMFViewAccessor(MemoryMappedViewStream accessor, long offset)
{
_accessor = accessor;
_absoluteOffset = offset;
}
public bool EOV => _accessor.Position >= _accessor.Length;
public long Position => _absoluteOffset + _accessor.Position;
public bool CheckOutOfRange(int offset)
{
return offset < 0 || (_accessor.Position + offset) > _accessor.Length;
}
public void Dispose()
{
_accessor?.Dispose();
}
public byte[] ReadBuffer(int count)
{
if (count == 0) return null;
var buffer = new byte[count];
var readedCount = _accessor.Read(buffer, 0, count);
if (count != readedCount)
throw new InvalidOperationException($"The stream returned less data ({count} bytes) than expected ({readedCount} bytes)");
return buffer;
}
public void Seek(long offset)
{
_accessor.Seek(offset, SeekOrigin.Begin);
}
}
}

Powered by TurnKey Linux.