1
1
using System . Buffers ;
2
2
using System . Collections ;
3
+ using System . IO . Pipelines ;
3
4
using System . Runtime . CompilerServices ;
4
5
using System . Text ;
5
6
using System . Text . Json ;
@@ -226,29 +227,24 @@ private async ValueTask FormatInternalAsync(
226
227
Stream outputStream ,
227
228
CancellationToken cancellationToken = default )
228
229
{
229
- using var buffer = new ArrayWriter ( ) ;
230
- FormatInternal ( result , buffer ) ;
231
-
232
- await outputStream
233
- . WriteAsync ( buffer . GetWrittenMemory ( ) , cancellationToken )
234
- . ConfigureAwait ( false ) ;
235
-
236
- await outputStream . FlushAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
230
+ var writer = PipeWriter . Create ( outputStream , new StreamPipeWriterOptions ( leaveOpen : true ) ) ;
231
+ FormatInternal ( result , writer ) ;
232
+ await writer . CompleteAsync ( ) . ConfigureAwait ( false ) ;
237
233
}
238
234
239
235
private async ValueTask FormatInternalAsync (
240
236
OperationResultBatch resultBatch ,
241
237
Stream outputStream ,
242
238
CancellationToken cancellationToken = default )
243
239
{
244
- using var buffer = new ArrayWriter ( ) ;
240
+ var writer = PipeWriter . Create ( outputStream , new StreamPipeWriterOptions ( leaveOpen : true ) ) ;
245
241
246
242
foreach ( var result in resultBatch . Results )
247
243
{
248
244
switch ( result )
249
245
{
250
246
case IOperationResult singleResult :
251
- FormatInternal ( singleResult , buffer ) ;
247
+ FormatInternal ( singleResult , writer ) ;
252
248
break ;
253
249
254
250
case IResponseStream batchResult :
@@ -259,7 +255,7 @@ private async ValueTask FormatInternalAsync(
259
255
{
260
256
try
261
257
{
262
- FormatInternal ( partialResult , buffer ) ;
258
+ FormatInternal ( partialResult , writer ) ;
263
259
}
264
260
finally
265
261
{
@@ -271,39 +267,31 @@ private async ValueTask FormatInternalAsync(
271
267
}
272
268
}
273
269
274
- await outputStream
275
- . WriteAsync ( buffer . GetWrittenMemory ( ) , cancellationToken )
276
- . ConfigureAwait ( false ) ;
277
-
278
- await outputStream . FlushAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
270
+ await writer . CompleteAsync ( ) . ConfigureAwait ( false ) ;
279
271
}
280
272
281
273
private async ValueTask FormatInternalAsync (
282
274
IResponseStream batchResult ,
283
275
Stream outputStream ,
284
276
CancellationToken cancellationToken = default )
285
277
{
286
- using var buffer = new ArrayWriter ( ) ;
278
+ var writer = PipeWriter . Create ( outputStream , new StreamPipeWriterOptions ( leaveOpen : true ) ) ;
287
279
288
280
await foreach ( var partialResult in batchResult . ReadResultsAsync ( )
289
281
. WithCancellation ( cancellationToken )
290
282
. ConfigureAwait ( false ) )
291
283
{
292
284
try
293
285
{
294
- FormatInternal ( partialResult , buffer ) ;
286
+ FormatInternal ( partialResult , writer ) ;
295
287
}
296
288
finally
297
289
{
298
290
await partialResult . DisposeAsync ( ) . ConfigureAwait ( false ) ;
299
291
}
300
292
}
301
293
302
- await outputStream
303
- . WriteAsync ( buffer . GetWrittenMemory ( ) , cancellationToken )
304
- . ConfigureAwait ( false ) ;
305
-
306
- await outputStream . FlushAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
294
+ await writer . CompleteAsync ( ) . ConfigureAwait ( false ) ;
307
295
}
308
296
309
297
private void WriteResult ( Utf8JsonWriter writer , IOperationResult result )
0 commit comments