-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAlignedMemoryReader.cs
39 lines (36 loc) · 1.06 KB
/
AlignedMemoryReader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using System;
using System.Diagnostics;
using System.IO;
using Ara3D.Buffers;
namespace Ara3D.StepParser
{
public static class AlignedMemoryReader
{
public static unsafe AlignedMemory ReadAllBytes(string path, int bufferSize = 1024 * 1024)
{
using var fs = new FileStream(path,
FileMode.Open,
FileAccess.Read,
FileShare.Read,
bufferSize,
false);
var fileLength = fs.Length;
if (fileLength > int.MaxValue)
throw new IOException("File too big: > 2GB");
var count = (int)fileLength;
var r = new AlignedMemory(count);
var pBytes = r.BytePtr;
while (count > 0)
{
var span = new Span<byte>(pBytes, count);
var n = fs.Read(span);
if (n == 0)
break;
pBytes += n;
count -= n;
}
Debug.Assert(count == 0);
return r;
}
}
}