IOThrowHelpers.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // CommunityToolkit.HighPerformance.Streams
  4. using System.Diagnostics.CodeAnalysis;
  5. using System.Runtime.CompilerServices;
  6. namespace Lua.Tests.Helpers;
  7. internal static class IOThrowHelpers
  8. {
  9. /// <summary>
  10. /// Validates the <see cref="Stream.Position"/> argument (it needs to be in the [0, length]) range.
  11. /// </summary>
  12. /// <param name="position">The new <see cref="Stream.Position"/> value being set.</param>
  13. /// <param name="length">The maximum length of the target <see cref="Stream"/>.</param>
  14. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  15. public static void ValidatePosition(long position, int length)
  16. {
  17. if ((ulong)position > (ulong)length)
  18. {
  19. ThrowArgumentOutOfRangeExceptionForPosition();
  20. }
  21. }
  22. /// <summary>
  23. /// Validates the <see cref="Stream.Position"/> argument (it needs to be in the [0, length]) range.
  24. /// </summary>
  25. /// <param name="position">The new <see cref="Stream.Position"/> value being set.</param>
  26. /// <param name="length">The maximum length of the target <see cref="Stream"/>.</param>
  27. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  28. public static void ValidatePosition(long position, long length)
  29. {
  30. if ((ulong)position > (ulong)length)
  31. {
  32. ThrowArgumentOutOfRangeExceptionForPosition();
  33. }
  34. }
  35. /// <summary>
  36. /// Validates the <see cref="Stream.Read(byte[],int,int)"/> or <see cref="Stream.Write(byte[],int,int)"/> arguments.
  37. /// </summary>
  38. /// <param name="buffer">The target array.</param>
  39. /// <param name="offset">The offset within <paramref name="buffer"/>.</param>
  40. /// <param name="count">The number of elements to process within <paramref name="buffer"/>.</param>
  41. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  42. public static void ValidateBuffer([NotNull] byte[]? buffer, int offset, int count)
  43. {
  44. if (buffer is null)
  45. {
  46. ThrowArgumentNullExceptionForBuffer();
  47. }
  48. if (offset < 0)
  49. {
  50. ThrowArgumentOutOfRangeExceptionForOffset();
  51. }
  52. if (count < 0)
  53. {
  54. ThrowArgumentOutOfRangeExceptionForCount();
  55. }
  56. if (offset + count > buffer!.Length)
  57. {
  58. ThrowArgumentExceptionForLength();
  59. }
  60. }
  61. /// <summary>
  62. /// Validates the CanWrite property.
  63. /// </summary>
  64. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  65. public static void ValidateCanWrite(bool canWrite)
  66. {
  67. if (!canWrite)
  68. {
  69. ThrowNotSupportedException();
  70. }
  71. }
  72. /// <summary>
  73. /// Validates that a given <see cref="Stream"/> instance hasn't been disposed.
  74. /// </summary>
  75. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  76. public static void ValidateDisposed(bool disposed)
  77. {
  78. if (disposed)
  79. {
  80. ThrowObjectDisposedException();
  81. }
  82. }
  83. /// <summary>
  84. /// Gets a standard <see cref="NotSupportedException"/> instance for a stream.
  85. /// </summary>
  86. /// <returns>A <see cref="NotSupportedException"/> with the standard text.</returns>
  87. public static Exception GetNotSupportedException()
  88. {
  89. return new NotSupportedException("The requested operation is not supported for this stream.");
  90. }
  91. /// <summary>
  92. /// Throws a <see cref="NotSupportedException"/> when trying to perform a not supported operation.
  93. /// </summary>
  94. [DoesNotReturn]
  95. public static void ThrowNotSupportedException()
  96. {
  97. throw GetNotSupportedException();
  98. }
  99. /// <summary>
  100. /// Throws an <see cref="ArgumentException"/> when trying to write too many bytes to the target stream.
  101. /// </summary>
  102. [DoesNotReturn]
  103. public static void ThrowArgumentExceptionForEndOfStreamOnWrite()
  104. {
  105. throw new ArgumentException("The current stream can't contain the requested input data.");
  106. }
  107. /// <summary>
  108. /// Throws an <see cref="ArgumentException"/> when using an invalid seek mode.
  109. /// </summary>
  110. /// <returns>Nothing, as this method throws unconditionally.</returns>
  111. public static long ThrowArgumentExceptionForSeekOrigin()
  112. {
  113. throw new ArgumentException("The input seek mode is not valid.", "origin");
  114. }
  115. /// <summary>
  116. /// Throws an <see cref="ArgumentOutOfRangeException"/> when setting the <see cref="Stream.Position"/> property.
  117. /// </summary>
  118. private static void ThrowArgumentOutOfRangeExceptionForPosition()
  119. {
  120. throw new ArgumentOutOfRangeException(nameof(Stream.Position), "The value for the property was not in the valid range.");
  121. }
  122. /// <summary>
  123. /// Throws an <see cref="ArgumentNullException"/> when an input buffer is <see langword="null"/>.
  124. /// </summary>
  125. [DoesNotReturn]
  126. private static void ThrowArgumentNullExceptionForBuffer()
  127. {
  128. throw new ArgumentNullException("buffer", "The buffer is null.");
  129. }
  130. /// <summary>
  131. /// Throws an <see cref="ArgumentOutOfRangeException"/> when the input count is negative.
  132. /// </summary>
  133. [DoesNotReturn]
  134. private static void ThrowArgumentOutOfRangeExceptionForOffset()
  135. {
  136. throw new ArgumentOutOfRangeException("offset", "Offset can't be negative.");
  137. }
  138. /// <summary>
  139. /// Throws an <see cref="ArgumentOutOfRangeException"/> when the input count is negative.
  140. /// </summary>
  141. [DoesNotReturn]
  142. private static void ThrowArgumentOutOfRangeExceptionForCount()
  143. {
  144. throw new ArgumentOutOfRangeException("count", "Count can't be negative.");
  145. }
  146. /// <summary>
  147. /// Throws an <see cref="ArgumentException"/> when the sum of offset and count exceeds the length of the target buffer.
  148. /// </summary>
  149. [DoesNotReturn]
  150. private static void ThrowArgumentExceptionForLength()
  151. {
  152. throw new ArgumentException("The sum of offset and count can't be larger than the buffer length.", "buffer");
  153. }
  154. /// <summary>
  155. /// Throws an <see cref="ObjectDisposedException"/> when using a disposed <see cref="Stream"/> instance.
  156. /// </summary>
  157. [DoesNotReturn]
  158. private static void ThrowObjectDisposedException()
  159. {
  160. throw new ObjectDisposedException("source", "The current stream has already been disposed");
  161. }
  162. }