@@ -119,7 +119,7 @@ public static byte[] ReadFully(this Stream input, byte[] buffer)
119
119
/// <summary>
120
120
/// Reads the given stream up to the end, returning the MemoryStream Buffer as ReadOnlyMemory<byte>.
121
121
/// </summary>
122
- public static ReadOnlyMemory < byte > ReadFullyAsMemory ( this Stream input ) =>
122
+ public static ReadOnlyMemory < byte > ReadFullyAsMemory ( this Stream input ) =>
123
123
ReadFullyAsMemory ( input , DefaultBufferSize ) ;
124
124
125
125
/// <summary>
@@ -176,8 +176,8 @@ public static long CopyTo(this Stream input, Stream output, int bufferSize)
176
176
}
177
177
178
178
/// <summary>
179
- /// Copies all the data from one stream into another, using the given
180
- /// buffer for transferring data. Note that the current contents of
179
+ /// Copies all the data from one stream into another, using the given
180
+ /// buffer for transferring data. Note that the current contents of
181
181
/// the buffer is ignored, so the buffer needn't be cleared beforehand.
182
182
/// </summary>
183
183
public static long CopyTo ( this Stream input , Stream output , byte [ ] buffer )
@@ -313,18 +313,18 @@ public static byte[] Combine(this byte[] bytes, params byte[][] withBytes)
313
313
public static int AsyncBufferSize = 81920 ; // CopyToAsync() default value
314
314
315
315
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
316
- public static Task WriteAsync ( this Stream stream , ReadOnlyMemory < byte > value , CancellationToken token = default ) =>
316
+ public static Task WriteAsync ( this Stream stream , ReadOnlyMemory < byte > value , CancellationToken token = default ) =>
317
317
MemoryProvider . Instance . WriteAsync ( stream , value , token ) ;
318
318
319
319
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
320
- public static Task WriteAsync ( this Stream stream , byte [ ] bytes , CancellationToken token = default ) =>
320
+ public static Task WriteAsync ( this Stream stream , byte [ ] bytes , CancellationToken token = default ) =>
321
321
MemoryProvider . Instance . WriteAsync ( stream , bytes , token ) ;
322
322
323
323
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
324
324
public static Task CopyToAsync ( this Stream input , Stream output , CancellationToken token = default ) => input . CopyToAsync ( output , AsyncBufferSize , token ) ;
325
325
326
326
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
327
- public static Task WriteAsync ( this Stream stream , string text , CancellationToken token = default ) =>
327
+ public static Task WriteAsync ( this Stream stream , string text , CancellationToken token = default ) =>
328
328
MemoryProvider . Instance . WriteAsync ( stream , text . AsSpan ( ) , token ) ;
329
329
330
330
public static string ToMd5Hash ( this Stream stream )
@@ -361,78 +361,118 @@ public static MemoryStream InMemoryStream(this byte[] bytes)
361
361
public static string ReadToEnd ( this MemoryStream ms , Encoding encoding )
362
362
{
363
363
ms . Position = 0 ;
364
+
365
+ #if NETSTANDARD || NETCORE2_1
366
+ if ( ms . TryGetBuffer ( out var buffer ) )
367
+ {
368
+ return encoding . GetString ( buffer . Array , buffer . Offset , buffer . Count ) ;
369
+ }
370
+ #else
364
371
try
365
372
{
366
- var ret = encoding . GetString ( ms . GetBuffer ( ) , 0 , ( int ) ms . Length ) ;
367
- return ret ;
373
+ return encoding . GetString ( ms . GetBuffer ( ) , 0 , ( int ) ms . Length ) ;
368
374
}
369
375
catch ( UnauthorizedAccessException )
370
376
{
371
- Tracer . Instance . WriteWarning ( "MemoryStream wasn't created with a publiclyVisible:true byte[] bufffer, falling back to slow impl" ) ;
372
-
373
- using ( var reader = new StreamReader ( ms , encoding , true , DefaultBufferSize , leaveOpen : true ) )
374
- {
375
- return reader . ReadToEnd ( ) ;
376
- }
377
+ }
378
+ #endif
379
+
380
+ Tracer . Instance . WriteWarning ( "MemoryStream wasn't created with a publiclyVisible:true byte[] buffer, falling back to slow impl" ) ;
381
+
382
+ using ( var reader = new StreamReader ( ms , encoding , true , DefaultBufferSize , leaveOpen : true ) )
383
+ {
384
+ return reader . ReadToEnd ( ) ;
377
385
}
378
386
}
379
387
380
388
public static ReadOnlyMemory < byte > GetBufferAsMemory ( this MemoryStream ms )
381
389
{
390
+ #if NETSTANDARD || NETCORE2_1
391
+ if ( ms . TryGetBuffer ( out var buffer ) )
392
+ {
393
+ return new ReadOnlyMemory < byte > ( buffer . Array , buffer . Offset , buffer . Count ) ;
394
+ }
395
+ #else
382
396
try
383
397
{
384
- return new ReadOnlyMemory < byte > ( ms . GetBuffer ( ) , 0 , ( int ) ms . Length ) ;
398
+ return new ReadOnlyMemory < byte > ( ms . GetBuffer ( ) , 0 , ( int ) ms . Length ) ;
385
399
}
386
400
catch ( UnauthorizedAccessException )
387
401
{
388
- Tracer . Instance . WriteWarning ( "MemoryStream in GetBufferAsSpan() wasn't created with a publiclyVisible:true byte[] bufffer, falling back to slow impl" ) ;
389
- return new ReadOnlyMemory < byte > ( ms . ToArray ( ) ) ;
390
402
}
403
+ #endif
404
+
405
+ Tracer . Instance . WriteWarning ( "MemoryStream in GetBufferAsSpan() wasn't created with a publiclyVisible:true byte[] buffer, falling back to slow impl" ) ;
406
+ return new ReadOnlyMemory < byte > ( ms . ToArray ( ) ) ;
391
407
}
392
408
393
409
public static ReadOnlySpan < byte > GetBufferAsSpan ( this MemoryStream ms )
394
410
{
411
+ #if NETSTANDARD || NETCORE2_1
412
+ if ( ms . TryGetBuffer ( out var buffer ) )
413
+ {
414
+ return new ReadOnlySpan < byte > ( buffer . Array , buffer . Offset , buffer . Count ) ;
415
+ }
416
+ #else
395
417
try
396
418
{
397
- return new ReadOnlySpan < byte > ( ms . GetBuffer ( ) , 0 , ( int ) ms . Length ) ;
419
+ return new ReadOnlySpan < byte > ( ms . GetBuffer ( ) , 0 , ( int ) ms . Length ) ;
398
420
}
399
421
catch ( UnauthorizedAccessException )
400
422
{
401
- Tracer . Instance . WriteWarning ( "MemoryStream in GetBufferAsSpan() wasn't created with a publiclyVisible:true byte[] bufffer, falling back to slow impl" ) ;
402
- return new ReadOnlySpan < byte > ( ms . ToArray ( ) ) ;
403
423
}
424
+ #endif
425
+
426
+ Tracer . Instance . WriteWarning ( "MemoryStream in GetBufferAsSpan() wasn't created with a publiclyVisible:true byte[] buffer, falling back to slow impl" ) ;
427
+ return new ReadOnlySpan < byte > ( ms . ToArray ( ) ) ;
404
428
}
405
429
406
430
public static byte [ ] GetBufferAsBytes ( this MemoryStream ms )
407
431
{
432
+ #if NETSTANDARD || NETCORE2_1
433
+ if ( ms . TryGetBuffer ( out var buffer ) )
434
+ {
435
+ return buffer . Array ;
436
+ }
437
+ #else
408
438
try
409
439
{
410
440
return ms . GetBuffer ( ) ;
411
441
}
412
442
catch ( UnauthorizedAccessException )
413
443
{
414
- Tracer . Instance . WriteWarning ( "MemoryStream in GetBufferAsBytes() wasn't created with a publiclyVisible:true byte[] bufffer, falling back to slow impl" ) ;
415
- return ms . ToArray ( ) ;
416
444
}
445
+ #endif
446
+
447
+ Tracer . Instance . WriteWarning ( "MemoryStream in GetBufferAsBytes() wasn't created with a publiclyVisible:true byte[] buffer, falling back to slow impl" ) ;
448
+ return ms . ToArray ( ) ;
417
449
}
418
450
419
451
public static Task < string > ReadToEndAsync ( this MemoryStream ms ) => ReadToEndAsync ( ms , JsConfig . UTF8Encoding ) ;
420
452
public static Task < string > ReadToEndAsync ( this MemoryStream ms , Encoding encoding )
421
453
{
422
454
ms . Position = 0 ;
455
+
456
+ #if NETSTANDARD || NETCORE2_1
457
+ if ( ms . TryGetBuffer ( out var buffer ) )
458
+ {
459
+ return encoding . GetString ( buffer . Array , buffer . Offset , buffer . Count ) . InTask ( ) ;
460
+ }
461
+ #else
423
462
try
424
463
{
425
- var ret = encoding . GetString ( ms . GetBuffer ( ) , 0 , ( int ) ms . Length ) ;
426
- return ret . InTask ( ) ;
464
+ return encoding . GetString ( ms . GetBuffer ( ) , 0 , ( int ) ms . Length ) . InTask ( ) ;
427
465
}
428
466
catch ( UnauthorizedAccessException )
429
467
{
430
- Tracer . Instance . WriteWarning ( "MemoryStream in ReadToEndAsync() wasn't created with a publiclyVisible:true byte[] bufffer, falling back to slow impl" ) ;
431
-
432
- using ( var reader = new StreamReader ( ms , encoding , true , DefaultBufferSize , leaveOpen : true ) )
433
- {
434
- return reader . ReadToEndAsync ( ) ;
435
- }
468
+ }
469
+ #endif
470
+
471
+ Tracer . Instance . WriteWarning ( "MemoryStream in ReadToEndAsync() wasn't created with a publiclyVisible:true byte[] buffer, falling back to slow impl" ) ;
472
+
473
+ using ( var reader = new StreamReader ( ms , encoding , true , DefaultBufferSize , leaveOpen : true ) )
474
+ {
475
+ return reader . ReadToEndAsync ( ) ;
436
476
}
437
477
}
438
478
@@ -446,7 +486,7 @@ public static string ReadToEnd(this Stream stream, Encoding encoding)
446
486
{
447
487
stream . Position = 0 ;
448
488
}
449
-
489
+
450
490
using ( var reader = new StreamReader ( stream , encoding , true , DefaultBufferSize , leaveOpen : true ) )
451
491
{
452
492
return reader . ReadToEnd ( ) ;
@@ -463,40 +503,49 @@ public static Task<string> ReadToEndAsync(this Stream stream, Encoding encoding)
463
503
{
464
504
stream . Position = 0 ;
465
505
}
466
-
506
+
467
507
using ( var reader = new StreamReader ( stream , encoding , true , DefaultBufferSize , leaveOpen : true ) )
468
508
{
469
509
return reader . ReadToEndAsync ( ) ;
470
510
}
471
511
}
472
512
473
- public static Task WriteToAsync ( this MemoryStream stream , Stream output , CancellationToken token = default ( CancellationToken ) ) =>
513
+ public static Task WriteToAsync ( this MemoryStream stream , Stream output , CancellationToken token = default ( CancellationToken ) ) =>
474
514
WriteToAsync ( stream , output , JsConfig . UTF8Encoding , token ) ;
475
-
515
+
476
516
public static async Task WriteToAsync ( this MemoryStream stream , Stream output , Encoding encoding , CancellationToken token )
477
517
{
518
+ #if NETSTANDARD || NETCORE2_1
519
+ if ( stream . TryGetBuffer ( out var buffer ) )
520
+ {
521
+ await output . WriteAsync ( buffer . Array , buffer . Offset , buffer . Count , token ) . ConfigAwait ( ) ;
522
+ return ;
523
+ }
524
+ #else
478
525
try
479
526
{
480
527
await output . WriteAsync ( stream . GetBuffer ( ) , 0 , ( int ) stream . Length , token ) . ConfigAwait ( ) ;
528
+ return ;
481
529
}
482
530
catch ( UnauthorizedAccessException )
483
531
{
484
- Tracer . Instance . WriteWarning ( "MemoryStream in WriteToAsync() wasn't created with a publiclyVisible:true byte[] bufffer, falling back to slow impl" ) ;
485
-
486
- var bytes = stream . ToArray ( ) ;
487
- await output . WriteAsync ( bytes , 0 , bytes . Length , token ) . ConfigAwait ( ) ;
488
532
}
533
+ #endif
534
+ Tracer . Instance . WriteWarning ( "MemoryStream in WriteToAsync() wasn't created with a publiclyVisible:true byte[] bufffer, falling back to slow impl" ) ;
535
+
536
+ var bytes = stream . ToArray ( ) ;
537
+ await output . WriteAsync ( bytes , 0 , bytes . Length , token ) . ConfigAwait ( ) ;
489
538
}
490
-
491
- public static Task WriteToAsync ( this Stream stream , Stream output , CancellationToken token = default ( CancellationToken ) ) =>
539
+
540
+ public static Task WriteToAsync ( this Stream stream , Stream output , CancellationToken token = default ( CancellationToken ) ) =>
492
541
WriteToAsync ( stream , output , JsConfig . UTF8Encoding , token ) ;
493
-
494
-
542
+
543
+
495
544
public static Task WriteToAsync ( this Stream stream , Stream output , Encoding encoding , CancellationToken token )
496
545
{
497
546
if ( stream is MemoryStream ms )
498
547
return ms . WriteToAsync ( output , encoding , token ) ;
499
-
548
+
500
549
return stream . CopyToAsync ( output , token ) ;
501
550
}
502
551
@@ -516,4 +565,4 @@ public static async Task<MemoryStream> CopyToNewMemoryStreamAsync(this Stream st
516
565
return ms ;
517
566
}
518
567
}
519
- }
568
+ }
0 commit comments