@@ -8,19 +8,43 @@ internal static class IBufferWriterExtensions
8
8
{
9
9
public static void WriteUInt16BigEndian ( this IBufferWriter < byte > writer , ushort value )
10
10
{
11
- Span < byte > buffer = stackalloc byte [ 2 ] ;
11
+ var buffer = writer . GetSpan ( 2 ) ;
12
12
13
13
BinaryPrimitives . WriteUInt16BigEndian ( buffer , value ) ;
14
14
15
- writer . Write ( buffer ) ;
15
+ writer . Advance ( 2 ) ;
16
16
}
17
17
18
18
public static void WriteUInt32BigEndian ( this IBufferWriter < byte > writer , uint value )
19
19
{
20
- Span < byte > buffer = stackalloc byte [ 4 ] ;
20
+ var buffer = writer . GetSpan ( 4 ) ;
21
21
22
22
BinaryPrimitives . WriteUInt32BigEndian ( buffer , value ) ;
23
23
24
- writer . Write ( buffer ) ;
24
+ writer . Advance ( 4 ) ;
25
+ }
26
+
27
+ public static void WriteGuidBigEndian ( this IBufferWriter < byte > writer , Guid value )
28
+ {
29
+ var buffer = writer . GetSpan ( 16 ) ;
30
+
31
+ _ = value . TryWriteBytes ( buffer ) ;
32
+
33
+ if ( BitConverter . IsLittleEndian )
34
+ {
35
+ SwapBytes ( buffer , 0 , 3 ) ;
36
+ SwapBytes ( buffer , 1 , 2 ) ;
37
+ SwapBytes ( buffer , 4 , 5 ) ;
38
+ SwapBytes ( buffer , 6 , 7 ) ;
39
+ }
40
+
41
+ writer . Advance ( 16 ) ;
42
+ }
43
+
44
+ private static void SwapBytes ( Span < byte > bytes , int index1 , int index2 )
45
+ {
46
+ var temp = bytes [ index1 ] ;
47
+ bytes [ index1 ] = bytes [ index2 ] ;
48
+ bytes [ index2 ] = temp ;
25
49
}
26
50
}
0 commit comments