fix: Makes SpanWriter/SpanReader exceptions clearer (#2262)
This commit is contained in:
parent
5d920a25b1
commit
68caaab92c
2 changed files with 55 additions and 96 deletions
|
|
@ -1,6 +1,6 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2023 - ModernUO Development Team *
|
||||
* Copyright 2019-2025 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: SpanWriter.cs *
|
||||
* *
|
||||
|
|
@ -14,7 +14,6 @@
|
|||
*************************************************************************/
|
||||
|
||||
using System.Buffers.Binary;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
|
@ -56,14 +55,14 @@ public ref struct SpanWriter
|
|||
public Span<byte> RawBuffer => _buffer;
|
||||
|
||||
/**
|
||||
* Converts the writer to a Span<byte> using a SpanOwner.
|
||||
* If the buffer was stackalloc, it will be copied to a rented buffer.
|
||||
* Otherwise the existing rented buffer is used.
|
||||
*
|
||||
* Note:
|
||||
* Do not use the SpanWriter after calling this method.
|
||||
* This method will effectively dispose of the SpanWriter and is therefore considered terminal.
|
||||
*/
|
||||
* Converts the writer to a Span<byte> using a SpanOwner.
|
||||
* If the buffer was stackalloc, it will be copied to a rented buffer.
|
||||
* Otherwise the existing rented buffer is used.
|
||||
*
|
||||
* Note:
|
||||
* Do not use the SpanWriter after calling this method.
|
||||
* This method will effectively dispose of the SpanWriter and is therefore considered terminal.
|
||||
*/
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public SpanOwner ToSpan()
|
||||
{
|
||||
|
|
@ -117,11 +116,11 @@ public ref struct SpanWriter
|
|||
private void Grow(int additionalCapacity)
|
||||
{
|
||||
var newSize = Math.Max(BytesWritten + additionalCapacity, _buffer.Length * 2);
|
||||
byte[] poolArray = STArrayPool<byte>.Shared.Rent(newSize);
|
||||
var poolArray = STArrayPool<byte>.Shared.Rent(newSize);
|
||||
|
||||
_buffer[..BytesWritten].CopyTo(poolArray);
|
||||
|
||||
byte[] toReturn = _arrayToReturnToPool;
|
||||
var toReturn = _arrayToReturnToPool;
|
||||
_buffer = _arrayToReturnToPool = poolArray;
|
||||
if (toReturn != null)
|
||||
{
|
||||
|
|
@ -136,7 +135,7 @@ public ref struct SpanWriter
|
|||
{
|
||||
if (!_resize)
|
||||
{
|
||||
throw new OutOfMemoryException();
|
||||
throw new InvalidOperationException("Buffer is full and resizing is disabled.");
|
||||
}
|
||||
|
||||
Grow(count);
|
||||
|
|
@ -151,7 +150,7 @@ public ref struct SpanWriter
|
|||
{
|
||||
if (!_resize)
|
||||
{
|
||||
throw new OutOfMemoryException();
|
||||
throw new InvalidOperationException("Buffer is full and resizing is disabled.");
|
||||
}
|
||||
|
||||
Grow(capacity - BytesWritten);
|
||||
|
|
@ -400,46 +399,25 @@ public ref struct SpanWriter
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public int Seek(int offset, SeekOrigin origin)
|
||||
{
|
||||
Debug.Assert(
|
||||
origin != SeekOrigin.End || _resize || offset <= 0,
|
||||
"Attempting to seek to a position beyond capacity using SeekOrigin.End without resize"
|
||||
);
|
||||
|
||||
Debug.Assert(
|
||||
origin != SeekOrigin.End || offset >= -_buffer.Length,
|
||||
|
||||
"Attempting to seek to a negative position using SeekOrigin.End"
|
||||
);
|
||||
|
||||
Debug.Assert(
|
||||
origin != SeekOrigin.Begin || offset >= 0,
|
||||
"Attempting to seek to a negative position using SeekOrigin.Begin"
|
||||
);
|
||||
|
||||
Debug.Assert(
|
||||
origin != SeekOrigin.Begin || _resize || offset <= _buffer.Length,
|
||||
"Attempting to seek to a position beyond the capacity using SeekOrigin.Begin without resize"
|
||||
);
|
||||
|
||||
Debug.Assert(
|
||||
origin != SeekOrigin.Current || _position + offset >= 0,
|
||||
"Attempting to seek to a negative position using SeekOrigin.Current"
|
||||
);
|
||||
|
||||
Debug.Assert(
|
||||
origin != SeekOrigin.Current || _resize || _position + offset <= _buffer.Length,
|
||||
"Attempting to seek to a position beyond the capacity using SeekOrigin.Current without resize"
|
||||
);
|
||||
|
||||
var newPosition = Math.Max(0, origin switch
|
||||
var newPosition = origin switch
|
||||
{
|
||||
SeekOrigin.Current => _position + offset,
|
||||
SeekOrigin.End => BytesWritten + offset,
|
||||
_ => offset // Begin
|
||||
});
|
||||
};
|
||||
|
||||
if (newPosition < 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(offset), "Seek operation would result in a negative position.");
|
||||
}
|
||||
|
||||
if (newPosition > _buffer.Length)
|
||||
{
|
||||
if (!_resize)
|
||||
{
|
||||
throw new InvalidOperationException($"Cannot seek to position {newPosition} beyond buffer capacity {_buffer.Length} when resizing is disabled.");
|
||||
}
|
||||
|
||||
Grow(newPosition - _buffer.Length + 1);
|
||||
}
|
||||
|
||||
|
|
@ -449,7 +427,7 @@ public ref struct SpanWriter
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Dispose()
|
||||
{
|
||||
byte[] toReturn = _arrayToReturnToPool;
|
||||
var toReturn = _arrayToReturnToPool;
|
||||
this = default; // for safety, to avoid using pooled array if this instance is erroneously appended to again
|
||||
if (toReturn != null)
|
||||
{
|
||||
|
|
@ -478,7 +456,7 @@ public ref struct SpanWriter
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Dispose()
|
||||
{
|
||||
byte[] toReturn = _arrayToReturnToPool;
|
||||
var toReturn = _arrayToReturnToPool;
|
||||
this = default;
|
||||
if (_length > 0)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue