Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1ca17cd

Browse files
authored
Override Read(Span)/ReadByte on PipeReaderStream (#55086)
We can do better than the base implementations for reading. For writing, there's not a lot we can do better than the base.
1 parenta48a11b commit1ca17cd

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

‎src/libraries/System.IO.Pipelines/src/System/IO/Pipelines/PipeReaderStream.cs‎

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,28 @@ public override void Flush()
4545
{
4646
}
4747

48-
publicoverrideintRead(byte[]buffer,intoffset,intcount)=>
49-
ReadAsync(buffer,offset,count).GetAwaiter().GetResult();
48+
publicoverrideintRead(byte[]buffer,intoffset,intcount)
49+
{
50+
if(bufferisnull)
51+
{
52+
thrownewArgumentNullException(nameof(buffer));
53+
}
54+
55+
returnReadInternal(newSpan<byte>(buffer,offset,count));
56+
}
57+
58+
publicoverrideintReadByte()
59+
{
60+
Span<byte>oneByte=stackallocbyte[1];
61+
returnReadInternal(oneByte)==0?-1:oneByte[0];
62+
}
63+
64+
privateintReadInternal(Span<byte>buffer)
65+
{
66+
ValueTask<ReadResult>vt=_pipeReader.ReadAsync();
67+
ReadResultresult=vt.IsCompletedSuccessfully?vt.Result:vt.AsTask().Result;
68+
returnHandleReadResult(result,buffer);
69+
}
5070

5171
publicoverridelongSeek(longoffset,SeekOriginorigin)=>thrownewNotSupportedException();
5272

@@ -71,6 +91,11 @@ public override Task<int> ReadAsync(byte[] buffer, int offset, int count, Cancel
7191
}
7292

7393
#if(!NETSTANDARD2_0&&!NETFRAMEWORK)
94+
publicoverrideintRead(Span<byte>buffer)
95+
{
96+
returnReadInternal(buffer);
97+
}
98+
7499
publicoverrideValueTask<int>ReadAsync(Memory<byte>buffer,CancellationTokencancellationToken=default)
75100
{
76101
returnReadAsyncInternal(buffer,cancellationToken);
@@ -80,7 +105,11 @@ public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken
80105
privateasyncValueTask<int>ReadAsyncInternal(Memory<byte>buffer,CancellationTokencancellationToken)
81106
{
82107
ReadResultresult=await_pipeReader.ReadAsync(cancellationToken).ConfigureAwait(false);
108+
returnHandleReadResult(result,buffer.Span);
109+
}
83110

111+
privateintHandleReadResult(ReadResultresult,Span<byte>buffer)
112+
{
84113
if(result.IsCanceled)
85114
{
86115
ThrowHelper.ThrowOperationCanceledException_ReadCanceled();
@@ -98,7 +127,7 @@ private async ValueTask<int> ReadAsyncInternal(Memory<byte> buffer, Cancellation
98127

99128
ReadOnlySequence<byte>slice=actual==bufferLength?sequence:sequence.Slice(0,actual);
100129
consumed=slice.End;
101-
slice.CopyTo(buffer.Span);
130+
slice.CopyTo(buffer);
102131

103132
returnactual;
104133
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp