@@ -511,28 +511,12 @@ public override Task FlushAsync(CancellationToken cancellationToken)
511
511
/// </remarks>
512
512
public override int Read ( byte [ ] buffer , int offset , int count )
513
513
{
514
- var readLen = 0 ;
515
-
516
- ThrowHelper . ThrowIfNull ( buffer ) ;
517
-
518
- #if NET
519
- ArgumentOutOfRangeException . ThrowIfNegative ( offset ) ;
520
- ArgumentOutOfRangeException . ThrowIfNegative ( count ) ;
521
- #else
522
- if ( offset < 0 )
523
- {
524
- throw new ArgumentOutOfRangeException ( nameof ( offset ) ) ;
525
- }
526
-
527
- if ( count < 0 )
528
- {
529
- throw new ArgumentOutOfRangeException ( nameof ( count ) ) ;
530
- }
514
+ #if ! NET
515
+ ThrowHelper .
531
516
#endif
532
- if ( ( buffer . Length - offset ) < count )
533
- {
534
- throw new ArgumentException ( "Invalid array range." ) ;
535
- }
517
+ ValidateBufferArguments ( buffer , offset , count ) ;
518
+
519
+ var readLen = 0 ;
536
520
537
521
// Lock down the file stream while we do this.
538
522
lock ( _lock )
@@ -653,28 +637,14 @@ public override int Read(byte[] buffer, int offset, int count)
653
637
/// <returns>A <see cref="Task" /> that represents the asynchronous read operation.</returns>
654
638
public override async Task < int > ReadAsync ( byte [ ] buffer , int offset , int count , CancellationToken cancellationToken )
655
639
{
656
- var readLen = 0 ;
657
-
658
- ThrowHelper . ThrowIfNull ( buffer ) ;
640
+ #if ! NET
641
+ ThrowHelper .
642
+ #endif
643
+ ValidateBufferArguments ( buffer , offset , count ) ;
659
644
660
- #if NET
661
- ArgumentOutOfRangeException . ThrowIfNegative ( offset ) ;
662
- ArgumentOutOfRangeException . ThrowIfNegative ( count ) ;
663
- #else
664
- if ( offset < 0 )
665
- {
666
- throw new ArgumentOutOfRangeException ( nameof ( offset ) ) ;
667
- }
645
+ cancellationToken . ThrowIfCancellationRequested ( ) ;
668
646
669
- if ( count < 0 )
670
- {
671
- throw new ArgumentOutOfRangeException ( nameof ( count ) ) ;
672
- }
673
- #endif
674
- if ( ( buffer . Length - offset ) < count )
675
- {
676
- throw new ArgumentException ( "Invalid array range." ) ;
677
- }
647
+ var readLen = 0 ;
678
648
679
649
CheckSessionIsOpen ( ) ;
680
650
@@ -1005,26 +975,10 @@ public override void SetLength(long value)
1005
975
/// <exception cref="ObjectDisposedException">Methods were called after the stream was closed.</exception>
1006
976
public override void Write ( byte [ ] buffer , int offset , int count )
1007
977
{
1008
- ThrowHelper . ThrowIfNull ( buffer ) ;
1009
-
1010
- #if NET
1011
- ArgumentOutOfRangeException . ThrowIfNegative ( offset ) ;
1012
- ArgumentOutOfRangeException . ThrowIfNegative ( count ) ;
1013
- #else
1014
- if ( offset < 0 )
1015
- {
1016
- throw new ArgumentOutOfRangeException ( nameof ( offset ) ) ;
1017
- }
1018
-
1019
- if ( count < 0 )
1020
- {
1021
- throw new ArgumentOutOfRangeException ( nameof ( count ) ) ;
1022
- }
978
+ #if ! NET
979
+ ThrowHelper .
1023
980
#endif
1024
- if ( ( buffer . Length - offset ) < count )
1025
- {
1026
- throw new ArgumentException ( "Invalid array range." ) ;
1027
- }
981
+ ValidateBufferArguments ( buffer , offset , count ) ;
1028
982
1029
983
// Lock down the file stream while we do this.
1030
984
lock ( _lock )
@@ -1105,26 +1059,12 @@ public override void Write(byte[] buffer, int offset, int count)
1105
1059
/// <exception cref="ObjectDisposedException">Methods were called after the stream was closed.</exception>
1106
1060
public override async Task WriteAsync ( byte [ ] buffer , int offset , int count , CancellationToken cancellationToken )
1107
1061
{
1108
- ThrowHelper . ThrowIfNull ( buffer ) ;
1109
-
1110
- #if NET
1111
- ArgumentOutOfRangeException . ThrowIfNegative ( offset ) ;
1112
- ArgumentOutOfRangeException . ThrowIfNegative ( count ) ;
1113
- #else
1114
- if ( offset < 0 )
1115
- {
1116
- throw new ArgumentOutOfRangeException ( nameof ( offset ) ) ;
1117
- }
1118
-
1119
- if ( count < 0 )
1120
- {
1121
- throw new ArgumentOutOfRangeException ( nameof ( count ) ) ;
1122
- }
1062
+ #if ! NET
1063
+ ThrowHelper .
1123
1064
#endif
1124
- if ( ( buffer . Length - offset ) < count )
1125
- {
1126
- throw new ArgumentException ( "Invalid array range." ) ;
1127
- }
1065
+ ValidateBufferArguments ( buffer , offset , count ) ;
1066
+
1067
+ cancellationToken . ThrowIfCancellationRequested ( ) ;
1128
1068
1129
1069
CheckSessionIsOpen ( ) ;
1130
1070
0 commit comments