Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,43 @@ public int ReadArray<T>(long position, T[] array, int offset, int count) where T
return n;
}

public int Read(long position, Span<byte> buffer)
{
if (!_isOpen)
{
throw new ObjectDisposedException(nameof(UnmanagedMemoryAccessor), SR.ObjectDisposed_ViewAccessorClosed);
}
if (!_canRead)
{
throw new NotSupportedException(SR.NotSupported_Reading);
}
ArgumentOutOfRangeException.ThrowIfNegative(position);

if (position >= _capacity)
{
throw new ArgumentOutOfRangeException(nameof(position), SR.ArgumentOutOfRange_PositionLessThanCapacityRequired);
}

int n = buffer.Length;
long spaceLeft = _capacity - position;
if (spaceLeft < 0)
{
n = 0;
}
else
{
ulong spaceNeeded = (ulong)(n);
if ((ulong)spaceLeft < spaceNeeded)
{
n = (int)(spaceLeft);
}
}

_buffer.ReadSpan((ulong)(_offset + position), buffer.Slice(0, n));

return n;
}

// ************** Write Methods ****************/

public void Write(long position, bool value) => Write(position, (byte)(value ? 1 : 0));
Expand Down Expand Up @@ -564,6 +601,26 @@ public void WriteArray<T>(long position, T[] array, int offset, int count) where
_buffer.WriteArray((ulong)(_offset + position), array, offset, count);
}

public void Write(long position, ReadOnlySpan<byte> buffer)
{
ArgumentOutOfRangeException.ThrowIfNegative(position);
if (position >= Capacity)
{
throw new ArgumentOutOfRangeException(nameof(position), SR.ArgumentOutOfRange_PositionLessThanCapacityRequired);
}

if (!_isOpen)
{
throw new ObjectDisposedException(nameof(UnmanagedMemoryAccessor), SR.ObjectDisposed_ViewAccessorClosed);
}
if (!_canWrite)
{
throw new NotSupportedException(SR.NotSupported_Writing);
}

_buffer.WriteSpan((ulong)(_offset + position), buffer);
}

private void EnsureSafeToRead(long position, int sizeOfType)
{
if (!_isOpen)
Expand Down
Loading