-
-
Notifications
You must be signed in to change notification settings - Fork 167
/
GitPackDeltafiedStream.cs
207 lines (173 loc) · 6.53 KB
/
GitPackDeltafiedStream.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#nullable enable
using System;
using System.Buffers;
using System.Diagnostics;
using System.IO;
namespace Nerdbank.GitVersioning.ManagedGit
{
/// <summary>
/// Reads data from a deltafied object.
/// </summary>
/// <seealso href="https://git-scm.com/docs/pack-format#_deltified_representation"/>
public class GitPackDeltafiedStream : Stream
{
private readonly long length;
private long position;
private readonly Stream baseStream;
private readonly Stream deltaStream;
private DeltaInstruction? current;
private int offset;
/// <summary>
/// Initializes a new instance of the <see cref="GitPackDeltafiedStream"/> class.
/// </summary>
/// <param name="baseStream">
/// The base stream to which the deltas are applied.
/// </param>
/// <param name="deltaStream">
/// A <see cref="Stream"/> which contains a sequence of <see cref="DeltaInstruction"/>s.
/// </param>
public GitPackDeltafiedStream(Stream baseStream, Stream deltaStream)
{
this.baseStream = baseStream ?? throw new ArgumentNullException(nameof(baseStream));
this.deltaStream = deltaStream ?? throw new ArgumentNullException(nameof(deltaStream));
int baseObjectlength = deltaStream.ReadMbsInt();
this.length = deltaStream.ReadMbsInt();
}
/// <summary>
/// Gets the base stream to which the deltas are applied.
/// </summary>
public Stream BaseStream => this.baseStream;
/// <inheritdoc/>
public override bool CanRead => true;
/// <inheritdoc/>
public override bool CanSeek => false;
/// <inheritdoc/>
public override bool CanWrite => false;
/// <inheritdoc/>
public override long Length => this.length;
/// <inheritdoc/>
public override long Position
{
get => this.position;
set => throw new NotImplementedException();
}
#if NETSTANDARD
/// <summary>
/// Reads a sequence of bytes from the current <see cref="GitPackDeltafiedStream"/> and advances the position
/// within the stream by the number of bytes read.
/// </summary>
/// <param name="span">
/// A region of memory. When this method returns, the contents of this region are replaced by the bytes
/// read from the current source.
/// </param>
/// <returns>
/// The total number of bytes read into the buffer. This can be less than the number of bytes allocated
/// in the buffer if that many bytes are not currently available, or zero (0) if the end of the stream
/// has been reached.
/// </returns>
public int Read(Span<byte> span)
#else
/// <inheritdoc/>
public override int Read(Span<byte> span)
#endif
{
int read = 0;
int canRead;
int didRead;
while (read < span.Length && this.TryGetInstruction(out DeltaInstruction instruction))
{
var source = instruction.InstructionType == DeltaInstructionType.Copy ? this.baseStream : this.deltaStream;
Debug.Assert(instruction.Size > this.offset);
Debug.Assert(source.Position + instruction.Size - this.offset <= source.Length);
canRead = Math.Min(span.Length - read, instruction.Size - this.offset);
didRead = source.Read(span.Slice(read, canRead));
Debug.Assert(didRead != 0);
read += didRead;
this.offset += didRead;
}
this.position += read;
Debug.Assert(read <= span.Length);
return read;
}
/// <inheritdoc/>
public override int Read(byte[] buffer, int offset, int count)
{
return this.Read(buffer.AsSpan(offset, count));
}
/// <inheritdoc/>
public override void Flush()
{
throw new NotImplementedException();
}
/// <inheritdoc/>
public override long Seek(long offset, SeekOrigin origin)
{
if (origin == SeekOrigin.Begin && offset == this.position)
{
return this.position;
}
if (origin == SeekOrigin.Current && offset == 0)
{
return this.position;
}
if (origin == SeekOrigin.Begin && offset > this.position)
{
// We can optimise this by skipping over instructions rather than executing them
int length = (int)(offset - this.position);
byte[] buffer = ArrayPool<byte>.Shared.Rent(length);
this.Read(buffer, 0, length);
ArrayPool<byte>.Shared.Return(buffer);
return this.position;
}
else
{
throw new NotImplementedException();
}
}
/// <inheritdoc/>
public override void SetLength(long value)
{
throw new NotImplementedException();
}
/// <inheritdoc/>
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotImplementedException();
}
/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
this.deltaStream.Dispose();
this.baseStream.Dispose();
}
private bool TryGetInstruction(out DeltaInstruction instruction)
{
if (this.current is not null && this.offset < this.current.Value.Size)
{
instruction = this.current.Value;
return true;
}
this.current = DeltaStreamReader.Read(this.deltaStream);
if (this.current is null)
{
instruction = default;
return false;
}
instruction = this.current.Value;
switch (instruction.InstructionType)
{
case DeltaInstructionType.Copy:
this.baseStream.Seek(instruction.Offset, SeekOrigin.Begin);
Debug.Assert(this.baseStream.Position == instruction.Offset);
this.offset = 0;
break;
case DeltaInstructionType.Insert:
this.offset = 0;
break;
default:
throw new GitException();
}
return true;
}
}
}