Skip to content

Commit bd7655e

Browse files
committed
Merge branch '4-ensure-that-readresult-is-not-default-before-first-read' into 'master'
Resolve "Ensure that ReadResult is not default before first read." Closes #4 See merge request marta/kaitai_struct_csharp_runtime!9
2 parents 797617d + e389f4b commit bd7655e

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

Kaitai.Struct.Runtime.Async/ReaderContext/PipeReaderContext.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ public async ValueTask<long> GetSize()
2929

3030
public async ValueTask<bool> IsEof()
3131
{
32-
if (_readResult.Equals(default(ReadResult)) ||
33-
Position >= _readResult.Buffer.Length && !_readResult.IsCompleted)
32+
await EnsureReadResultIsNotDefault();
33+
34+
if (Position >= _readResult.Buffer.Length && !_readResult.IsCompleted)
3435
{
3536
_pipeReader.AdvanceTo(_readResult.Buffer.Start, _readResult.Buffer.GetPosition(Position));
3637
_readResult = await _pipeReader.ReadAsync();
@@ -48,6 +49,8 @@ public async ValueTask SeekAsync(long position)
4849
}
4950
else
5051
{
52+
await EnsureReadResultIsNotDefault();
53+
5154
while (_readResult.Buffer.Length < position && !_readResult.IsCompleted)
5255
{
5356
_pipeReader.AdvanceTo(_readResult.Buffer.Start, _readResult.Buffer.End);
@@ -70,6 +73,8 @@ public async ValueTask SeekAsync(long position)
7073

7174
public async ValueTask<byte> ReadByteAsync()
7275
{
76+
await EnsureReadResultIsNotDefault();
77+
7378
var value = byte.MinValue;
7479
while (!TryReadByte(out value) && !_readResult.IsCompleted)
7580
{
@@ -96,6 +101,8 @@ public async ValueTask<byte[]> ReadBytesAsync(long count)
96101
$"requested {count} bytes, while only non-negative int32 amount of bytes possible");
97102
}
98103

104+
await EnsureReadResultIsNotDefault();
105+
99106
byte[] value = null;
100107

101108
while (!TryRead(out value, count))
@@ -136,13 +143,23 @@ public virtual async ValueTask<byte[]> ReadBytesFullAsync()
136143
return value;
137144
}
138145

139-
private async Task FillReadResultBufferToTheEnd()
146+
private async ValueTask FillReadResultBufferToTheEnd()
140147
{
148+
await EnsureReadResultIsNotDefault();
149+
141150
while (!_readResult.IsCompleted)
142151
{
143152
_pipeReader.AdvanceTo(_readResult.Buffer.Start, _readResult.Buffer.End);
144153
_readResult = await _pipeReader.ReadAsync();
145154
}
146155
}
156+
157+
private async ValueTask EnsureReadResultIsNotDefault()
158+
{
159+
if (_readResult.Equals(default(ReadResult)))
160+
{
161+
_readResult = await _pipeReader.ReadAsync();
162+
}
163+
}
147164
}
148165
}

0 commit comments

Comments
 (0)