Skip to content

Commit

Permalink
Fixed completion of decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
sakno committed Sep 21, 2023
1 parent 2d79111 commit f5dee28
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions src/DotNext.IO/IO/Pipelines/PipeExtensions.Readers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -774,30 +774,36 @@ public static async ValueTask ReadUtf8Async(this PipeReader reader, IBufferWrite
ArgumentNullException.ThrowIfNull(output);

var decoder = Encoding.UTF8.GetDecoder();
SequencePosition consumed;
bool completed;
ReadResult result;

do
{
var readResult = await reader.ReadAsync(token).ConfigureAwait(false);
var buffer = readResult.Buffer;
result = await reader.ReadAsync(token).ConfigureAwait(false);
}
while (!ConvertToUtf8(decoder, reader, in result, output, token));

if (buffer.PositionOf(DecodingContext.Utf8NullChar).TryGetValue(out consumed))
static bool ConvertToUtf8(Decoder decoder, PipeReader reader, in ReadResult result, IBufferWriter<char> output, CancellationToken token)
{
bool completed;
var buffer = result.Buffer;

if (buffer.PositionOf(DecodingContext.Utf8NullChar).TryGetValue(out var consumed))
{
buffer = buffer.Slice(0, consumed);
completed = true;
consumed = readResult.Buffer.GetPosition(1L, consumed);
consumed = result.Buffer.GetPosition(1L, consumed);
}
else
{
completed = buffer.IsEmpty;
completed = result.IsCompleted;
consumed = buffer.End;
}

decoder.Convert(in buffer, output, readResult.IsCompleted, out _, out _);
readResult.ThrowIfCancellationRequested(reader, token);
decoder.Convert(in buffer, output, completed, out _, out _);
result.ThrowIfCancellationRequested(reader, token);
reader.AdvanceTo(consumed);

return completed;
}
while (!completed);
}
}

0 comments on commit f5dee28

Please sign in to comment.