@@ -1,6 +1,5 @@
|
||||
using System.Numerics;
|
||||
using System.Runtime.InteropServices;
|
||||
using Just.Core.Extensions;
|
||||
|
||||
namespace Just.Core.Collections;
|
||||
|
||||
@@ -122,14 +121,14 @@ public static class DataMap
|
||||
where T : unmanaged, IComparisonOperators<T, T, bool>, IEqualityOperators<T, T, bool>
|
||||
{
|
||||
byte[] headBytes = new byte[HeaderSize];
|
||||
await stream.PopulateAsync(headBytes, cancellationToken);
|
||||
await stream.ReadExactlyAsync(headBytes, cancellationToken);
|
||||
var head = MemoryMarshal.Read<Header>(headBytes);
|
||||
|
||||
var bodySize = DataMap<T>.ElementSize * head.Width * head.Height;
|
||||
if (bodySize != head.BodySize) throw new InvalidOperationException("Can not read DataMap. Element size mismatch.");
|
||||
|
||||
byte[] bodyBytes = new byte[bodySize];
|
||||
await stream.PopulateAsync(bodyBytes, cancellationToken);
|
||||
await stream.ReadExactlyAsync(bodyBytes, cancellationToken);
|
||||
|
||||
T[] body = MemoryMarshal.Cast<byte, T>(bodyBytes).ToArray();
|
||||
return new DataMap<T>((int)head.Width, (int)head.Height, body);
|
||||
@@ -157,7 +156,7 @@ public static class DataMap
|
||||
{
|
||||
Header head = default;
|
||||
var headSpan = MemoryMarshal.AsBytes(MemoryMarshal.CreateSpan(ref head, 1));
|
||||
stream.Populate(headSpan);
|
||||
stream.ReadExactly(headSpan);
|
||||
|
||||
var bodySize = DataMap<T>.ElementSize * head.Width * head.Height;
|
||||
if (bodySize != head.BodySize) throw new InvalidOperationException("Can not read DataMap. Element size mismatch.");
|
||||
@@ -165,7 +164,7 @@ public static class DataMap
|
||||
T[] body = new T[bodySize];
|
||||
var bodySpan = MemoryMarshal.AsBytes(body.AsSpan());
|
||||
|
||||
stream.Populate(bodySpan);
|
||||
stream.ReadExactly(bodySpan);
|
||||
|
||||
return new DataMap<T>((int)head.Width, (int)head.Height, body);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user