@@ -29,7 +29,7 @@ private static void ValidateBufferBounds(byte[] buffer, int offset, int required
2929 if ( offset + requiredSize > buffer . Length )
3030 {
3131 throw new ArgumentOutOfRangeException ( nameof ( offset ) ,
32- $ "Buffer too small: need { offset + requiredSize } bytes, have { buffer . Length } ." ) ;
32+ LocalizationService . GetString ( LocalizationKeys . Exceptions . BufferWriteOutOfRange , offset + requiredSize , buffer . Length ) ) ;
3333 }
3434 }
3535
@@ -48,7 +48,8 @@ private static void ValidateReadBounds(byte[] buffer, int offset, int requiredSi
4848
4949 if ( offset + requiredSize > buffer . Length )
5050 {
51- throw new ArgumentOutOfRangeException ( nameof ( offset ) , "Buffer read out of index." ) ;
51+ throw new ArgumentOutOfRangeException ( nameof ( offset ) ,
52+ LocalizationService . GetString ( LocalizationKeys . Exceptions . BufferWriteOutOfRange , offset + requiredSize , buffer . Length ) ) ;
5253 }
5354 }
5455
@@ -74,7 +75,8 @@ public static string ToArrayString(this byte[] bytes)
7475 {
7576 ArgumentNullException . ThrowIfNull ( bytes , nameof ( bytes ) ) ;
7677
77- var stringBuilder = new StringBuilder ( ) ;
78+ // 预分配容量: 每个字节最多 3 个字符(如 "255")+ 1 个空格
79+ var stringBuilder = new StringBuilder ( bytes . Length * 4 ) ;
7880 foreach ( var b in bytes )
7981 {
8082 stringBuilder . Append ( b ) . Append ( ' ' ) ;
@@ -93,7 +95,8 @@ public static string ToHex(this byte[] bytes)
9395 {
9496 ArgumentNullException . ThrowIfNull ( bytes , nameof ( bytes ) ) ;
9597
96- var stringBuilder = new StringBuilder ( ) ;
98+ // 预分配容量: 每个字节 2 个十六进制字符
99+ var stringBuilder = new StringBuilder ( bytes . Length * 2 ) ;
97100 foreach ( var b in bytes )
98101 {
99102 stringBuilder . Append ( b . ToString ( "X2" ) ) ;
@@ -113,7 +116,8 @@ public static string ToHex(this byte[] bytes, string format)
113116 {
114117 ArgumentNullException . ThrowIfNull ( bytes , nameof ( bytes ) ) ;
115118
116- var stringBuilder = new StringBuilder ( ) ;
119+ // 预分配容量: 假设每个字节最多 4 个字符
120+ var stringBuilder = new StringBuilder ( bytes . Length * 4 ) ;
117121 foreach ( var b in bytes )
118122 {
119123 stringBuilder . Append ( b . ToString ( format ) ) ;
@@ -142,7 +146,8 @@ public static string ToHex(this byte[] bytes, int offset, int count)
142146 throw new ArgumentException ( LocalizationService . GetString ( LocalizationKeys . Exceptions . OffsetCountExceedBufferLength ) , nameof ( count ) ) ;
143147 }
144148
145- var stringBuilder = new StringBuilder ( ) ;
149+ // 预分配容量: 每个字节 2 个十六进制字符
150+ var stringBuilder = new StringBuilder ( count * 2 ) ;
146151 for ( var i = offset ; i < offset + count ; ++ i )
147152 {
148153 stringBuilder . Append ( bytes [ i ] . ToString ( "X2" ) ) ;
@@ -1004,11 +1009,15 @@ public static string ToBase64String(this byte[] bytes, int offset, int length)
10041009 /// 将 Base64 字符串转换为字节数组。
10051010 /// </summary>
10061011 /// <param name="base64String">Base64 编码的字符串。</param>
1007- /// <returns>解码后的字节数组。</returns>
1012+ /// <returns>解码后的字节数组。如果输入为空字符串,返回空数组。 </returns>
10081013 /// <exception cref="ArgumentNullException">当 <paramref name="base64String"/> 为 null 时抛出。</exception>
10091014 public static byte [ ] ToByteArrayFromBase64 ( this string base64String )
10101015 {
10111016 ArgumentNullException . ThrowIfNull ( base64String , nameof ( base64String ) ) ;
1017+ if ( string . IsNullOrEmpty ( base64String ) )
1018+ {
1019+ return Array . Empty < byte > ( ) ;
1020+ }
10121021 return Convert . FromBase64String ( base64String ) ;
10131022 }
10141023
0 commit comments