25
25
using MongoDB . TestHelpers . XunitExtensions ;
26
26
using MongoDB . Driver . Core . Misc ;
27
27
using Xunit ;
28
+ using System . Buffers . Binary ;
28
29
29
30
namespace MongoDB . Driver . Core . WireProtocol . Messages . Encoders . BinaryEncoders
30
31
{
@@ -141,7 +142,7 @@ public void ReadMessage_should_throw_when_flags_is_invalid(
141
142
[ Values ( - 1 , 1 , 4 ) ] int flags )
142
143
{
143
144
var bytes = CreateMessageBytes ( ) ;
144
- BitConverter . GetBytes ( flags ) . CopyTo ( bytes , 16 ) ;
145
+ BinaryPrimitives . WriteInt32LittleEndian ( bytes . AsSpan ( 16 , 4 ) , flags ) ;
145
146
var subject = CreateSubject ( bytes ) ;
146
147
var expectedMessage = flags == 1 ? "Command message CheckSumPresent flag not supported." : "Command message has invalid flags" ;
147
148
@@ -271,7 +272,7 @@ public void WriteMessage_should_write_messageLength(int[] sectionTypes)
271
272
var result = stream . ToArray ( ) ;
272
273
273
274
result . Length . Should ( ) . Be ( expectedMessageLength ) ;
274
- var writtenMessageLength = BitConverter . ToInt32 ( result , 0 ) ;
275
+ var writtenMessageLength = BinaryPrimitives . ReadInt32LittleEndian ( result . AsSpan ( 0 , 4 ) ) ;
275
276
writtenMessageLength . Should ( ) . Be ( expectedMessageLength ) ;
276
277
}
277
278
@@ -287,7 +288,7 @@ public void WriteMessage_should_write_requestId(
287
288
subject . WriteMessage ( message ) ;
288
289
var result = stream . ToArray ( ) ;
289
290
290
- var resultRequestId = BitConverter . ToInt32 ( result , 4 ) ;
291
+ var resultRequestId = BinaryPrimitives . ReadInt32LittleEndian ( result . AsSpan ( 4 , 4 ) ) ;
291
292
resultRequestId . Should ( ) . Be ( requestId ) ;
292
293
}
293
294
@@ -303,7 +304,7 @@ public void WriteMessage_should_write_responseTo(
303
304
subject . WriteMessage ( message ) ;
304
305
var result = stream . ToArray ( ) ;
305
306
306
- var resultResponseTo = BitConverter . ToInt32 ( result , 8 ) ;
307
+ var resultResponseTo = BinaryPrimitives . ReadInt32LittleEndian ( result . AsSpan ( 8 , 4 ) ) ;
307
308
resultResponseTo . Should ( ) . Be ( responseTo ) ;
308
309
}
309
310
@@ -317,7 +318,7 @@ public void WriteMessage_should_write_expected_opcode()
317
318
subject . WriteMessage ( message ) ;
318
319
var result = stream . ToArray ( ) ;
319
320
320
- var opcode = BitConverter . ToInt32 ( result , 12 ) ;
321
+ var opcode = BinaryPrimitives . ReadInt32LittleEndian ( result . AsSpan ( 12 , 4 ) ) ;
321
322
opcode . Should ( ) . Be ( ( int ) Opcode . OpMsg ) ;
322
323
}
323
324
@@ -334,7 +335,7 @@ public void WriteMessage_should_write_flags(
334
335
subject . WriteMessage ( message ) ;
335
336
336
337
var result = stream . ToArray ( ) ;
337
- var flags = ( OpMsgFlags ) BitConverter . ToInt32 ( result , 16 ) ;
338
+ var flags = ( OpMsgFlags ) BinaryPrimitives . ReadInt32LittleEndian ( result . AsSpan ( 16 , 4 ) ) ;
338
339
flags . HasFlag ( OpMsgFlags . MoreToCome ) . Should ( ) . Be ( moreToCome ) ;
339
340
flags . HasFlag ( OpMsgFlags . ExhaustAllowed ) . Should ( ) . Be ( exhaustAllowed ) ;
340
341
}
0 commit comments