-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathSectorStream.cs
115 lines (99 loc) · 2.65 KB
/
SectorStream.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;
using System.IO;
namespace XTSSharp;
public class SectorStream : Stream
{
private readonly Stream _baseStream;
private readonly long _offset;
private ulong _currentSector;
public int SectorSize
{
get;
private set;
}
public override bool CanRead => _baseStream.CanRead;
public override bool CanSeek => _baseStream.CanSeek;
public override bool CanWrite => _baseStream.CanWrite;
public override long Length => _baseStream.Length - _offset;
public override long Position
{
get
{
return _baseStream.Position - _offset;
}
set
{
ValidateSizeMultiple(value);
_baseStream.Position = value + _offset;
_currentSector = (ulong)(value / SectorSize);
}
}
protected ulong CurrentSector => _currentSector;
public SectorStream(Stream baseStream, int sectorSize)
: this(baseStream, sectorSize, 0L)
{
}
public SectorStream(Stream baseStream, int sectorSize, long offset)
{
SectorSize = sectorSize;
_baseStream = baseStream;
_offset = offset;
}
private void ValidateSizeMultiple(long value)
{
if (value % SectorSize == 0L)
{
return;
}
throw new ArgumentException($"Value needs to be a multiple of {SectorSize}");
}
protected void ValidateSize(long value)
{
if (value == SectorSize)
{
return;
}
throw new ArgumentException($"Value needs to be {SectorSize}");
}
protected void ValidateSize(int value)
{
if (value == SectorSize)
{
return;
}
throw new ArgumentException($"Value needs to be {SectorSize}");
}
public override void Flush()
{
_baseStream.Flush();
}
public override long Seek(long offset, SeekOrigin origin)
{
long num = origin switch
{
SeekOrigin.Begin => offset,
SeekOrigin.End => Length - offset,
_ => Position + offset,
};
Position = num;
return num;
}
public override void SetLength(long value)
{
ValidateSizeMultiple(value);
_baseStream.SetLength(value);
}
public override int Read(byte[] buffer, int offset, int count)
{
ValidateSize(count);
int result = _baseStream.Read(buffer, offset, count);
_currentSector++;
return result;
}
public override void Write(byte[] buffer, int offset, int count)
{
ValidateSize(count);
_baseStream.Write(buffer, offset, count);
_currentSector++;
}
}