Skip to content

Commit b0e355a

Browse files
committed
fix(Hash): 修复 CrcHelper 异常处理问题
- CrcHelper.Crc64.cs: 在调用点直接抛出 ArgumentException,使用 nameof(destination) - CrcHelper.cs: 统一使用 ArgumentNullException.ThrowIfNull 替代旧构造方式
1 parent 848ade3 commit b0e355a

2 files changed

Lines changed: 6 additions & 21 deletions

File tree

GameFrameX.Foundation.Hash/CrcHelper.Crc64.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ public static int Hash(ReadOnlySpan<byte> source, Span<byte> destination)
220220
{
221221
if (destination.Length < Size)
222222
{
223-
ThrowDestinationTooShort();
223+
throw new ArgumentException("Destination buffer is too short.", nameof(destination));
224224
}
225225

226226
var hash = HashToUInt64(source);
@@ -469,7 +469,7 @@ public int GetCurrentHash(Span<byte> destination)
469469
{
470470
if (destination.Length < HashLengthInBytes)
471471
{
472-
ThrowDestinationTooShort();
472+
throw new ArgumentException("Destination buffer is too short.", nameof(destination));
473473
}
474474

475475
GetCurrentHashCore(destination.Slice(0, HashLengthInBytes));
@@ -531,7 +531,7 @@ public int GetHashAndReset(Span<byte> destination)
531531
{
532532
if (destination.Length < HashLengthInBytes)
533533
{
534-
ThrowDestinationTooShort();
534+
throw new ArgumentException("Destination buffer is too short.", nameof(destination));
535535
}
536536

537537
GetHashAndResetCore(destination.Slice(0, HashLengthInBytes));
@@ -582,11 +582,5 @@ public override int GetHashCode()
582582
{
583583
throw new NotSupportedException();
584584
}
585-
586-
587-
private protected static void ThrowDestinationTooShort()
588-
{
589-
throw new ArgumentException("destination");
590-
}
591585
}
592586
}

GameFrameX.Foundation.Hash/CrcHelper.cs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,7 @@ public static ulong GetCrc64(Stream stream)
6767
/// <exception cref="ArgumentNullException">当bytes参数为null时抛出</exception>
6868
public static int GetCrc32(byte[] bytes)
6969
{
70-
if (bytes == null)
71-
{
72-
throw new ArgumentNullException(nameof(bytes), @"Bytes is invalid.");
73-
}
70+
ArgumentNullException.ThrowIfNull(bytes, nameof(bytes));
7471

7572
return GetCrc32(bytes, 0, bytes.Length);
7673
}
@@ -86,10 +83,7 @@ public static int GetCrc32(byte[] bytes)
8683
/// <exception cref="ArgumentException">当offset或length参数无效时抛出</exception>
8784
public static int GetCrc32(byte[] bytes, int offset, int length)
8885
{
89-
if (bytes == null)
90-
{
91-
throw new ArgumentNullException(nameof(bytes), @"Bytes is invalid.");
92-
}
86+
ArgumentNullException.ThrowIfNull(bytes, nameof(bytes));
9387

9488
if (offset < 0 || length < 0 || offset + length > bytes.Length)
9589
{
@@ -110,10 +104,7 @@ public static int GetCrc32(byte[] bytes, int offset, int length)
110104
/// <exception cref="ArgumentNullException">当stream参数为null时抛出</exception>
111105
public static int GetCrc32(Stream stream)
112106
{
113-
if (stream == null)
114-
{
115-
throw new ArgumentNullException(nameof(stream), @"Stream is invalid.");
116-
}
107+
ArgumentNullException.ThrowIfNull(stream, nameof(stream));
117108

118109
while (true)
119110
{

0 commit comments

Comments
 (0)