@@ -148,7 +148,8 @@ public static int ReadLongstr(ReadOnlySpan<byte> span, out byte[] value)
148
148
149
149
if ( byteCount > int . MaxValue )
150
150
{
151
- throw new SyntaxErrorException ( $ "Long string too long; byte length={ byteCount } , max={ int . MaxValue } ") ;
151
+ value = null ;
152
+ return ThrowSyntaxErrorException ( byteCount ) ;
152
153
}
153
154
154
155
value = span . Slice ( 4 , ( int ) byteCount ) . ToArray ( ) ;
@@ -183,7 +184,8 @@ public static int ReadShortstr(ReadOnlySpan<byte> span, out string value)
183
184
#endif
184
185
}
185
186
186
- throw new ArgumentOutOfRangeException ( "span" , $ "Span has not enough space ({ span . Length } instead of { byteCount + 1 } )") ;
187
+ value = null ;
188
+ return ThrowArgumentOutOfRangeException ( span . Length , byteCount + 1 ) ;
187
189
}
188
190
189
191
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
@@ -737,9 +739,9 @@ public static int WriteShortstr(Span<byte> span, string val)
737
739
span [ 0 ] = ( byte ) bytesWritten ;
738
740
return bytesWritten + 1 ;
739
741
}
740
- catch ( ArgumentException e )
742
+ catch ( ArgumentException )
741
743
{
742
- throw new ArgumentOutOfRangeException ( $ "Value exceeds the maximum allowed length of { maxLength } bytes." , e ) ;
744
+ return ThrowArgumentOutOfRangeException ( nameof ( val ) , val , maxLength ) ;
743
745
}
744
746
#else
745
747
unsafe
@@ -753,9 +755,9 @@ public static int WriteShortstr(Span<byte> span, string val)
753
755
span [ 0 ] = ( byte ) bytesWritten ;
754
756
return bytesWritten + 1 ;
755
757
}
756
- catch ( ArgumentException e )
758
+ catch ( ArgumentException )
757
759
{
758
- throw new ArgumentOutOfRangeException ( $ "Value exceeds the maximum allowed length of { maxLength } bytes." , e ) ;
760
+ return ThrowArgumentOutOfRangeException ( nameof ( val ) , val , maxLength ) ;
759
761
}
760
762
}
761
763
}
@@ -894,5 +896,20 @@ public static int WriteTimestamp(Span<byte> span, AmqpTimestamp val)
894
896
// See also MethodArgumentReader.ReadTimestamp and AmqpTimestamp itself
895
897
return WriteLonglong ( span , ( ulong ) val . UnixTime ) ;
896
898
}
899
+
900
+ public static int ThrowArgumentOutOfRangeException ( int orig , int expected )
901
+ {
902
+ throw new ArgumentOutOfRangeException ( "span" , $ "Span has not enough space ({ orig } instead of { expected } )") ;
903
+ }
904
+
905
+ public static int ThrowArgumentOutOfRangeException ( string paramName , string val , int maxLength )
906
+ {
907
+ throw new ArgumentOutOfRangeException ( paramName , val , $ "Value exceeds the maximum allowed length of { maxLength } bytes.") ;
908
+ }
909
+
910
+ public static int ThrowSyntaxErrorException ( uint byteCount )
911
+ {
912
+ throw new SyntaxErrorException ( $ "Long string too long; byte length={ byteCount } , max={ int . MaxValue } ") ;
913
+ }
897
914
}
898
915
}
0 commit comments