1
0
mirror of https://github.com/chylex/.NET-Community-Toolkit.git synced 2024-11-23 22:42:47 +01:00
.NET-Community-Toolkit/CommunityToolkit.Diagnostics/Guard.IO.cs
2021-12-29 18:51:11 +01:00

82 lines
3.2 KiB
C#

// 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;
using System.IO;
using System.Runtime.CompilerServices;
namespace CommunityToolkit.Diagnostics;
/// <inheritdoc/>
partial class Guard
{
/// <summary>
/// Asserts that the input <see cref="Stream"/> instance must support reading.
/// </summary>
/// <param name="stream">The input <see cref="Stream"/> instance to test.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="stream"/> doesn't support reading.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void CanRead(Stream stream, [CallerArgumentExpression("stream")] string name = "")
{
if (stream.CanRead)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForCanRead(stream, name);
}
/// <summary>
/// Asserts that the input <see cref="Stream"/> instance must support writing.
/// </summary>
/// <param name="stream">The input <see cref="Stream"/> instance to test.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="stream"/> doesn't support writing.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void CanWrite(Stream stream, [CallerArgumentExpression("stream")] string name = "")
{
if (stream.CanWrite)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForCanWrite(stream, name);
}
/// <summary>
/// Asserts that the input <see cref="Stream"/> instance must support seeking.
/// </summary>
/// <param name="stream">The input <see cref="Stream"/> instance to test.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="stream"/> doesn't support seeking.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void CanSeek(Stream stream, [CallerArgumentExpression("stream")] string name = "")
{
if (stream.CanSeek)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForCanSeek(stream, name);
}
/// <summary>
/// Asserts that the input <see cref="Stream"/> instance must be at the starting position.
/// </summary>
/// <param name="stream">The input <see cref="Stream"/> instance to test.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="stream"/> is not at the starting position.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsAtStartPosition(Stream stream, [CallerArgumentExpression("stream")] string name = "")
{
if (stream.Position == 0)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForIsAtStartPosition(stream, name);
}
}