@@ -511,28 +511,12 @@ public override Task FlushAsync(CancellationToken cancellationToken)
511511 /// </remarks>
512512 public override int Read ( byte [ ] buffer , int offset , int count )
513513 {
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 .
531516#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 ;
536520
537521 // Lock down the file stream while we do this.
538522 lock ( _lock )
@@ -653,28 +637,14 @@ public override int Read(byte[] buffer, int offset, int count)
653637 /// <returns>A <see cref="Task" /> that represents the asynchronous read operation.</returns>
654638 public override async Task < int > ReadAsync ( byte [ ] buffer , int offset , int count , CancellationToken cancellationToken )
655639 {
656- var readLen = 0 ;
657-
658- ThrowHelper . ThrowIfNull ( buffer ) ;
640+ #if ! NET
641+ ThrowHelper .
642+ #endif
643+ ValidateBufferArguments ( buffer , offset , count ) ;
659644
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 ( ) ;
668646
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 ;
678648
679649 CheckSessionIsOpen ( ) ;
680650
@@ -952,14 +922,7 @@ public override long Seek(long offset, SeekOrigin origin)
952922 /// </remarks>
953923 public override void SetLength ( long value )
954924 {
955- #if NET
956- ArgumentOutOfRangeException . ThrowIfNegative ( value ) ;
957- #else
958- if ( value < 0 )
959- {
960- throw new ArgumentOutOfRangeException ( nameof ( value ) ) ;
961- }
962- #endif
925+ ThrowHelper . ThrowIfNegative ( value ) ;
963926
964927 // Lock down the file stream while we do this.
965928 lock ( _lock )
@@ -1005,26 +968,10 @@ public override void SetLength(long value)
1005968 /// <exception cref="ObjectDisposedException">Methods were called after the stream was closed.</exception>
1006969 public override void Write ( byte [ ] buffer , int offset , int count )
1007970 {
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- }
971+ #if ! NET
972+ ThrowHelper .
1023973#endif
1024- if ( ( buffer . Length - offset ) < count )
1025- {
1026- throw new ArgumentException ( "Invalid array range." ) ;
1027- }
974+ ValidateBufferArguments ( buffer , offset , count ) ;
1028975
1029976 // Lock down the file stream while we do this.
1030977 lock ( _lock )
@@ -1105,26 +1052,12 @@ public override void Write(byte[] buffer, int offset, int count)
11051052 /// <exception cref="ObjectDisposedException">Methods were called after the stream was closed.</exception>
11061053 public override async Task WriteAsync ( byte [ ] buffer , int offset , int count , CancellationToken cancellationToken )
11071054 {
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- }
1055+ #if ! NET
1056+ ThrowHelper .
11231057#endif
1124- if ( ( buffer . Length - offset ) < count )
1125- {
1126- throw new ArgumentException ( "Invalid array range." ) ;
1127- }
1058+ ValidateBufferArguments ( buffer , offset , count ) ;
1059+
1060+ cancellationToken . ThrowIfCancellationRequested ( ) ;
11281061
11291062 CheckSessionIsOpen ( ) ;
11301063
0 commit comments