@@ -199,9 +199,12 @@ private async Task<string> GetStringAsyncCore(HttpRequestMessage request, Cancel
199
199
200
200
// Since the underlying byte[] will never be exposed, we use an ArrayPool-backed
201
201
// stream to which we copy all of the data from the response.
202
- using Stream responseStream = c . TryReadAsStream ( ) ?? await c . ReadAsStreamAsync ( cts . Token ) . ConfigureAwait ( false ) ;
203
- using var buffer = new HttpContent . LimitArrayPoolWriteStream ( _maxResponseContentBufferSize , ( int ) c . Headers . ContentLength . GetValueOrDefault ( ) ) ;
202
+ using var buffer = new HttpContent . LimitArrayPoolWriteStream (
203
+ _maxResponseContentBufferSize ,
204
+ c . Headers . ContentLength . GetValueOrDefault ( ) ,
205
+ getFinalSizeFromPool : true ) ;
204
206
207
+ using Stream responseStream = c . TryReadAsStream ( ) ?? await c . ReadAsStreamAsync ( cts . Token ) . ConfigureAwait ( false ) ;
205
208
try
206
209
{
207
210
await responseStream . CopyToAsync ( buffer , cts . Token ) . ConfigureAwait ( false ) ;
@@ -211,14 +214,8 @@ private async Task<string> GetStringAsyncCore(HttpRequestMessage request, Cancel
211
214
throw HttpContent . WrapStreamCopyException ( e ) ;
212
215
}
213
216
214
- if ( buffer . Length > 0 )
215
- {
216
- // Decode and return the data from the buffer.
217
- return HttpContent . ReadBufferAsString ( buffer . GetBuffer ( ) , c . Headers ) ;
218
- }
219
-
220
- // No content to return.
221
- return string . Empty ;
217
+ // Decode and return the data from the buffer.
218
+ return HttpContent . ReadBufferAsString ( buffer , c . Headers ) ;
222
219
}
223
220
catch ( Exception e )
224
221
{
@@ -272,17 +269,16 @@ private async Task<byte[]> GetByteArrayAsyncCore(HttpRequestMessage request, Can
272
269
responseContentTelemetryStarted = true ;
273
270
}
274
271
275
- // If we got a content length, then we assume that it's correct and create a MemoryStream
276
- // to which the content will be transferred. That way, assuming we actually get the exact
277
- // amount we were expecting, we can simply return the MemoryStream's underlying buffer.
272
+ // If we got a content length, then we assume that it's correct. If that's the case,
273
+ // we can opportunistically allocate the exact-sized buffer while buffering the content.
278
274
// If we didn't get a content length, then we assume we're going to have to grow
279
275
// the buffer potentially several times and that it's unlikely the underlying buffer
280
276
// at the end will be the exact size needed, in which case it's more beneficial to use
281
277
// ArrayPool buffers and copy out to a new array at the end.
282
- long ? contentLength = c . Headers . ContentLength ;
283
- using Stream buffer = contentLength . HasValue ?
284
- new HttpContent . LimitMemoryStream ( _maxResponseContentBufferSize , ( int ) contentLength . GetValueOrDefault ( ) ) :
285
- new HttpContent . LimitArrayPoolWriteStream ( _maxResponseContentBufferSize ) ;
278
+ using var buffer = new HttpContent . LimitArrayPoolWriteStream (
279
+ _maxResponseContentBufferSize ,
280
+ c . Headers . ContentLength . GetValueOrDefault ( ) ,
281
+ getFinalSizeFromPool : false ) ;
286
282
287
283
using Stream responseStream = c . TryReadAsStream ( ) ?? await c . ReadAsStreamAsync ( cts . Token ) . ConfigureAwait ( false ) ;
288
284
try
@@ -294,10 +290,7 @@ private async Task<byte[]> GetByteArrayAsyncCore(HttpRequestMessage request, Can
294
290
throw HttpContent . WrapStreamCopyException ( e ) ;
295
291
}
296
292
297
- return
298
- buffer . Length == 0 ? Array . Empty < byte > ( ) :
299
- buffer is HttpContent . LimitMemoryStream lms ? lms . GetSizedBuffer ( ) :
300
- ( ( HttpContent . LimitArrayPoolWriteStream ) buffer ) . ToArray ( ) ;
293
+ return buffer . ToArray ( ) ;
301
294
}
302
295
catch ( Exception e )
303
296
{
0 commit comments