1
1
using System ;
2
+ using System . Buffers . Binary ;
2
3
using System . Collections . Generic ;
3
4
using System . Runtime . CompilerServices ;
4
5
using MongoDB . Bson . IO ;
@@ -77,6 +78,7 @@ private static T[] ReadBsonArray<T>(
77
78
78
79
var result = new List < T > ( ) ;
79
80
81
+
80
82
var index = 4 ; // 4 first bytes are array object size in bytes
81
83
var maxIndex = array . Length - 1 ;
82
84
@@ -85,7 +87,7 @@ private static T[] ReadBsonArray<T>(
85
87
ValidateBsonType ( bsonDataType ) ;
86
88
87
89
// Skip name
88
- while ( bytes [ index ] != 0 ) { index ++ ; } ;
90
+ while ( bytes [ index ] != 0 ) { index ++ ; }
89
91
index ++ ; // Skip string terminating 0
90
92
91
93
T value = default ;
@@ -95,85 +97,81 @@ private static T[] ReadBsonArray<T>(
95
97
{
96
98
case ConversionType . DoubleToSingle :
97
99
{
98
- var v = ( float ) BitConverter . ToDouble ( bytes , index ) ;
99
-
100
+ var v = ( float ) BitConverter . Int64BitsToDouble ( BinaryPrimitives . ReadInt64LittleEndian ( bytes . AsSpan ( index ) ) ) ;
100
101
value = Unsafe . As < float , T > ( ref v ) ;
101
102
break ;
102
103
}
103
104
case ConversionType . DoubleToDouble :
104
105
{
105
- var v = BitConverter . ToDouble ( bytes , index ) ;
106
+ var v = BitConverter . Int64BitsToDouble ( BinaryPrimitives . ReadInt64LittleEndian ( bytes . AsSpan ( index ) ) ) ;
106
107
value = Unsafe . As < double , T > ( ref v ) ;
107
108
break ;
108
109
}
109
110
case ConversionType . Decimal128ToDecimal128 :
110
111
{
111
- var lowBits = ( ulong ) BitConverter . ToInt64 ( bytes , index ) ;
112
- var highBits = ( ulong ) BitConverter . ToInt64 ( bytes , index + 8 ) ;
112
+ var lowBits = BinaryPrimitives . ReadUInt64LittleEndian ( bytes . AsSpan ( index ) ) ;
113
+ var highBits = BinaryPrimitives . ReadUInt64LittleEndian ( bytes . AsSpan ( index + 8 ) ) ;
113
114
var v = Decimal128 . ToDecimal ( Decimal128 . FromIEEEBits ( highBits , lowBits ) ) ;
114
-
115
115
value = Unsafe . As < decimal , T > ( ref v ) ;
116
116
break ;
117
117
}
118
118
case ConversionType . BoolToBool :
119
119
{
120
120
var v = bytes [ index ] != 0 ;
121
-
122
121
value = Unsafe . As < bool , T > ( ref v ) ;
123
122
break ;
124
123
}
125
124
case ConversionType . Int32ToInt8 :
126
125
{
127
- var v = ( sbyte ) BitConverter . ToInt32 ( bytes , index ) ;
126
+ var v = ( sbyte ) BinaryPrimitives . ReadInt32LittleEndian ( bytes . AsSpan ( index ) ) ;
128
127
value = Unsafe . As < sbyte , T > ( ref v ) ;
129
-
130
128
break ;
131
129
}
132
130
case ConversionType . Int32ToUInt8 :
133
131
{
134
- var v = ( byte ) BitConverter . ToInt32 ( bytes , index ) ;
132
+ var v = ( byte ) BinaryPrimitives . ReadInt32LittleEndian ( bytes . AsSpan ( index ) ) ;
135
133
value = Unsafe . As < byte , T > ( ref v ) ;
136
134
break ;
137
135
}
138
136
case ConversionType . Int32ToInt16 :
139
137
{
140
- var v = ( short ) BitConverter . ToInt32 ( bytes , index ) ;
138
+ var v = ( short ) BinaryPrimitives . ReadInt32LittleEndian ( bytes . AsSpan ( index ) ) ;
141
139
value = Unsafe . As < short , T > ( ref v ) ;
142
140
break ;
143
141
}
144
142
case ConversionType . Int32ToUInt16 :
145
143
{
146
- var v = ( ushort ) BitConverter . ToInt32 ( bytes , index ) ;
144
+ var v = ( ushort ) BinaryPrimitives . ReadInt32LittleEndian ( bytes . AsSpan ( index ) ) ;
147
145
value = Unsafe . As < ushort , T > ( ref v ) ;
148
146
break ;
149
147
}
150
148
case ConversionType . Int32ToChar :
151
149
{
152
- var v = BitConverter . ToChar ( bytes , index ) ;
150
+ var v = ( char ) ( ushort ) BinaryPrimitives . ReadInt32LittleEndian ( bytes . AsSpan ( index ) ) ;
153
151
value = Unsafe . As < char , T > ( ref v ) ;
154
152
break ;
155
153
}
156
154
case ConversionType . Int32ToInt32 :
157
155
{
158
- var v = BitConverter . ToInt32 ( bytes , index ) ;
156
+ var v = BinaryPrimitives . ReadInt32LittleEndian ( bytes . AsSpan ( index ) ) ;
159
157
value = Unsafe . As < int , T > ( ref v ) ;
160
158
break ;
161
159
}
162
160
case ConversionType . Int32ToUInt32 :
163
161
{
164
- var v = BitConverter . ToUInt32 ( bytes , index ) ;
162
+ var v = BinaryPrimitives . ReadUInt32LittleEndian ( bytes . AsSpan ( index ) ) ;
165
163
value = Unsafe . As < uint , T > ( ref v ) ;
166
164
break ;
167
165
}
168
166
case ConversionType . Int64ToInt64 :
169
167
{
170
- var v = BitConverter . ToInt64 ( bytes , index ) ;
168
+ var v = BinaryPrimitives . ReadInt64LittleEndian ( bytes . AsSpan ( index ) ) ;
171
169
value = Unsafe . As < long , T > ( ref v ) ;
172
170
break ;
173
171
}
174
172
case ConversionType . Int64ToUInt64 :
175
173
{
176
- var v = BitConverter . ToUInt64 ( bytes , index ) ;
174
+ var v = BinaryPrimitives . ReadUInt64LittleEndian ( bytes . AsSpan ( index ) ) ;
177
175
value = Unsafe . As < ulong , T > ( ref v ) ;
178
176
break ;
179
177
}
@@ -182,12 +180,10 @@ private static T[] ReadBsonArray<T>(
182
180
}
183
181
184
182
result . Add ( value ) ;
185
-
186
183
index += bsonDataSize ;
187
184
}
188
185
189
186
ValidateBsonType ( BsonType . EndOfDocument ) ;
190
-
191
187
return result . ToArray ( ) ;
192
188
193
189
void ValidateBsonType ( BsonType bsonType )
0 commit comments