77namespace Dns
88{
99 using System ;
10+ using System . Buffers . Binary ;
1011 using System . IO ;
1112
1213 public class DnsMessage
@@ -31,8 +32,8 @@ public ushort Flags
3132 set
3233 {
3334 _flags = value . SwapEndian ( ) ;
34- byte [ ] bytes = BitConverter . GetBytes ( _flags ) ;
35- bytes . CopyTo ( _header , 2 ) ;
35+ // Phase 5: Use BinaryPrimitives to write directly to header (zero allocation)
36+ BinaryPrimitives . WriteUInt16LittleEndian ( _header . AsSpan ( 2 ) , _flags ) ;
3637 }
3738 }
3839
@@ -189,8 +190,8 @@ public ushort AdditionalCount
189190 set
190191 {
191192 _additionalCount = value ;
192- byte [ ] bytes = BitConverter . GetBytes ( _additionalCount . SwapEndian ( ) ) ;
193- bytes . CopyTo ( _header , 10 ) ;
193+ // Phase 5: Use BinaryPrimitives to write directly to header (zero allocation)
194+ BinaryPrimitives . WriteUInt16BigEndian ( _header . AsSpan ( 10 ) , _additionalCount ) ;
194195 }
195196 }
196197
@@ -200,8 +201,8 @@ public ushort AnswerCount
200201 set
201202 {
202203 _answerCount = value ;
203- byte [ ] bytes = BitConverter . GetBytes ( _answerCount . SwapEndian ( ) ) ;
204- bytes . CopyTo ( _header , 6 ) ;
204+ // Phase 5: Use BinaryPrimitives to write directly to header (zero allocation)
205+ BinaryPrimitives . WriteUInt16BigEndian ( _header . AsSpan ( 6 ) , _answerCount ) ;
205206 }
206207 }
207208
@@ -211,8 +212,8 @@ public ushort NameServerCount
211212 set
212213 {
213214 _nameServerCount = value ;
214- byte [ ] bytes = BitConverter . GetBytes ( _nameServerCount . SwapEndian ( ) ) ;
215- bytes . CopyTo ( _header , 8 ) ;
215+ // Phase 5: Use BinaryPrimitives to write directly to header (zero allocation)
216+ BinaryPrimitives . WriteUInt16BigEndian ( _header . AsSpan ( 8 ) , _nameServerCount ) ;
216217 }
217218 }
218219
@@ -222,8 +223,8 @@ public ushort QueryIdentifier
222223 set
223224 {
224225 _queryIdentifier = value ;
225- byte [ ] bytes = BitConverter . GetBytes ( _queryIdentifier . SwapEndian ( ) ) ;
226- bytes . CopyTo ( _header , 0 ) ;
226+ // Phase 5: Use BinaryPrimitives to write directly to header (zero allocation)
227+ BinaryPrimitives . WriteUInt16BigEndian ( _header . AsSpan ( 0 ) , _queryIdentifier ) ;
227228 }
228229 }
229230
@@ -233,8 +234,8 @@ public ushort QuestionCount
233234 set
234235 {
235236 _questionCount = value ;
236- byte [ ] bytes = BitConverter . GetBytes ( _questionCount . SwapEndian ( ) ) ;
237- bytes . CopyTo ( _header , 4 ) ;
237+ // Phase 5: Use BinaryPrimitives to write directly to header (zero allocation)
238+ BinaryPrimitives . WriteUInt16BigEndian ( _header . AsSpan ( 4 ) , _questionCount ) ;
238239 }
239240 }
240241
@@ -274,12 +275,15 @@ private int ParseHeader(byte[] bytes, int offset)
274275 }
275276
276277 Buffer . BlockCopy ( bytes , 0 , _header , 0 , 12 ) ;
277- _queryIdentifier = BitConverter . ToUInt16 ( _header , 0 ) . SwapEndian ( ) ;
278- _flags = BitConverter . ToUInt16 ( _header , 2 ) ;
279- _questionCount = BitConverter . ToUInt16 ( _header , 4 ) . SwapEndian ( ) ;
280- _answerCount = BitConverter . ToUInt16 ( _header , 6 ) . SwapEndian ( ) ;
281- _nameServerCount = BitConverter . ToUInt16 ( _header , 8 ) . SwapEndian ( ) ;
282- _additionalCount = BitConverter . ToUInt16 ( _header , 10 ) . SwapEndian ( ) ;
278+
279+ // Phase 5: Use BinaryPrimitives for reading (cleaner, no SwapEndian needed)
280+ var headerSpan = _header . AsSpan ( ) ;
281+ _queryIdentifier = BinaryPrimitives . ReadUInt16BigEndian ( headerSpan ) ;
282+ _flags = BinaryPrimitives . ReadUInt16LittleEndian ( headerSpan . Slice ( 2 ) ) ; // Flags stored in little-endian
283+ _questionCount = BinaryPrimitives . ReadUInt16BigEndian ( headerSpan . Slice ( 4 ) ) ;
284+ _answerCount = BinaryPrimitives . ReadUInt16BigEndian ( headerSpan . Slice ( 6 ) ) ;
285+ _nameServerCount = BinaryPrimitives . ReadUInt16BigEndian ( headerSpan . Slice ( 8 ) ) ;
286+ _additionalCount = BinaryPrimitives . ReadUInt16BigEndian ( headerSpan . Slice ( 10 ) ) ;
283287
284288 return 12 ;
285289 }
0 commit comments