// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// CommunityToolkit.HighPerformance.Streams
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace Lua.Tests.Helpers;
static class IOThrowHelpers
{
///
/// 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 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, long 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([NotNull] 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 CanWrite 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();
}
}
///
/// Gets a standard instance for a stream.
///
/// A with the standard text.
public static Exception GetNotSupportedException()
{
return new NotSupportedException("The requested operation is not supported for this stream.");
}
///
/// Throws a when trying to perform a not supported operation.
///
[DoesNotReturn]
public static void ThrowNotSupportedException()
{
throw GetNotSupportedException();
}
///
/// Throws an when trying to write too many bytes to the target stream.
///
[DoesNotReturn]
public static void ThrowArgumentExceptionForEndOfStreamOnWrite()
{
throw new ArgumentException("The current stream can't contain the requested input data.");
}
///
/// Throws an when using an invalid seek mode.
///
/// Nothing, as this method throws unconditionally.
public static long ThrowArgumentExceptionForSeekOrigin()
{
throw new ArgumentException("The input seek mode is not valid.", "origin");
}
///
/// Throws an when setting the property.
///
static void ThrowArgumentOutOfRangeExceptionForPosition()
{
throw new ArgumentOutOfRangeException(nameof(Stream.Position), "The value for the property was not in the valid range.");
}
///
/// Throws an when an input buffer is .
///
[DoesNotReturn]
static void ThrowArgumentNullExceptionForBuffer()
{
throw new ArgumentNullException("buffer", "The buffer is null.");
}
///
/// Throws an when the input count is negative.
///
[DoesNotReturn]
static void ThrowArgumentOutOfRangeExceptionForOffset()
{
throw new ArgumentOutOfRangeException("offset", "Offset can't be negative.");
}
///
/// Throws an when the input count is negative.
///
[DoesNotReturn]
static void ThrowArgumentOutOfRangeExceptionForCount()
{
throw new ArgumentOutOfRangeException("count", "Count can't be negative.");
}
///
/// Throws an when the sum of offset and count exceeds the length of the target buffer.
///
[DoesNotReturn]
static void ThrowArgumentExceptionForLength()
{
throw new ArgumentException("The sum of offset and count can't be larger than the buffer length.", "buffer");
}
///
/// Throws an when using a disposed instance.
///
[DoesNotReturn]
static void ThrowObjectDisposedException()
{
throw new ObjectDisposedException("source", "The current stream has already been disposed");
}
}