@@ -30,40 +30,46 @@ private sealed class ChunkedEncodingReadStream : HttpContentReadStream
3030
3131 public ChunkedEncodingReadStream ( HttpConnection connection ) : base ( connection ) { }
3232
33- public override ValueTask < int > ReadAsync ( Memory < byte > buffer , CancellationToken cancellationToken )
33+ public override Task < int > ReadAsync ( byte [ ] buffer , int offset , int count , CancellationToken cancellationToken )
34+ {
35+ ValidateBufferArgs ( buffer , offset , count ) ;
36+ return ReadAsyncInternal ( new Memory < byte > ( buffer , offset , count ) , cancellationToken ) ;
37+ }
38+
39+ private Task < int > ReadAsyncInternal ( Memory < byte > buffer , CancellationToken cancellationToken )
3440 {
3541 if ( cancellationToken . IsCancellationRequested )
3642 {
3743 // Cancellation requested.
38- return new ValueTask < int > ( Task . FromCanceled < int > ( cancellationToken ) ) ;
44+ return Task . FromCanceled < int > ( cancellationToken ) ;
3945 }
4046
4147 if ( _connection == null || buffer . Length == 0 )
4248 {
4349 // Response body fully consumed or the caller didn't ask for any data.
44- return new ValueTask < int > ( 0 ) ;
50+ return Task . FromResult ( 0 ) ;
4551 }
4652
4753 // Try to consume from data we already have in the buffer.
48- int bytesRead = ReadChunksFromConnectionBuffer ( buffer . Span , cancellationRegistration : default ) ;
54+ int bytesRead = ReadChunksFromConnectionBuffer ( buffer . Span , cancellationRegistration : default , cancellationToken ) ;
4955 if ( bytesRead > 0 )
5056 {
51- return new ValueTask < int > ( bytesRead ) ;
57+ return Task . FromResult ( bytesRead ) ;
5258 }
5359
5460 // We may have just consumed the remainder of the response (with no actual data
5561 // available), so check again.
5662 if ( _connection == null )
5763 {
5864 Debug . Assert ( _state == ParsingState . Done ) ;
59- return new ValueTask < int > ( 0 ) ;
65+ return Task . FromResult ( 0 ) ;
6066 }
6167
6268 // Nothing available to consume. Fall back to I/O.
6369 return ReadAsyncCore ( buffer , cancellationToken ) ;
6470 }
6571
66- private async ValueTask < int > ReadAsyncCore ( Memory < byte > buffer , CancellationToken cancellationToken )
72+ private async Task < int > ReadAsyncCore ( Memory < byte > buffer , CancellationToken cancellationToken )
6773 {
6874 // Should only be called if ReadChunksFromConnectionBuffer returned 0.
6975
@@ -108,7 +114,7 @@ private async ValueTask<int> ReadAsyncCore(Memory<byte> buffer, CancellationToke
108114
109115 // Now that we have more, see if we can get any response data, and if
110116 // we can we're done.
111- int bytesCopied = ReadChunksFromConnectionBuffer ( buffer . Span , ctr ) ;
117+ int bytesCopied = ReadChunksFromConnectionBuffer ( buffer . Span , ctr , cancellationToken ) ;
112118 if ( bytesCopied > 0 )
113119 {
114120 return bytesCopied ;
@@ -144,7 +150,7 @@ private async Task CopyToAsyncCore(Stream destination, CancellationToken cancell
144150 {
145151 while ( true )
146152 {
147- ReadOnlyMemory < byte > bytesRead = ReadChunkFromConnectionBuffer ( int . MaxValue , ctr ) ;
153+ ReadOnlyMemory < byte > bytesRead = ReadChunkFromConnectionBuffer ( int . MaxValue , ctr , cancellationToken ) ;
148154 if ( bytesRead . Length == 0 )
149155 {
150156 break ;
@@ -171,12 +177,12 @@ private async Task CopyToAsyncCore(Stream destination, CancellationToken cancell
171177 }
172178 }
173179
174- private int ReadChunksFromConnectionBuffer ( Span < byte > buffer , CancellationTokenRegistration cancellationRegistration )
180+ private int ReadChunksFromConnectionBuffer ( Span < byte > buffer , CancellationTokenRegistration cancellationRegistration , CancellationToken cancellationToken )
175181 {
176182 int totalBytesRead = 0 ;
177183 while ( buffer . Length > 0 )
178184 {
179- ReadOnlyMemory < byte > bytesRead = ReadChunkFromConnectionBuffer ( buffer . Length , cancellationRegistration ) ;
185+ ReadOnlyMemory < byte > bytesRead = ReadChunkFromConnectionBuffer ( buffer . Length , cancellationRegistration , cancellationToken ) ;
180186 Debug . Assert ( bytesRead . Length <= buffer . Length ) ;
181187 if ( bytesRead . Length == 0 )
182188 {
@@ -190,7 +196,7 @@ private int ReadChunksFromConnectionBuffer(Span<byte> buffer, CancellationTokenR
190196 return totalBytesRead ;
191197 }
192198
193- private ReadOnlyMemory < byte > ReadChunkFromConnectionBuffer ( int maxBytesToRead , CancellationTokenRegistration cancellationRegistration )
199+ private ReadOnlyMemory < byte > ReadChunkFromConnectionBuffer ( int maxBytesToRead , CancellationTokenRegistration cancellationRegistration , CancellationToken cancellationToken = default )
194200 {
195201 Debug . Assert ( maxBytesToRead > 0 ) ;
196202
@@ -295,7 +301,7 @@ private ReadOnlyMemory<byte> ReadChunkFromConnectionBuffer(int maxBytesToRead, C
295301 // (e.g. if a timer is used and has already queued its callback but the
296302 // callback hasn't yet run).
297303 cancellationRegistration . Dispose ( ) ;
298- CancellationHelper . ThrowIfCancellationRequested ( cancellationRegistration . Token ) ;
304+ CancellationHelper . ThrowIfCancellationRequested ( cancellationToken ) ;
299305
300306 _state = ParsingState . Done ;
301307 _connection . CompleteResponse ( ) ;
0 commit comments