-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFileSector.cs
65 lines (57 loc) · 1.93 KB
/
FileSector.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using ItzWarty;
namespace Dargon.VirtualFileMaps
{
[Guid("5DB2B4C2-39AE-4629-988A-CFFFCE89F230")]
public class FileSector : ISector
{
private string path;
private long offset;
private long length;
public FileSector() { }
public FileSector(string path, long offset, long length)
{
this.path = path;
this.offset = offset;
this.length = length;
}
public string Path => path;
public long Offset => offset;
public long Size { get { return length; } }
public IEnumerable<KeyValuePair<SectorRange, ISector>> Segment(SectorRange currentRange, IEnumerable<SectorRange> newRanges) {
foreach (var piece in newRanges) {
yield return piece.PairValue(
(ISector)new FileSector(
path,
offset + (piece.startInclusive - currentRange.startInclusive),
piece.endExclusive - piece.startInclusive
)
);
}
}
public void Read(long readOffset, long readLength, byte[] buffer, long bufferOffset)
{
using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)) {
fs.Seek(offset + readOffset, SeekOrigin.Begin);
var bytesRemaining = readLength;
while (bytesRemaining > 0) {
bytesRemaining -= fs.Read(buffer, (int)bufferOffset, (int)bytesRemaining);
}
}
}
public void Serialize(BinaryWriter writer)
{
writer.WriteNullTerminatedString(path);
writer.Write((long)offset);
writer.Write((long)length);
}
public void Deserialize(BinaryReader reader)
{
path = reader.ReadNullTerminatedString();
offset = reader.ReadInt64();
length = reader.ReadInt64();
}
}
}