NdsForge.NET 1.0.1
Read, validate, edit, compare, and build Nintendo DS and DSi images from .NET
Loading...
Searching...
No Matches
FileImageDataSource.cs
1using Microsoft.Win32.SafeHandles;
2
3namespace NdsForge;
4
6internal sealed class FileImageDataSource : IImageDataSource
7{
9 private readonly SafeFileHandle _handle;
10
13 public FileImageDataSource(string path)
14 {
15 _handle = File.OpenHandle(
16 path,
17 FileMode.Open,
18 FileAccess.Read,
19 FileShare.Read,
20 FileOptions.Asynchronous | FileOptions.RandomAccess);
21 Length = RandomAccess.GetLength(_handle);
22 }
23
25 public long Length { get; }
26
28 public int Read(long offset, Span<byte> destination) => RandomAccess.Read(_handle, destination, offset);
29
31 public ValueTask<int> ReadAsync(long offset, Memory<byte> destination, CancellationToken cancellationToken) =>
32 RandomAccess.ReadAsync(_handle, destination, offset, cancellationToken);
33
35 public void Dispose() => _handle.Dispose();
36
38 public ValueTask DisposeAsync()
39 {
40 _handle.Dispose();
41 return ValueTask.CompletedTask;
42 }
43}