namespace EvilEngine.Mounts; public sealed class StreamAccess : IRandomAccess { private readonly Lock _lock = new(); private readonly Stream _stream; public StreamAccess(Stream stream) { if (!stream.CanSeek || !stream.CanRead) throw new ArgumentException("Reading at an offset needs a readable, seekable stream.", nameof(stream)); _stream = stream; Length = stream.Length; } public long Length { get; } public int Read(long offset, Span buffer) { if (offset >= Length) return 0; var wanted = (int)Math.Min(buffer.Length, Length - offset); lock (_lock) { _stream.Position = offset; return _stream.ReadAtLeast(buffer[..wanted], wanted, false); } } public void Dispose() { lock (_lock) { _stream.Dispose(); } } }