@@ -226,11 +226,11 @@ internal int InternalGetPosition()
226226 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
227227 internal ReadOnlySpan < byte > InternalReadSpan ( int count )
228228 {
229+ EnsureNotClosed ( ) ;
230+
229231 if ( _memoryData is not null )
230232 return _memoryData . InternalReadSpan ( this , count ) ;
231233
232- EnsureNotClosed ( ) ;
233-
234234 int origPos = _position ;
235235 int newPos = origPos + count ;
236236
@@ -266,17 +266,16 @@ internal int InternalEmulateRead(int count)
266266 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
267267 internal ReadOnlySpan < byte > InternalRead ( int count )
268268 {
269+ EnsureNotClosed ( ) ;
270+
269271 if ( _memoryData is not null )
270272 {
271- EnsureNotClosed ( ) ;
272273 int n = Math . Min ( _length - _position , count ) ;
273274 if ( n <= 0 )
274275 return default ;
275276 return _memoryData . InternalReadSpan ( this , n ) ;
276277 }
277278
278- EnsureNotClosed ( ) ;
279-
280279 int available = _length - _position ;
281280 if ( available > count )
282281 available = count ;
@@ -362,12 +361,11 @@ public override long Position
362361 public override int Read ( byte [ ] buffer , int offset , int count )
363362 {
364363 ValidateBufferArguments ( buffer , offset , count ) ;
364+ EnsureNotClosed ( ) ;
365365
366366 if ( _memoryData is not null )
367367 return _memoryData . Read ( this , new Span < byte > ( buffer , offset , count ) ) ;
368368
369- EnsureNotClosed ( ) ;
370-
371369 int n = _length - _position ;
372370 if ( n > count )
373371 n = count ;
@@ -399,11 +397,11 @@ public override int Read(Span<byte> buffer)
399397 return base . Read ( buffer ) ;
400398 }
401399
400+ EnsureNotClosed ( ) ;
401+
402402 if ( _memoryData is not null )
403403 return _memoryData . Read ( this , buffer ) ;
404404
405- EnsureNotClosed ( ) ;
406-
407405 int n = Math . Min ( _length - _position , buffer . Length ) ;
408406 if ( n <= 0 )
409407 return 0 ;
@@ -475,11 +473,11 @@ public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken
475473
476474 public override int ReadByte ( )
477475 {
476+ EnsureNotClosed ( ) ;
477+
478478 if ( _memoryData is not null )
479479 return _memoryData . ReadByte ( this ) ;
480480
481- EnsureNotClosed ( ) ;
482-
483481 if ( _position >= _length )
484482 return - 1 ;
485483
@@ -500,15 +498,14 @@ public override void CopyTo(Stream destination, int bufferSize)
500498
501499 // Validate the arguments the same way Stream does for back-compat.
502500 ValidateCopyToArguments ( destination , bufferSize ) ;
501+ EnsureNotClosed ( ) ;
503502
504503 if ( _memoryData is not null )
505504 {
506505 _memoryData . CopyTo ( this , destination ) ;
507506 return ;
508507 }
509508
510- EnsureNotClosed ( ) ;
511-
512509 int originalPosition = _position ;
513510
514511 // Seek to the end of the MemoryStream.
@@ -528,12 +525,11 @@ public override Task CopyToAsync(Stream destination, int bufferSize, Cancellatio
528525 // This implementation offers better performance compared to the base class version.
529526
530527 ValidateCopyToArguments ( destination , bufferSize ) ;
528+ EnsureNotClosed ( ) ;
531529
532530 if ( _memoryData is not null )
533531 return _memoryData . CopyToAsync ( this , destination , cancellationToken ) ;
534532
535- EnsureNotClosed ( ) ;
536-
537533 // If we have been inherited into a subclass, the following implementation could be incorrect
538534 // since it does not call through to ReadAsync() which a subclass might have overridden.
539535 // To be safe we will only use this implementation in cases where we know it is safe to do so,
@@ -610,17 +606,17 @@ private long SeekCore(long offset, int loc)
610606 //
611607 public override void SetLength ( long value )
612608 {
609+ if ( value < 0 || value > MemStreamMaxLength )
610+ throw new ArgumentOutOfRangeException ( nameof ( value ) , SR . Format ( SR . ArgumentOutOfRange_StreamLength , Array . MaxLength ) ) ;
611+
612+ EnsureWriteable ( ) ;
613+
613614 if ( _memoryData is not null )
614615 {
615616 _memoryData . SetLength ( this , value ) ;
616617 return ;
617618 }
618619
619- if ( value < 0 || value > MemStreamMaxLength )
620- throw new ArgumentOutOfRangeException ( nameof ( value ) , SR . Format ( SR . ArgumentOutOfRange_StreamLength , Array . MaxLength ) ) ;
621-
622- EnsureWriteable ( ) ;
623-
624620 // Origin wasn't publicly exposed above.
625621 Debug . Assert ( MemStreamMaxLength == Array . MaxLength ) ; // Check parameter validation logic in this method if this fails.
626622 if ( value > ( MemStreamMaxLength - _origin ) )
@@ -650,16 +646,15 @@ public virtual byte[] ToArray()
650646 public override void Write ( byte [ ] buffer , int offset , int count )
651647 {
652648 ValidateBufferArguments ( buffer , offset , count ) ;
649+ EnsureNotClosed ( ) ;
650+ EnsureWriteable ( ) ;
653651
654652 if ( _memoryData is not null )
655653 {
656654 _memoryData . Write ( this , new ReadOnlySpan < byte > ( buffer , offset , count ) ) ;
657655 return ;
658656 }
659657
660- EnsureNotClosed ( ) ;
661- EnsureWriteable ( ) ;
662-
663658 int i = _position + count ;
664659 // Check for overflow
665660 if ( i < 0 )
@@ -708,15 +703,15 @@ public override void Write(ReadOnlySpan<byte> buffer)
708703 return ;
709704 }
710705
706+ EnsureNotClosed ( ) ;
707+ EnsureWriteable ( ) ;
708+
711709 if ( _memoryData is not null )
712710 {
713711 _memoryData . Write ( this , buffer ) ;
714712 return ;
715713 }
716714
717- EnsureNotClosed ( ) ;
718- EnsureWriteable ( ) ;
719-
720715 // Check for overflow
721716 int i = _position + buffer . Length ;
722717 if ( i < 0 )
@@ -800,15 +795,15 @@ public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationTo
800795
801796 public override void WriteByte ( byte value )
802797 {
798+ EnsureNotClosed ( ) ;
799+ EnsureWriteable ( ) ;
800+
803801 if ( _memoryData is not null )
804802 {
805803 _memoryData . WriteByte ( this , value ) ;
806804 return ;
807805 }
808806
809- EnsureNotClosed ( ) ;
810- EnsureWriteable ( ) ;
811-
812807 if ( _position >= _length )
813808 {
814809 int newLength = _position + 1 ;
@@ -833,16 +828,15 @@ public override void WriteByte(byte value)
833828 // Writes this MemoryStream to another stream.
834829 public virtual void WriteTo ( Stream stream )
835830 {
831+ ArgumentNullException . ThrowIfNull ( stream ) ;
832+ EnsureNotClosed ( ) ;
833+
836834 if ( _memoryData is not null )
837835 {
838836 _memoryData . WriteTo ( this , stream ) ;
839837 return ;
840838 }
841839
842- ArgumentNullException . ThrowIfNull ( stream ) ;
843-
844- EnsureNotClosed ( ) ;
845-
846840 stream . Write ( _buffer , _origin , _length - _origin ) ;
847841 }
848842 }
0 commit comments