NdsForge.NET 1.0.1
Read, validate, edit, compare, and build Nintendo DS and DSi images from .NET
Loading...
Searching...
No Matches
MemoryImageDataSource.cs
1namespace NdsForge;
2
4internal sealed class MemoryImageDataSource : IImageDataSource
5{
7 private readonly ReadOnlyMemory<byte> _data;
8
11 public MemoryImageDataSource(ReadOnlyMemory<byte> data)
12 {
13 _data = data;
14 }
15
17 public long Length => _data.Length;
18
20 public int Read(long offset, Span<byte> destination)
21 {
22 int count = GetReadLength(offset, destination.Length);
23 _data.Span.Slice(checked((int)offset), count).CopyTo(destination);
24 return count;
25 }
26
28 public ValueTask<int> ReadAsync(
29 long offset,
30 Memory<byte> destination,
31 CancellationToken cancellationToken)
32 {
33 cancellationToken.ThrowIfCancellationRequested();
34 return ValueTask.FromResult(Read(offset, destination.Span));
35 }
36
38 public void Dispose()
39 {
40 }
41
43 public ValueTask DisposeAsync() => ValueTask.CompletedTask;
44
49 private int GetReadLength(long offset, int requestedLength)
50 {
51 ArgumentOutOfRangeException.ThrowIfNegative(offset);
52 if (offset >= _data.Length)
53 {
54 return 0;
55 }
56
57 return (int)Math.Min(requestedLength, _data.Length - offset);
58 }
59}