99using System . Diagnostics ;
1010using System . IO ;
1111using System . Text ; // For StringBuilder.
12-
12+ using System . Buffers . Binary ;
13+ using System . Runtime . InteropServices ;
1314// see #Introduction and #SerializerIntroduction
1415namespace FastSerialization
1516{
@@ -590,7 +591,6 @@ public Serializer(IStreamWriter writer, IFastSerializable entryObject)
590591 Log ( "<Serializer>" ) ;
591592 // Write the header.
592593 Write ( "!FastSerialization.1" ) ;
593-
594594 // Write the main object. This is recursive and does most of the work.
595595 Write ( entryObject ) ;
596596
@@ -605,7 +605,7 @@ public Serializer(IStreamWriter writer, IFastSerializable entryObject)
605605 Log ( "<ForwardRefTable StreamLabel=\" 0x" + forwardRefsLabel . ToString ( "x" ) + "\" >" ) ;
606606 if ( forwardReferenceDefinitions != null )
607607 {
608- Write ( forwardReferenceDefinitions . Count ) ;
608+ Write ( BinaryPrimitives . ReadInt32LittleEndian ( BitConverter . GetBytes ( forwardReferenceDefinitions . Count ) ) ) ;
609609 for ( int i = 0 ; i < forwardReferenceDefinitions . Count ; i ++ )
610610 {
611611 Debug . Assert ( forwardReferenceDefinitions [ i ] != StreamLabel . Invalid ) ;
@@ -688,7 +688,15 @@ public void Write(long value)
688688 public void Write ( Guid value )
689689 {
690690 Log ( "<Write Type=\" Guid\" Value=\" " + value + "\" StreamLabel=\" 0x" + writer . GetLabel ( ) . ToString ( "x" ) + "\" />" ) ;
691- byte [ ] bytes = value . ToByteArray ( ) ;
691+ Span < byte > bytes = value . ToByteArray ( ) ;
692+
693+ if ( ! BitConverter . IsLittleEndian )
694+ {
695+ BinaryPrimitives . WriteInt32LittleEndian ( bytes . Slice ( 0 ) , ( MemoryMarshal . Read < int > ( bytes . Slice ( 0 , 4 ) ) ) ) ;
696+ BinaryPrimitives . WriteInt16LittleEndian ( bytes . Slice ( 4 ) , ( MemoryMarshal . Read < short > ( bytes . Slice ( 4 , 6 ) ) ) ) ;
697+ BinaryPrimitives . WriteInt16LittleEndian ( bytes . Slice ( 6 ) , ( MemoryMarshal . Read < short > ( bytes . Slice ( 6 , 8 ) ) ) ) ;
698+ }
699+
692700 for ( int i = 0 ; i < bytes . Length ; i ++ )
693701 {
694702 writer . Write ( bytes [ i ] ) ;
@@ -815,15 +823,15 @@ public void DefineForwardReference(ForwardReference forwardReference)
815823 /// <summary>
816824 /// Write a byte preceded by a tag that indicates its a short. These should be read with the corresponding TryReadTagged operation
817825 /// </summary>
818- public void WriteTagged ( short value ) { WriteTag ( Tags . Int16 ) ; Write ( value ) ; }
826+ public void WriteTagged ( short value ) { WriteTag ( Tags . Int16 ) ; Write ( BinaryPrimitives . ReadInt16LittleEndian ( BitConverter . GetBytes ( value ) ) ) ; }
819827 /// <summary>
820828 /// Write a byte preceded by a tag that indicates its a int. These should be read with the corresponding TryReadTagged operation
821829 /// </summary>
822- public void WriteTagged ( int value ) { WriteTag ( Tags . Int32 ) ; Write ( value ) ; }
830+ public void WriteTagged ( int value ) { WriteTag ( Tags . Int32 ) ; Write ( BinaryPrimitives . ReadInt32LittleEndian ( BitConverter . GetBytes ( value ) ) ) ; }
823831 /// <summary>
824832 /// Write a byte preceded by a tag that indicates its a long. These should be read with the corresponding TryReadTagged operation
825833 /// </summary>
826- public void WriteTagged ( long value ) { WriteTag ( Tags . Int64 ) ; Write ( value ) ; }
834+ public void WriteTagged ( long value ) { WriteTag ( Tags . Int64 ) ; Write ( BinaryPrimitives . ReadInt64LittleEndian ( BitConverter . GetBytes ( value ) ) ) ; }
827835 /// <summary>
828836 /// Write a byte preceded by a tag that indicates its a string. These should be read with the corresponding TryReadTagged operation
829837 /// </summary>
@@ -1591,7 +1599,6 @@ public StreamLabel ResolveForwardReference(ForwardReference reference, bool pres
15911599 reader . GotoSuffixLabel ( ) ;
15921600 Log ( "<Trailer StreamLabel=\" 0x" + reader . Current . ToString ( "x" ) + "\" />" ) ;
15931601 StreamLabel forwardRefsLabel = reader . ReadLabel ( ) ;
1594-
15951602 Goto ( forwardRefsLabel ) ;
15961603 int fowardRefCount = reader . ReadInt32 ( ) ;
15971604 Log ( "<ForwardReferenceDefinitons StreamLabel=\" 0x" + forwardRefsLabel . ToString ( "x" ) +
@@ -2226,7 +2233,7 @@ public void Write(Serializer serializer, Action toStream)
22262233 serializer . Log ( "<DeferedRegion>\r \n " ) ;
22272234 // We actually don't use the this pointer! We did this for symmetry with Read
22282235 ForwardReference endRegion = serializer . GetForwardReference ( ) ;
2229- serializer . Write ( endRegion ) ; // Allow the reader to skip this.
2236+ serializer . Write ( BinaryPrimitives . ReadInt32LittleEndian ( BitConverter . GetBytes ( ( int ) endRegion ) ) ) ; // Allow the reader to skip this.
22302237 toStream ( ) ; // Write the deferred data.
22312238 serializer . DefineForwardReference ( endRegion ) ;
22322239 serializer . Log ( "</DeferedRegion>\r \n " ) ;
@@ -2503,8 +2510,8 @@ internal SerializationType(string fullName, Deserializer deserializer)
25032510 }
25042511 void IFastSerializable . ToStream ( Serializer serializer )
25052512 {
2506- serializer . Write ( version ) ;
2507- serializer . Write ( minimumReaderVersion ) ;
2513+ serializer . Write ( BinaryPrimitives . ReadInt32LittleEndian ( BitConverter . GetBytes ( version ) ) ) ;
2514+ serializer . Write ( BinaryPrimitives . ReadInt32LittleEndian ( BitConverter . GetBytes ( minimumReaderVersion ) ) ) ;
25082515 serializer . Write ( fullName ) ;
25092516 }
25102517 void IFastSerializable . FromStream ( Deserializer deserializer )
0 commit comments