NdsForge.NET 1.0.1
Read, validate, edit, compare, and build Nintendo DS and DSi images from .NET
Loading...
Searching...
No Matches
ImageDataSourceExtensions.cs
1namespace NdsForge;
2
4internal static class ImageDataSourceExtensions
5{
11 public static void ReadExactly(this IImageDataSource source, long offset, Span<byte> destination)
12 {
13 int totalRead = 0;
14 while (totalRead < destination.Length)
15 {
16 int count = source.Read(offset + totalRead, destination[totalRead..]);
17 if (count == 0)
18 {
19 throw new EndOfStreamException("The image ended before the requested region was read.");
20 }
21
22 totalRead += count;
23 }
24 }
25
32 public static async ValueTask ReadExactlyAsync(
33 this IImageDataSource source,
34 long offset,
35 Memory<byte> destination,
36 CancellationToken cancellationToken)
37 {
38 int totalRead = 0;
39 while (totalRead < destination.Length)
40 {
41 int count = await source.ReadAsync(
42 offset + totalRead,
43 destination[totalRead..],
44 cancellationToken).ConfigureAwait(false);
45 if (count == 0)
46 {
47 throw new EndOfStreamException("The image ended before the requested region was read.");
48 }
49
50 totalRead += count;
51 }
52 }
53}