-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathFilePositionStorage.cs
90 lines (77 loc) · 1.77 KB
/
FilePositionStorage.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
using System;
using System.IO;
namespace LibHac.Fs
{
public class FilePositionStorage : StorageBase
{
private IFile BaseFile { get; }
private long Position = 0;
public FilePositionStorage(IFile baseFile)
: this(baseFile, false)
{
}
public FilePositionStorage(IFile baseFile, bool canAutoExpand)
{
BaseFile = baseFile;
CanAutoExpand = canAutoExpand;
}
protected override void ReadImpl(Span<byte> destination, long offset)
{
Position = offset;
BaseFile.Read(destination, offset);
Position += destination.Length;
}
public void Read(Span<byte> destination)
{
BaseFile.Read(destination, Position);
Position += destination.Length;
}
protected override void WriteImpl(ReadOnlySpan<byte> source, long offset)
{
Position = offset;
ExtendFileSize(offset + source.Length);
BaseFile.Write(source, offset);
Position += source.Length;
}
public void Write(ReadOnlySpan<byte> source)
{
ExtendFileSize(Position + source.Length);
BaseFile.Write(source, Position);
Position += source.Length;
}
public override void Flush()
{
BaseFile.Flush();
}
public override long GetSize() => BaseFile.GetSize();
public override void SetSize(long size)
{
BaseFile.SetSize(size);
}
public void ExtendFileSize(long newSize)
{
var fileSize = GetSize();
if (newSize > fileSize)
{
BaseFile.SetSize(newSize);
}
}
public void Seek(long value, SeekOrigin origin = SeekOrigin.Begin)
{
switch (origin)
{
case SeekOrigin.Begin:
Position = value;
break;
case SeekOrigin.Current:
Position += value;
break;
case SeekOrigin.End:
Position = GetSize() - value;
break;
default:
throw new NotImplementedException($"Unknown SeekOrigin: {origin}");
}
}
}
}