4internal sealed class MemoryImageDataSource : IImageDataSource
7 private readonly ReadOnlyMemory<byte> _data;
11 public MemoryImageDataSource(ReadOnlyMemory<byte> data)
17 public long Length => _data.Length;
20 public int Read(
long offset, Span<byte> destination)
22 int count = GetReadLength(offset, destination.Length);
23 _data.Span.Slice(checked((
int)offset), count).CopyTo(destination);
28 public ValueTask<int> ReadAsync(
30 Memory<byte> destination,
31 CancellationToken cancellationToken)
33 cancellationToken.ThrowIfCancellationRequested();
34 return ValueTask.FromResult(Read(offset, destination.Span));
43 public ValueTask DisposeAsync() => ValueTask.CompletedTask;
49 private int GetReadLength(
long offset,
int requestedLength)
51 ArgumentOutOfRangeException.ThrowIfNegative(offset);
52 if (offset >= _data.Length)
57 return (
int)Math.Min(requestedLength, _data.Length - offset);