// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.IO;
using System.Runtime.CompilerServices;
namespace CommunityToolkit.HighPerformance.Streams;
///
/// A factory class to produce instances.
///
internal static partial class MemoryStream
{
///
/// Validates the argument (it needs to be in the [0, length]) range.
///
/// The new value being set.
/// The maximum length of the target .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ValidatePosition(long position, int length)
{
if ((ulong)position > (ulong)length)
{
ThrowArgumentOutOfRangeExceptionForPosition();
}
}
///
/// Validates the or arguments.
///
/// The target array.
/// The offset within .
/// The number of elements to process within .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ValidateBuffer(byte[]? buffer, int offset, int count)
{
if (buffer is null)
{
ThrowArgumentNullExceptionForBuffer();
}
if (offset < 0)
{
ThrowArgumentOutOfRangeExceptionForOffset();
}
if (count < 0)
{
ThrowArgumentOutOfRangeExceptionForCount();
}
if (offset + count > buffer!.Length)
{
ThrowArgumentExceptionForLength();
}
}
///
/// Validates the property.
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ValidateCanWrite(bool canWrite)
{
if (!canWrite)
{
ThrowNotSupportedException();
}
}
///
/// Validates that a given instance hasn't been disposed.
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ValidateDisposed(bool disposed)
{
if (disposed)
{
ThrowObjectDisposedException();
}
}
}