@@ -328,6 +328,9 @@ public override long Seek(
328328 }
329329
330330 /// <inheritdoc/>
331+ /// <exception cref="ObjectDisposedException"></exception>
332+ /// <exception cref="NotSupportedException">The stream buffer does not have the capacity to hold the data and is not expandable.</exception>
333+ /// <exception cref="ArgumentOutOfRangeException">The MemoryStream max size was exceeded</exception>
331334 public override void SetLength ( long value )
332335 {
333336 EnsureOpen ( ) ;
@@ -364,6 +367,12 @@ public virtual byte[] ToArray()
364367 }
365368
366369 /// <inheritdoc/>
370+ /// <exception cref="ObjectDisposedException"></exception>
371+ /// <exception cref="NotSupportedException">The stream buffer does not have the capacity to hold the data and is not expandable.</exception>
372+ /// <exception cref="ArgumentOutOfRangeException">
373+ /// The MemoryStream max size was exceeded or
374+ /// <paramref name="offset"/> or <paramref name="count"/> are less than 0
375+ /// </exception>
367376 public override void Write ( byte [ ] buffer , int offset , int count )
368377 {
369378 EnsureOpen ( ) ;
@@ -401,6 +410,9 @@ public override void Write(byte[] buffer, int offset, int count)
401410 }
402411
403412 /// <inheritdoc/>
413+ /// <exception cref="ObjectDisposedException"></exception>
414+ /// <exception cref="NotSupportedException">The stream buffer does not have the capacity to hold the data and is not expandable.</exception>
415+ /// <exception cref="ArgumentOutOfRangeException">The MemoryStream max size was exceeded</exception>
404416 public override void WriteByte ( byte value )
405417 {
406418 EnsureOpen ( ) ;
@@ -452,7 +464,8 @@ private void EnsureOpen()
452464 /// </summary>
453465 /// <param name="value">Value for the new capacity.</param>
454466 /// <returns><see langword="true"/> if allocation for a new array was successful. <see langword="false"/> otherwise.</returns>
455- /// <exception cref="NotSupportedException"></exception>
467+ /// <exception cref="NotSupportedException">The stream buffer does not have the capacity to hold the data and is not expandable.</exception>
468+ /// <exception cref="ArgumentOutOfRangeException">The MemoryStream max size would be exceeded by resizing</exception>
456469 private bool EnsureCapacity ( int value )
457470 {
458471 if ( value > _capacity )
@@ -464,11 +477,21 @@ private bool EnsureCapacity(int value)
464477 newCapacity = CapacityDefaultSize ;
465478 }
466479
480+ if ( value > MemStreamMaxLength )
481+ {
482+ throw new ArgumentOutOfRangeException ( ) ;
483+ }
484+
467485 if ( newCapacity < _capacity * 2 )
468486 {
469487 newCapacity = _capacity * 2 ;
470488 }
471489
490+ if ( newCapacity > MemStreamMaxLength )
491+ {
492+ newCapacity = MemStreamMaxLength ;
493+ }
494+
472495 if ( ! _expandable && newCapacity > _capacity )
473496 {
474497 throw new NotSupportedException ( ) ;
0 commit comments