Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 36 additions & 45 deletions src/libraries/System.Private.CoreLib/src/System/IO/BinaryWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,14 @@ public virtual void Write(char[] chars, int index, int count)
//
public virtual void Write(double value)
{
Span<byte> 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)
Expand All @@ -258,84 +263,70 @@ public virtual void Write(decimal value)
// Writes a two-byte signed integer to this stream. The current position of
// the stream is advanced by two.
//
public virtual void Write(short value)
{
Span<byte> buffer = stackalloc byte[sizeof(short)];
BinaryPrimitives.WriteInt16LittleEndian(buffer, value);
OutStream.Write(buffer);
}
public virtual void Write(short value) => WriteBytes(BitConverter.IsLittleEndian ? (ushort)value : BinaryPrimitives.ReverseEndianness((ushort)value));

// Writes a two-byte unsigned integer to this stream. The current position
// of the stream is advanced by two.
//
[CLSCompliant(false)]
public virtual void Write(ushort value)
{
Span<byte> buffer = stackalloc byte[sizeof(ushort)];
BinaryPrimitives.WriteUInt16LittleEndian(buffer, value);
OutStream.Write(buffer);
}
public virtual void Write(ushort value) => WriteBytes(BitConverter.IsLittleEndian ? value : BinaryPrimitives.ReverseEndianness(value));

// Writes a four-byte signed integer to this stream. The current position
// of the stream is advanced by four.
//
public virtual void Write(int value)
{
Span<byte> buffer = stackalloc byte[sizeof(int)];
BinaryPrimitives.WriteInt32LittleEndian(buffer, value);
OutStream.Write(buffer);
}
public virtual void Write(int value) => WriteBytes(BitConverter.IsLittleEndian ? (uint)value : BinaryPrimitives.ReverseEndianness((uint)value));

// Writes a four-byte unsigned integer to this stream. The current position
// of the stream is advanced by four.
//
[CLSCompliant(false)]
public virtual void Write(uint value)
{
Span<byte> buffer = stackalloc byte[sizeof(uint)];
BinaryPrimitives.WriteUInt32LittleEndian(buffer, value);
OutStream.Write(buffer);
}
public virtual void Write(uint value) => WriteBytes(BitConverter.IsLittleEndian ? value : BinaryPrimitives.ReverseEndianness(value));

// Writes an eight-byte signed integer to this stream. The current position
// of the stream is advanced by eight.
//
public virtual void Write(long value)
{
Span<byte> buffer = stackalloc byte[sizeof(long)];
BinaryPrimitives.WriteInt64LittleEndian(buffer, value);
OutStream.Write(buffer);
}
public virtual void Write(long value) => WriteBytes(BitConverter.IsLittleEndian ? (ulong)value : BinaryPrimitives.ReverseEndianness((ulong)value));

// Writes an eight-byte unsigned integer to this stream. The current
// position of the stream is advanced by eight.
//
[CLSCompliant(false)]
public virtual void Write(ulong value)
{
Span<byte> buffer = stackalloc byte[sizeof(ulong)];
BinaryPrimitives.WriteUInt64LittleEndian(buffer, value);
OutStream.Write(buffer);
}
public virtual void Write(ulong value) => WriteBytes(BitConverter.IsLittleEndian ? value : BinaryPrimitives.ReverseEndianness(value));

// Writes a float to this stream. The current position of the stream is
// advanced by four.
//
public virtual void Write(float value)
{
Span<byte> 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
// advanced by two.
//
public virtual void Write(Half value)
{
Span<byte> 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)));
}
}

private void WriteBytes<T>(T value)
where T : unmanaged
{
OutStream.Write(MemoryMarshal.AsBytes(new ReadOnlySpan<T>(in value)));
}

// Writes a length-prefixed string to this stream in the BinaryWriter's
Expand Down