Skip to content

Commit

Permalink
Improve exception throws
Browse files Browse the repository at this point in the history
  • Loading branch information
davidxuang committed Dec 13, 2023
1 parent d026083 commit 242f6e3
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions MusicDecrypto.Library/MarshalMemoryStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ public long Origin
get => _offset;
set
{
if (value > _length)
throw new ArgumentOutOfRangeException(nameof(value));
ArgumentOutOfRangeException.ThrowIfGreaterThan(value, _length);
_offset = value;
if (_offset > _position) _position = _offset;
}
Expand All @@ -59,14 +58,12 @@ public long Origin
#region Helpers
private void AssertNotClosed()
{
if (!_isOpen)
throw new ObjectDisposedException(nameof(MarshalMemoryStream));
ObjectDisposedException.ThrowIf(!_isOpen, typeof(MarshalMemoryStream));
}

private bool EnsureCapacity(long value)
{
if (value < 0)
throw new ArgumentOutOfRangeException(nameof(value));
ArgumentOutOfRangeException.ThrowIfNegative(value);

if (value > _capacity)
{
Expand Down Expand Up @@ -156,8 +153,7 @@ public override void SetLength(long value)
AssertNotClosed();
var n = checked(_offset + value);
_ = EnsureCapacity(n + SimdHelper.LaneCount - 1);
if (value < 0)
throw new ArgumentOutOfRangeException(nameof(value));
ArgumentOutOfRangeException.ThrowIfNegative(value);
_length = n;
if (_position > _length) _position = _length;
}
Expand All @@ -166,8 +162,7 @@ public void SetLengthWithPadding(long value)
AssertNotClosed();
var n = checked(_offset + value);
_ = EnsureCapacity(n + 0x1000); // 4KiB reserved
if (value < 0)
throw new ArgumentOutOfRangeException(nameof(value));
ArgumentOutOfRangeException.ThrowIfNegative(value);
_length = n;
if (_position > _length) _position = _length;
}
Expand All @@ -183,8 +178,7 @@ public override long Position
set
{
AssertNotClosed();
if (value < 0)
throw new ArgumentOutOfRangeException(nameof(value));
ArgumentOutOfRangeException.ThrowIfNegative(value);
_position = checked(_offset + value);
}
}
Expand Down Expand Up @@ -222,8 +216,7 @@ public override void CopyTo(Stream destination, int bufferSize)
{
AssertNotClosed();

if (bufferSize <= 0)
throw new ArgumentOutOfRangeException(nameof(bufferSize));
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(bufferSize);
if (!destination.CanWrite)
{
if (destination.CanSeek)
Expand Down

0 comments on commit 242f6e3

Please sign in to comment.