diff --git a/src/libraries/System.Private.CoreLib/src/System/IO/BinaryWriter.cs b/src/libraries/System.Private.CoreLib/src/System/IO/BinaryWriter.cs index b6fef2e0dadeef..1eb469931f2dfd 100644 --- a/src/libraries/System.Private.CoreLib/src/System/IO/BinaryWriter.cs +++ b/src/libraries/System.Private.CoreLib/src/System/IO/BinaryWriter.cs @@ -3,6 +3,7 @@ using System.Buffers; using System.Buffers.Binary; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; @@ -135,18 +136,27 @@ public virtual long Seek(int offset, SeekOrigin origin) // Writes a boolean to this stream. A single byte is written to the stream // with the value 0 representing false or the value 1 representing true. // - public virtual void Write(bool value) => OutStream.WriteByte((byte)(value ? 1 : 0)); + public virtual void Write(bool value) + { + OutStream.WriteByte((byte)(value ? 1 : 0)); + } // Writes a byte to this stream. The current position of the stream is // advanced by one. // - public virtual void Write(byte value) => OutStream.WriteByte(value); + public virtual void Write(byte value) + { + OutStream.WriteByte(value); + } // Writes a signed byte to this stream. The current position of the stream // is advanced by one. // [CLSCompliant(false)] - public virtual void Write(sbyte value) => OutStream.WriteByte((byte)value); + public virtual void Write(sbyte value) + { + OutStream.WriteByte((byte)value); + } // Writes a byte array to this stream. // @@ -243,9 +253,14 @@ public virtual void Write(char[] chars, int index, int count) // public virtual void Write(double value) { - Span buffer = stackalloc byte[sizeof(double)]; - BinaryPrimitives.WriteDoubleLittleEndian(buffer, value); - OutStream.Write(buffer); + if (BitConverter.IsLittleEndian) + { + WriteBytes(value); + } + else + { + WriteBytes(BinaryPrimitives.ReverseEndianness(BitConverter.DoubleToUInt64Bits(value))); + } } public virtual void Write(decimal value) @@ -260,9 +275,7 @@ public virtual void Write(decimal value) // public virtual void Write(short value) { - Span buffer = stackalloc byte[sizeof(short)]; - BinaryPrimitives.WriteInt16LittleEndian(buffer, value); - OutStream.Write(buffer); + WriteBytes(BitConverter.IsLittleEndian ? (ushort)value : BinaryPrimitives.ReverseEndianness((ushort)value)); } // Writes a two-byte unsigned integer to this stream. The current position @@ -271,9 +284,7 @@ public virtual void Write(short value) [CLSCompliant(false)] public virtual void Write(ushort value) { - Span buffer = stackalloc byte[sizeof(ushort)]; - BinaryPrimitives.WriteUInt16LittleEndian(buffer, value); - OutStream.Write(buffer); + WriteBytes(BitConverter.IsLittleEndian ? value : BinaryPrimitives.ReverseEndianness(value)); } // Writes a four-byte signed integer to this stream. The current position @@ -281,9 +292,7 @@ public virtual void Write(ushort value) // public virtual void Write(int value) { - Span buffer = stackalloc byte[sizeof(int)]; - BinaryPrimitives.WriteInt32LittleEndian(buffer, value); - OutStream.Write(buffer); + WriteBytes(BitConverter.IsLittleEndian ? (uint)value : BinaryPrimitives.ReverseEndianness((uint)value)); } // Writes a four-byte unsigned integer to this stream. The current position @@ -292,9 +301,7 @@ public virtual void Write(int value) [CLSCompliant(false)] public virtual void Write(uint value) { - Span buffer = stackalloc byte[sizeof(uint)]; - BinaryPrimitives.WriteUInt32LittleEndian(buffer, value); - OutStream.Write(buffer); + WriteBytes(BitConverter.IsLittleEndian ? value : BinaryPrimitives.ReverseEndianness(value)); } // Writes an eight-byte signed integer to this stream. The current position @@ -302,9 +309,7 @@ public virtual void Write(uint value) // public virtual void Write(long value) { - Span buffer = stackalloc byte[sizeof(long)]; - BinaryPrimitives.WriteInt64LittleEndian(buffer, value); - OutStream.Write(buffer); + WriteBytes(BitConverter.IsLittleEndian ? (ulong)value : BinaryPrimitives.ReverseEndianness((ulong)value)); } // Writes an eight-byte unsigned integer to this stream. The current @@ -313,9 +318,7 @@ public virtual void Write(long value) [CLSCompliant(false)] public virtual void Write(ulong value) { - Span buffer = stackalloc byte[sizeof(ulong)]; - BinaryPrimitives.WriteUInt64LittleEndian(buffer, value); - OutStream.Write(buffer); + WriteBytes(BitConverter.IsLittleEndian ? value : BinaryPrimitives.ReverseEndianness(value)); } // Writes a float to this stream. The current position of the stream is @@ -323,9 +326,14 @@ public virtual void Write(ulong value) // public virtual void Write(float value) { - Span buffer = stackalloc byte[sizeof(float)]; - BinaryPrimitives.WriteSingleLittleEndian(buffer, value); - OutStream.Write(buffer); + if (BitConverter.IsLittleEndian) + { + WriteBytes(value); + } + else + { + WriteBytes(BinaryPrimitives.ReverseEndianness(BitConverter.SingleToUInt32Bits(value))); + } } // Writes a half to this stream. The current position of the stream is @@ -333,9 +341,21 @@ public virtual void Write(float value) // public virtual void Write(Half value) { - Span buffer = stackalloc byte[sizeof(ushort) /* = sizeof(Half) */]; - BinaryPrimitives.WriteHalfLittleEndian(buffer, value); - OutStream.Write(buffer); + if (BitConverter.IsLittleEndian) + { + WriteBytes(value); + } + else + { + WriteBytes(BinaryPrimitives.ReverseEndianness(BitConverter.HalfToUInt16Bits(value))); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private void WriteBytes(T value) + where T : unmanaged + { + OutStream.Write(MemoryMarshal.AsBytes(new ReadOnlySpan(in value))); } // Writes a length-prefixed string to this stream in the BinaryWriter's