Summary

Class:System.Buffers.Binary.BinaryPrimitives
Assembly:System.Memory
File(s):C:\GitHub\corefx\src\System.Memory\src\System\Buffers\Binary\Reader.cs
C:\GitHub\corefx\src\System.Memory\src\System\Buffers\Binary\ReaderBigEndian.cs
C:\GitHub\corefx\src\System.Memory\src\System\Buffers\Binary\ReaderLittleEndian.cs
C:\GitHub\corefx\src\System.Memory\src\System\Buffers\Binary\Writer.cs
C:\GitHub\corefx\src\System.Memory\src\System\Buffers\Binary\WriterBigEndian.cs
C:\GitHub\corefx\src\System.Memory\src\System\Buffers\Binary\WriterLittleEndian.cs
Covered lines:351
Uncovered lines:78
Coverable lines:429
Total lines:994
Line coverage:81.8%
Branch coverage:57.1%

Metrics

MethodCyclomatic complexity NPath complexity Sequence coverage Branch coverage
ReverseEndianness(...)10100100
ReverseEndianness(...)10100100
ReverseEndianness(...)10100100
ReverseEndianness(...)10100100
ReverseEndianness(...)10100100
ReverseEndianness(...)10100100
ReverseEndianness(...)10100100
ReverseEndianness(...)10100100
ReadMachineEndian(...)3481.82100
TryReadMachineEndian(...)3491.67100
ReadInt16BigEndian(...)2210066.67
ReadInt32BigEndian(...)2210066.67
ReadInt64BigEndian(...)2210066.67
ReadUInt16BigEndian(...)2210066.67
ReadUInt32BigEndian(...)2210066.67
ReadUInt64BigEndian(...)2210066.67
TryReadInt16BigEndian(...)2210066.67
TryReadInt32BigEndian(...)2210066.67
TryReadInt64BigEndian(...)2210066.67
TryReadUInt16BigEndian(...)2210066.67
TryReadUInt32BigEndian(...)2210066.67
TryReadUInt64BigEndian(...)2210066.67
ReadInt16LittleEndian(...)2262.566.67
ReadInt32LittleEndian(...)2262.566.67
ReadInt64LittleEndian(...)2262.566.67
ReadUInt16LittleEndian(...)2262.566.67
ReadUInt32LittleEndian(...)2262.566.67
ReadUInt64LittleEndian(...)2262.566.67
TryReadInt16LittleEndian(...)2262.566.67
TryReadInt32LittleEndian(...)2262.566.67
TryReadInt64LittleEndian(...)2262.566.67
TryReadUInt16LittleEndian(...)2262.566.67
TryReadUInt32LittleEndian(...)2262.566.67
TryReadUInt64LittleEndian(...)2262.566.67
WriteMachineEndian(...)3481.82100
TryWriteMachineEndian(...)3490.91100
WriteInt16BigEndian(...)2210066.67
WriteInt32BigEndian(...)2210066.67
WriteInt64BigEndian(...)2210066.67
WriteUInt16BigEndian(...)2210066.67
WriteUInt32BigEndian(...)2210066.67
WriteUInt64BigEndian(...)2210066.67
TryWriteInt16BigEndian(...)2210066.67
TryWriteInt32BigEndian(...)2210066.67
TryWriteInt64BigEndian(...)2210066.67
TryWriteUInt16BigEndian(...)2210066.67
TryWriteUInt32BigEndian(...)2210066.67
TryWriteUInt64BigEndian(...)2210066.67
WriteInt16LittleEndian(...)2257.1466.67
WriteInt32LittleEndian(...)2257.1466.67
WriteInt64LittleEndian(...)2257.1466.67
WriteUInt16LittleEndian(...)2257.1466.67
WriteUInt32LittleEndian(...)2257.1466.67
WriteUInt64LittleEndian(...)2257.1466.67
TryWriteInt16LittleEndian(...)2257.1466.67
TryWriteInt32LittleEndian(...)2257.1466.67
TryWriteInt64LittleEndian(...)2257.1466.67
TryWriteUInt16LittleEndian(...)2257.1466.67
TryWriteUInt32LittleEndian(...)2257.1466.67
TryWriteUInt64LittleEndian(...)2257.1466.67

File(s)

C:\GitHub\corefx\src\System.Memory\src\System\Buffers\Binary\Reader.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3// See the LICENSE file in the project root for more information.
 4
 5using System.Runtime.CompilerServices;
 6using System.Runtime.InteropServices;
 7
 8#if !netstandard
 9using Internal.Runtime.CompilerServices;
 10#endif
 11
 12namespace System.Buffers.Binary
 13{
 14    /// <summary>
 15    /// Reads bytes as primitives with specific endianness
 16    /// </summary>
 17    /// <remarks>
 18    /// For native formats, MemoryExtensions.Read{T}; should be used.
 19    /// Use these helpers when you need to read specific endinanness.
 20    /// </remarks>
 21    public static partial class BinaryPrimitives
 22    {
 23        /// <summary>
 24        /// This is a no-op and added only for consistency.
 25        /// This allows the caller to read a struct of numeric primitives and reverse each field
 26        /// rather than having to skip sbyte fields.
 27        /// </summary>
 28        [CLSCompliant(false)]
 29        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 30        public static sbyte ReverseEndianness(sbyte value)
 131        {
 132            return value;
 133        }
 34
 35        /// <summary>
 36        /// Reverses a primitive value - performs an endianness swap
 37        /// </summary>
 38        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 39        public static short ReverseEndianness(short value)
 140        {
 141            return (short)((value & 0x00FF) << 8 | (value & 0xFF00) >> 8);
 142        }
 43
 44        /// <summary>
 45        /// Reverses a primitive value - performs an endianness swap
 46        /// </summary>
 47        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 148        public static int ReverseEndianness(int value) => (int)ReverseEndianness((uint)value);
 49
 50        /// <summary>
 51        /// Reverses a primitive value - performs an endianness swap
 52        /// </summary>
 53        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 154        public static long ReverseEndianness(long value) => (long)ReverseEndianness((ulong)value);
 55
 56        /// <summary>
 57        /// This is a no-op and added only for consistency.
 58        /// This allows the caller to read a struct of numeric primitives and reverse each field
 59        /// rather than having to skip byte fields.
 60        /// </summary>
 61        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 62        public static byte ReverseEndianness(byte value)
 163        {
 164            return value;
 165        }
 66
 67        /// <summary>
 68        /// Reverses a primitive value - performs an endianness swap
 69        /// </summary>
 70        [CLSCompliant(false)]
 71        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 72        public static ushort ReverseEndianness(ushort value)
 173        {
 74            // Don't need to AND with 0xFF00 or 0x00FF since the final
 75            // cast back to ushort will clear out all bits above [ 15 .. 00 ].
 76            // This is normally implemented via "movzx eax, ax" on the return.
 77            // Alternatively, the compiler could elide the movzx instruction
 78            // entirely if it knows the caller is only going to access "ax"
 79            // instead of "eax" / "rax" when the function returns.
 80
 181            return (ushort)((value >> 8) + (value << 8));
 182        }
 83
 84        /// <summary>
 85        /// Reverses a primitive value - performs an endianness swap
 86        /// </summary>
 87        [CLSCompliant(false)]
 88        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 89        public static uint ReverseEndianness(uint value)
 190        {
 91            // This takes advantage of the fact that the JIT can detect
 92            // ROL32 / ROR32 patterns and output the correct intrinsic.
 93            //
 94            // Input: value = [ ww xx yy zz ]
 95            //
 96            // First line generates : [ ww xx yy zz ]
 97            //                      & [ 00 FF 00 FF ]
 98            //                      = [ 00 xx 00 zz ]
 99            //             ROR32(8) = [ zz 00 xx 00 ]
 100            //
 101            // Second line generates: [ ww xx yy zz ]
 102            //                      & [ FF 00 FF 00 ]
 103            //                      = [ ww 00 yy 00 ]
 104            //             ROL32(8) = [ 00 yy 00 ww ]
 105            //
 106            //                (sum) = [ zz yy xx ww ]
 107            //
 108            // Testing shows that throughput increases if the AND
 109            // is performed before the ROL / ROR.
 110
 1111            uint mask_xx_zz = (value & 0x00FF00FFU);
 1112            uint mask_ww_yy = (value & 0xFF00FF00U);
 1113            return ((mask_xx_zz >> 8) | (mask_xx_zz << 24))
 1114                + ((mask_ww_yy << 8) | (mask_ww_yy >> 24));
 1115        }
 116
 117        /// <summary>
 118        /// Reverses a primitive value - performs an endianness swap
 119        /// </summary>
 120        [CLSCompliant(false)]
 121        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 122        public static ulong ReverseEndianness(ulong value)
 1123        {
 124            // Operations on 32-bit values have higher throughput than
 125            // operations on 64-bit values, so decompose.
 126
 1127            return ((ulong)ReverseEndianness((uint)value) << 32)
 1128                + ReverseEndianness((uint)(value >> 32));
 1129        }
 130
 131        /// <summary>
 132        /// Reads a structure of type T out of a read-only span of bytes.
 133        /// </summary>
 134        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 135        public static T ReadMachineEndian<T>(ReadOnlySpan<byte> buffer)
 136            where T : struct
 1137        {
 138#if netstandard
 139            if (SpanHelpers.IsReferenceOrContainsReferences<T>())
 140            {
 141                ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T));
 142            }
 143#else
 1144            if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
 1145            {
 1146                ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T));
 0147            }
 148#endif
 1149            if (Unsafe.SizeOf<T>() > buffer.Length)
 1150            {
 1151                ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.length);
 0152            }
 1153            return Unsafe.ReadUnaligned<T>(ref MemoryMarshal.GetReference(buffer));
 1154        }
 155
 156        /// <summary>
 157        /// Reads a structure of type T out of a span of bytes.
 158        /// <returns>If the span is too small to contain the type T, return false.</returns>
 159        /// </summary>
 160        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 161        public static bool TryReadMachineEndian<T>(ReadOnlySpan<byte> buffer, out T value)
 162            where T : struct
 1163        {
 164#if netstandard
 165            if (SpanHelpers.IsReferenceOrContainsReferences<T>())
 166            {
 167                ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T));
 168            }
 169#else
 1170            if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
 1171            {
 1172                ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T));
 0173            }
 174#endif
 1175            if (Unsafe.SizeOf<T>() > (uint)buffer.Length)
 1176            {
 1177                value = default;
 1178                return false;
 179            }
 1180            value = Unsafe.ReadUnaligned<T>(ref MemoryMarshal.GetReference(buffer));
 1181            return true;
 1182        }
 183    }
 184}

C:\GitHub\corefx\src\System.Memory\src\System\Buffers\Binary\ReaderBigEndian.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3// See the LICENSE file in the project root for more information.
 4
 5using System.Runtime.CompilerServices;
 6
 7namespace System.Buffers.Binary
 8{
 9    public static partial class BinaryPrimitives
 10    {
 11        /// <summary>
 12        /// Reads an Int16 out of a read-only span of bytes as big endian.
 13        /// </summary>
 14        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 15        public static short ReadInt16BigEndian(ReadOnlySpan<byte> buffer)
 116        {
 117            short result = ReadMachineEndian<short>(buffer);
 118            if (BitConverter.IsLittleEndian)
 119            {
 120                result = ReverseEndianness(result);
 121            }
 122            return result;
 123        }
 24
 25        /// <summary>
 26        /// Reads an Int32 out of a read-only span of bytes as big endian.
 27        /// </summary>
 28        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 29        public static int ReadInt32BigEndian(ReadOnlySpan<byte> buffer)
 130        {
 131            int result = ReadMachineEndian<int>(buffer);
 132            if (BitConverter.IsLittleEndian)
 133            {
 134                result = ReverseEndianness(result);
 135            }
 136            return result;
 137        }
 38
 39        /// <summary>
 40        /// Reads an Int64 out of a read-only span of bytes as big endian.
 41        /// </summary>
 42        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 43        public static long ReadInt64BigEndian(ReadOnlySpan<byte> buffer)
 144        {
 145            long result = ReadMachineEndian<long>(buffer);
 146            if (BitConverter.IsLittleEndian)
 147            {
 148                result = ReverseEndianness(result);
 149            }
 150            return result;
 151        }
 52
 53        /// <summary>
 54        /// Reads a UInt16 out of a read-only span of bytes as big endian.
 55        /// </summary>
 56        [CLSCompliant(false)]
 57        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 58        public static ushort ReadUInt16BigEndian(ReadOnlySpan<byte> buffer)
 159        {
 160            ushort result = ReadMachineEndian<ushort>(buffer);
 161            if (BitConverter.IsLittleEndian)
 162            {
 163                result = ReverseEndianness(result);
 164            }
 165            return result;
 166        }
 67
 68        /// <summary>
 69        /// Reads a UInt32 out of a read-only span of bytes as big endian.
 70        /// </summary>
 71        [CLSCompliant(false)]
 72        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 73        public static uint ReadUInt32BigEndian(ReadOnlySpan<byte> buffer)
 174        {
 175            uint result = ReadMachineEndian<uint>(buffer);
 176            if (BitConverter.IsLittleEndian)
 177            {
 178                result = ReverseEndianness(result);
 179            }
 180            return result;
 181        }
 82
 83        /// <summary>
 84        /// Reads a UInt64 out of a read-only span of bytes as big endian.
 85        /// </summary>
 86        [CLSCompliant(false)]
 87        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 88        public static ulong ReadUInt64BigEndian(ReadOnlySpan<byte> buffer)
 189        {
 190            ulong result = ReadMachineEndian<ulong>(buffer);
 191            if (BitConverter.IsLittleEndian)
 192            {
 193                result = ReverseEndianness(result);
 194            }
 195            return result;
 196        }
 97
 98        /// <summary>
 99        /// Reads an Int16 out of a read-only span of bytes as big endian.
 100        /// <returns>If the span is too small to contain an Int16, return false.</returns>
 101        /// </summary>
 102        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 103        public static bool TryReadInt16BigEndian(ReadOnlySpan<byte> buffer, out short value)
 1104        {
 1105            bool success = TryReadMachineEndian(buffer, out value);
 1106            if (BitConverter.IsLittleEndian)
 1107            {
 1108                value = ReverseEndianness(value);
 1109            }
 1110            return success;
 1111        }
 112
 113        /// <summary>
 114        /// Reads an Int32 out of a read-only span of bytes as big endian.
 115        /// <returns>If the span is too small to contain an Int32, return false.</returns>
 116        /// </summary>
 117        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 118        public static bool TryReadInt32BigEndian(ReadOnlySpan<byte> buffer, out int value)
 1119        {
 1120            bool success = TryReadMachineEndian(buffer, out value);
 1121            if (BitConverter.IsLittleEndian)
 1122            {
 1123                value = ReverseEndianness(value);
 1124            }
 1125            return success;
 1126        }
 127
 128        /// <summary>
 129        /// Reads an Int64 out of a read-only span of bytes as big endian.
 130        /// <returns>If the span is too small to contain an Int64, return false.</returns>
 131        /// </summary>
 132        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 133        public static bool TryReadInt64BigEndian(ReadOnlySpan<byte> buffer, out long value)
 1134        {
 1135            bool success = TryReadMachineEndian(buffer, out value);
 1136            if (BitConverter.IsLittleEndian)
 1137            {
 1138                value = ReverseEndianness(value);
 1139            }
 1140            return success;
 1141        }
 142
 143        /// <summary>
 144        /// Reads a UInt16 out of a read-only span of bytes as big endian.
 145        /// <returns>If the span is too small to contain a UInt16, return false.</returns>
 146        /// </summary>
 147        [CLSCompliant(false)]
 148        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 149        public static bool TryReadUInt16BigEndian(ReadOnlySpan<byte> buffer, out ushort value)
 1150        {
 1151            bool success = TryReadMachineEndian(buffer, out value);
 1152            if (BitConverter.IsLittleEndian)
 1153            {
 1154                value = ReverseEndianness(value);
 1155            }
 1156            return success;
 1157        }
 158
 159        /// <summary>
 160        /// Reads a UInt32 out of a read-only span of bytes as big endian.
 161        /// <returns>If the span is too small to contain a UInt32, return false.</returns>
 162        /// </summary>
 163        [CLSCompliant(false)]
 164        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 165        public static bool TryReadUInt32BigEndian(ReadOnlySpan<byte> buffer, out uint value)
 1166        {
 1167            bool success = TryReadMachineEndian(buffer, out value);
 1168            if (BitConverter.IsLittleEndian)
 1169            {
 1170                value = ReverseEndianness(value);
 1171            }
 1172            return success;
 1173        }
 174
 175        /// <summary>
 176        /// Reads a UInt64 out of a read-only span of bytes as big endian.
 177        /// <returns>If the span is too small to contain a UInt64, return false.</returns>
 178        /// </summary>
 179        [CLSCompliant(false)]
 180        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 181        public static bool TryReadUInt64BigEndian(ReadOnlySpan<byte> buffer, out ulong value)
 1182        {
 1183            bool success = TryReadMachineEndian(buffer, out value);
 1184            if (BitConverter.IsLittleEndian)
 1185            {
 1186                value = ReverseEndianness(value);
 1187            }
 1188            return success;
 1189        }
 190    }
 191}
 192

C:\GitHub\corefx\src\System.Memory\src\System\Buffers\Binary\ReaderLittleEndian.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3// See the LICENSE file in the project root for more information.
 4
 5using System.Runtime.CompilerServices;
 6
 7namespace System.Buffers.Binary
 8{
 9    public static partial class BinaryPrimitives
 10    {
 11        /// <summary>
 12        /// Reads an Int16 out of a read-only span of bytes as little endian.
 13        /// </summary>
 14        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 15        public static short ReadInt16LittleEndian(ReadOnlySpan<byte> buffer)
 116        {
 117            short result = ReadMachineEndian<short>(buffer);
 118            if (!BitConverter.IsLittleEndian)
 019            {
 020                result = ReverseEndianness(result);
 021            }
 122            return result;
 123        }
 24
 25        /// <summary>
 26        /// Reads an Int32 out of a read-only span of bytes as little endian.
 27        /// </summary>
 28        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 29        public static int ReadInt32LittleEndian(ReadOnlySpan<byte> buffer)
 130        {
 131            int result = ReadMachineEndian<int>(buffer);
 132            if (!BitConverter.IsLittleEndian)
 033            {
 034                result = ReverseEndianness(result);
 035            }
 136            return result;
 137        }
 38
 39        /// <summary>
 40        /// Reads an Int64 out of a read-only span of bytes as little endian.
 41        /// </summary>
 42        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 43        public static long ReadInt64LittleEndian(ReadOnlySpan<byte> buffer)
 144        {
 145            long result = ReadMachineEndian<long>(buffer);
 146            if (!BitConverter.IsLittleEndian)
 047            {
 048                result = ReverseEndianness(result);
 049            }
 150            return result;
 151        }
 52
 53        /// <summary>
 54        /// Reads a UInt16 out of a read-only span of bytes as little endian.
 55        /// </summary>
 56        [CLSCompliant(false)]
 57        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 58        public static ushort ReadUInt16LittleEndian(ReadOnlySpan<byte> buffer)
 159        {
 160            ushort result = ReadMachineEndian<ushort>(buffer);
 161            if (!BitConverter.IsLittleEndian)
 062            {
 063                result = ReverseEndianness(result);
 064            }
 165            return result;
 166        }
 67
 68        /// <summary>
 69        /// Reads a UInt32 out of a read-only span of bytes as little endian.
 70        /// </summary>
 71        [CLSCompliant(false)]
 72        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 73        public static uint ReadUInt32LittleEndian(ReadOnlySpan<byte> buffer)
 174        {
 175            uint result = ReadMachineEndian<uint>(buffer);
 176            if (!BitConverter.IsLittleEndian)
 077            {
 078                result = ReverseEndianness(result);
 079            }
 180            return result;
 181        }
 82
 83        /// <summary>
 84        /// Reads a UInt64 out of a read-only span of bytes as little endian.
 85        /// </summary>
 86        [CLSCompliant(false)]
 87        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 88        public static ulong ReadUInt64LittleEndian(ReadOnlySpan<byte> buffer)
 189        {
 190            ulong result = ReadMachineEndian<ulong>(buffer);
 191            if (!BitConverter.IsLittleEndian)
 092            {
 093                result = ReverseEndianness(result);
 094            }
 195            return result;
 196        }
 97
 98        /// <summary>
 99        /// Reads an Int16 out of a read-only span of bytes as little endian.
 100        /// <returns>If the span is too small to contain an Int16, return false.</returns>
 101        /// </summary>
 102        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 103        public static bool TryReadInt16LittleEndian(ReadOnlySpan<byte> buffer, out short value)
 1104        {
 1105            bool success = TryReadMachineEndian(buffer, out value);
 1106            if (!BitConverter.IsLittleEndian)
 0107            {
 0108                value = ReverseEndianness(value);
 0109            }
 1110            return success;
 1111        }
 112
 113        /// <summary>
 114        /// Reads an Int32 out of a read-only span of bytes as little endian.
 115        /// <returns>If the span is too small to contain an Int32, return false.</returns>
 116        /// </summary>
 117        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 118        public static bool TryReadInt32LittleEndian(ReadOnlySpan<byte> buffer, out int value)
 1119        {
 1120            bool success = TryReadMachineEndian(buffer, out value);
 1121            if (!BitConverter.IsLittleEndian)
 0122            {
 0123                value = ReverseEndianness(value);
 0124            }
 1125            return success;
 1126        }
 127
 128        /// <summary>
 129        /// Reads an Int64 out of a read-only span of bytes as little endian.
 130        /// <returns>If the span is too small to contain an Int64, return false.</returns>
 131        /// </summary>
 132        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 133        public static bool TryReadInt64LittleEndian(ReadOnlySpan<byte> buffer, out long value)
 1134        {
 1135            bool success = TryReadMachineEndian(buffer, out value);
 1136            if (!BitConverter.IsLittleEndian)
 0137            {
 0138                value = ReverseEndianness(value);
 0139            }
 1140            return success;
 1141        }
 142
 143        /// <summary>
 144        /// Reads a UInt16 out of a read-only span of bytes as little endian.
 145        /// <returns>If the span is too small to contain a UInt16, return false.</returns>
 146        /// </summary>
 147        [CLSCompliant(false)]
 148        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 149        public static bool TryReadUInt16LittleEndian(ReadOnlySpan<byte> buffer, out ushort value)
 1150        {
 1151            bool success = TryReadMachineEndian(buffer, out value);
 1152            if (!BitConverter.IsLittleEndian)
 0153            {
 0154                value = ReverseEndianness(value);
 0155            }
 1156            return success;
 1157        }
 158
 159        /// <summary>
 160        /// Reads a UInt32 out of a read-only span of bytes as little endian.
 161        /// <returns>If the span is too small to contain a UInt32, return false.</returns>
 162        /// </summary>
 163        [CLSCompliant(false)]
 164        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 165        public static bool TryReadUInt32LittleEndian(ReadOnlySpan<byte> buffer, out uint value)
 1166        {
 1167            bool success = TryReadMachineEndian(buffer, out value);
 1168            if (!BitConverter.IsLittleEndian)
 0169            {
 0170                value = ReverseEndianness(value);
 0171            }
 1172            return success;
 1173        }
 174
 175        /// <summary>
 176        /// Reads a UInt64 out of a read-only span of bytes as little endian.
 177        /// <returns>If the span is too small to contain a UInt64, return false.</returns>
 178        /// </summary>
 179        [CLSCompliant(false)]
 180        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 181        public static bool TryReadUInt64LittleEndian(ReadOnlySpan<byte> buffer, out ulong value)
 1182        {
 1183            bool success = TryReadMachineEndian(buffer, out value);
 1184            if (!BitConverter.IsLittleEndian)
 0185            {
 0186                value = ReverseEndianness(value);
 0187            }
 1188            return success;
 1189        }
 190    }
 191}

C:\GitHub\corefx\src\System.Memory\src\System\Buffers\Binary\Writer.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3// See the LICENSE file in the project root for more information.
 4
 5using System.Runtime.CompilerServices;
 6using System.Runtime.InteropServices;
 7
 8#if !netstandard
 9using Internal.Runtime.CompilerServices;
 10#endif
 11
 12namespace System.Buffers.Binary
 13{
 14    public static partial class BinaryPrimitives
 15    {
 16        /// <summary>
 17        /// Writes a structure of type T into a span of bytes.
 18        /// </summary>
 19        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 20        public static void WriteMachineEndian<T>(Span<byte> buffer, ref T value)
 21            where T : struct
 122        {
 23#if netstandard
 24            if (SpanHelpers.IsReferenceOrContainsReferences<T>())
 25            {
 26                ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T));
 27            }
 28#else
 129            if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
 130            {
 131                ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T));
 032            }
 33#endif
 134            if ((uint)Unsafe.SizeOf<T>() > (uint)buffer.Length)
 135            {
 136                ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.length);
 037            }
 138            Unsafe.WriteUnaligned<T>(ref MemoryMarshal.GetReference(buffer), value);
 139        }
 40
 41        /// <summary>
 42        /// Writes a structure of type T into a span of bytes.
 43        /// <returns>If the span is too small to contain the type T, return false.</returns>
 44        /// </summary>
 45        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 46        public static bool TryWriteMachineEndian<T>(Span<byte> buffer, ref T value)
 47            where T : struct
 148        {
 49#if netstandard
 50            if (SpanHelpers.IsReferenceOrContainsReferences<T>())
 51            {
 52                ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T));
 53            }
 54#else
 155            if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
 156            {
 157                ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T));
 058            }
 59#endif
 160            if (Unsafe.SizeOf<T>() > (uint)buffer.Length)
 161            {
 162                return false;
 63            }
 164            Unsafe.WriteUnaligned<T>(ref MemoryMarshal.GetReference(buffer), value);
 165            return true;
 166        }
 67    }
 68}
 69

C:\GitHub\corefx\src\System.Memory\src\System\Buffers\Binary\WriterBigEndian.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3// See the LICENSE file in the project root for more information.
 4
 5using System.Runtime.CompilerServices;
 6
 7namespace System.Buffers.Binary
 8{
 9    public static partial class BinaryPrimitives
 10    {
 11        /// <summary>
 12        /// Writes an Int16 into a span of bytes as big endian.
 13        /// </summary>
 14        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 15        public static void WriteInt16BigEndian(Span<byte> buffer, short value)
 116        {
 117            if (BitConverter.IsLittleEndian)
 118            {
 119                value = ReverseEndianness(value);
 120            }
 121            WriteMachineEndian(buffer, ref value);
 122        }
 23
 24        /// <summary>
 25        /// Writes an Int32 into a span of bytes as big endian.
 26        /// </summary>
 27        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 28        public static void WriteInt32BigEndian(Span<byte> buffer, int value)
 129        {
 130            if (BitConverter.IsLittleEndian)
 131            {
 132                value = ReverseEndianness(value);
 133            }
 134            WriteMachineEndian(buffer, ref value);
 135        }
 36
 37        /// <summary>
 38        /// Writes an Int64 into a span of bytes as big endian.
 39        /// </summary>
 40        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 41        public static void WriteInt64BigEndian(Span<byte> buffer, long value)
 142        {
 143            if (BitConverter.IsLittleEndian)
 144            {
 145                value = ReverseEndianness(value);
 146            }
 147            WriteMachineEndian(buffer, ref value);
 148        }
 49
 50        /// <summary>
 51        /// Write a UInt16 into a span of bytes as big endian.
 52        /// </summary>
 53        [CLSCompliant(false)]
 54        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 55        public static void WriteUInt16BigEndian(Span<byte> buffer, ushort value)
 156        {
 157            if (BitConverter.IsLittleEndian)
 158            {
 159                value = ReverseEndianness(value);
 160            }
 161            WriteMachineEndian(buffer, ref value);
 162        }
 63
 64        /// <summary>
 65        /// Write a UInt32 into a span of bytes as big endian.
 66        /// </summary>
 67        [CLSCompliant(false)]
 68        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 69        public static void WriteUInt32BigEndian(Span<byte> buffer, uint value)
 170        {
 171            if (BitConverter.IsLittleEndian)
 172            {
 173                value = ReverseEndianness(value);
 174            }
 175            WriteMachineEndian(buffer, ref value);
 176        }
 77
 78        /// <summary>
 79        /// Write a UInt64 into a span of bytes as big endian.
 80        /// </summary>
 81        [CLSCompliant(false)]
 82        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 83        public static void WriteUInt64BigEndian(Span<byte> buffer, ulong value)
 184        {
 185            if (BitConverter.IsLittleEndian)
 186            {
 187                value = ReverseEndianness(value);
 188            }
 189            WriteMachineEndian(buffer, ref value);
 190        }
 91
 92        /// <summary>
 93        /// Writes an Int16 into a span of bytes as big endian.
 94        /// <returns>If the span is too small to contain the value, return false.</returns>
 95        /// </summary>
 96        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 97        public static bool TryWriteInt16BigEndian(Span<byte> buffer, short value)
 198        {
 199            if (BitConverter.IsLittleEndian)
 1100            {
 1101                value = ReverseEndianness(value);
 1102            }
 1103            return TryWriteMachineEndian(buffer, ref value);
 1104        }
 105
 106        /// <summary>
 107        /// Writes an Int32 into a span of bytes as big endian.
 108        /// <returns>If the span is too small to contain the value, return false.</returns>
 109        /// </summary>
 110        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 111        public static bool TryWriteInt32BigEndian(Span<byte> buffer, int value)
 1112        {
 1113            if (BitConverter.IsLittleEndian)
 1114            {
 1115                value = ReverseEndianness(value);
 1116            }
 1117            return TryWriteMachineEndian(buffer, ref value);
 1118        }
 119
 120        /// <summary>
 121        /// Writes an Int64 into a span of bytes as big endian.
 122        /// <returns>If the span is too small to contain the value, return false.</returns>
 123        /// </summary>
 124        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 125        public static bool TryWriteInt64BigEndian(Span<byte> buffer, long value)
 1126        {
 1127            if (BitConverter.IsLittleEndian)
 1128            {
 1129                value = ReverseEndianness(value);
 1130            }
 1131            return TryWriteMachineEndian(buffer, ref value);
 1132        }
 133
 134        /// <summary>
 135        /// Write a UInt16 into a span of bytes as big endian.
 136        /// <returns>If the span is too small to contain the value, return false.</returns>
 137        /// </summary>
 138        [CLSCompliant(false)]
 139        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 140        public static bool TryWriteUInt16BigEndian(Span<byte> buffer, ushort value)
 1141        {
 1142            if (BitConverter.IsLittleEndian)
 1143            {
 1144                value = ReverseEndianness(value);
 1145            }
 1146            return TryWriteMachineEndian(buffer, ref value);
 1147        }
 148
 149        /// <summary>
 150        /// Write a UInt32 into a span of bytes as big endian.
 151        /// <returns>If the span is too small to contain the value, return false.</returns>
 152        /// </summary>
 153        [CLSCompliant(false)]
 154        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 155        public static bool TryWriteUInt32BigEndian(Span<byte> buffer, uint value)
 1156        {
 1157            if (BitConverter.IsLittleEndian)
 1158            {
 1159                value = ReverseEndianness(value);
 1160            }
 1161            return TryWriteMachineEndian(buffer, ref value);
 1162        }
 163
 164        /// <summary>
 165        /// Write a UInt64 into a span of bytes as big endian.
 166        /// <returns>If the span is too small to contain the value, return false.</returns>
 167        /// </summary>
 168        [CLSCompliant(false)]
 169        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 170        public static bool TryWriteUInt64BigEndian(Span<byte> buffer, ulong value)
 1171        {
 1172            if (BitConverter.IsLittleEndian)
 1173            {
 1174                value = ReverseEndianness(value);
 1175            }
 1176            return TryWriteMachineEndian(buffer, ref value);
 1177        }
 178    }
 179}

C:\GitHub\corefx\src\System.Memory\src\System\Buffers\Binary\WriterLittleEndian.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3// See the LICENSE file in the project root for more information.
 4
 5using System.Runtime.CompilerServices;
 6
 7namespace System.Buffers.Binary
 8{
 9    public static partial class BinaryPrimitives
 10    {
 11        /// <summary>
 12        /// Writes an Int16 into a span of bytes as little endian.
 13        /// </summary>
 14        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 15        public static void WriteInt16LittleEndian(Span<byte> buffer, short value)
 116        {
 117            if (!BitConverter.IsLittleEndian)
 018            {
 019                value = ReverseEndianness(value);
 020            }
 121            WriteMachineEndian(buffer, ref value);
 122        }
 23
 24        /// <summary>
 25        /// Writes an Int32 into a span of bytes as little endian.
 26        /// </summary>
 27        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 28        public static void WriteInt32LittleEndian(Span<byte> buffer, int value)
 129        {
 130            if (!BitConverter.IsLittleEndian)
 031            {
 032                value = ReverseEndianness(value);
 033            }
 134            WriteMachineEndian(buffer, ref value);
 135        }
 36
 37        /// <summary>
 38        /// Writes an Int64 into a span of bytes as little endian.
 39        /// </summary>
 40        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 41        public static void WriteInt64LittleEndian(Span<byte> buffer, long value)
 142        {
 143            if (!BitConverter.IsLittleEndian)
 044            {
 045                value = ReverseEndianness(value);
 046            }
 147            WriteMachineEndian(buffer, ref value);
 148        }
 49
 50        /// <summary>
 51        /// Write a UInt16 into a span of bytes as little endian.
 52        /// </summary>
 53        [CLSCompliant(false)]
 54        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 55        public static void WriteUInt16LittleEndian(Span<byte> buffer, ushort value)
 156        {
 157            if (!BitConverter.IsLittleEndian)
 058            {
 059                value = ReverseEndianness(value);
 060            }
 161            WriteMachineEndian(buffer, ref value);
 162        }
 63
 64        /// <summary>
 65        /// Write a UInt32 into a span of bytes as little endian.
 66        /// </summary>
 67        [CLSCompliant(false)]
 68        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 69        public static void WriteUInt32LittleEndian(Span<byte> buffer, uint value)
 170        {
 171            if (!BitConverter.IsLittleEndian)
 072            {
 073                value = ReverseEndianness(value);
 074            }
 175            WriteMachineEndian(buffer, ref value);
 176        }
 77
 78        /// <summary>
 79        /// Write a UInt64 into a span of bytes as little endian.
 80        /// </summary>
 81        [CLSCompliant(false)]
 82        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 83        public static void WriteUInt64LittleEndian(Span<byte> buffer, ulong value)
 184        {
 185            if (!BitConverter.IsLittleEndian)
 086            {
 087                value = ReverseEndianness(value);
 088            }
 189            WriteMachineEndian(buffer, ref value);
 190        }
 91
 92        /// <summary>
 93        /// Writes an Int16 into a span of bytes as little endian.
 94        /// <returns>If the span is too small to contain the value, return false.</returns>
 95        /// </summary>
 96        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 97        public static bool TryWriteInt16LittleEndian(Span<byte> buffer, short value)
 198        {
 199            if (!BitConverter.IsLittleEndian)
 0100            {
 0101                value = ReverseEndianness(value);
 0102            }
 1103            return TryWriteMachineEndian(buffer, ref value);
 1104        }
 105
 106        /// <summary>
 107        /// Writes an Int32 into a span of bytes as little endian.
 108        /// <returns>If the span is too small to contain the value, return false.</returns>
 109        /// </summary>
 110        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 111        public static bool TryWriteInt32LittleEndian(Span<byte> buffer, int value)
 1112        {
 1113            if (!BitConverter.IsLittleEndian)
 0114            {
 0115                value = ReverseEndianness(value);
 0116            }
 1117            return TryWriteMachineEndian(buffer, ref value);
 1118        }
 119
 120        /// <summary>
 121        /// Writes an Int64 into a span of bytes as little endian.
 122        /// <returns>If the span is too small to contain the value, return false.</returns>
 123        /// </summary>
 124        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 125        public static bool TryWriteInt64LittleEndian(Span<byte> buffer, long value)
 1126        {
 1127            if (!BitConverter.IsLittleEndian)
 0128            {
 0129                value = ReverseEndianness(value);
 0130            }
 1131            return TryWriteMachineEndian(buffer, ref value);
 1132        }
 133
 134        /// <summary>
 135        /// Write a UInt16 into a span of bytes as little endian.
 136        /// <returns>If the span is too small to contain the value, return false.</returns>
 137        /// </summary>
 138        [CLSCompliant(false)]
 139        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 140        public static bool TryWriteUInt16LittleEndian(Span<byte> buffer, ushort value)
 1141        {
 1142            if (!BitConverter.IsLittleEndian)
 0143            {
 0144                value = ReverseEndianness(value);
 0145            }
 1146            return TryWriteMachineEndian(buffer, ref value);
 1147        }
 148
 149        /// <summary>
 150        /// Write a UInt32 into a span of bytes as little endian.
 151        /// <returns>If the span is too small to contain the value, return false.</returns>
 152        /// </summary>
 153        [CLSCompliant(false)]
 154        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 155        public static bool TryWriteUInt32LittleEndian(Span<byte> buffer, uint value)
 1156        {
 1157            if (!BitConverter.IsLittleEndian)
 0158            {
 0159                value = ReverseEndianness(value);
 0160            }
 1161            return TryWriteMachineEndian(buffer, ref value);
 1162        }
 163
 164        /// <summary>
 165        /// Write a UInt64 into a span of bytes as little endian.
 166        /// <returns>If the span is too small to contain the value, return false.</returns>
 167        /// </summary>
 168        [CLSCompliant(false)]
 169        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 170        public static bool TryWriteUInt64LittleEndian(Span<byte> buffer, ulong value)
 1171        {
 1172            if (!BitConverter.IsLittleEndian)
 0173            {
 0174                value = ReverseEndianness(value);
 0175            }
 1176            return TryWriteMachineEndian(buffer, ref value);
 1177        }
 178    }
 179}

Methods/Properties

ReverseEndianness(System.SByte)
ReverseEndianness(System.Int16)
ReverseEndianness(System.Int32)
ReverseEndianness(System.Int64)
ReverseEndianness(System.Byte)
ReverseEndianness(System.UInt16)
ReverseEndianness(System.UInt32)
ReverseEndianness(System.UInt64)
ReadMachineEndian(System.ReadOnlySpan`1<System.Byte>)
TryReadMachineEndian(System.ReadOnlySpan`1<System.Byte>,T&)
ReadInt16BigEndian(System.ReadOnlySpan`1<System.Byte>)
ReadInt32BigEndian(System.ReadOnlySpan`1<System.Byte>)
ReadInt64BigEndian(System.ReadOnlySpan`1<System.Byte>)
ReadUInt16BigEndian(System.ReadOnlySpan`1<System.Byte>)
ReadUInt32BigEndian(System.ReadOnlySpan`1<System.Byte>)
ReadUInt64BigEndian(System.ReadOnlySpan`1<System.Byte>)
TryReadInt16BigEndian(System.ReadOnlySpan`1<System.Byte>,System.Int16&)
TryReadInt32BigEndian(System.ReadOnlySpan`1<System.Byte>,System.Int32&)
TryReadInt64BigEndian(System.ReadOnlySpan`1<System.Byte>,System.Int64&)
TryReadUInt16BigEndian(System.ReadOnlySpan`1<System.Byte>,System.UInt16&)
TryReadUInt32BigEndian(System.ReadOnlySpan`1<System.Byte>,System.UInt32&)
TryReadUInt64BigEndian(System.ReadOnlySpan`1<System.Byte>,System.UInt64&)
ReadInt16LittleEndian(System.ReadOnlySpan`1<System.Byte>)
ReadInt32LittleEndian(System.ReadOnlySpan`1<System.Byte>)
ReadInt64LittleEndian(System.ReadOnlySpan`1<System.Byte>)
ReadUInt16LittleEndian(System.ReadOnlySpan`1<System.Byte>)
ReadUInt32LittleEndian(System.ReadOnlySpan`1<System.Byte>)
ReadUInt64LittleEndian(System.ReadOnlySpan`1<System.Byte>)
TryReadInt16LittleEndian(System.ReadOnlySpan`1<System.Byte>,System.Int16&)
TryReadInt32LittleEndian(System.ReadOnlySpan`1<System.Byte>,System.Int32&)
TryReadInt64LittleEndian(System.ReadOnlySpan`1<System.Byte>,System.Int64&)
TryReadUInt16LittleEndian(System.ReadOnlySpan`1<System.Byte>,System.UInt16&)
TryReadUInt32LittleEndian(System.ReadOnlySpan`1<System.Byte>,System.UInt32&)
TryReadUInt64LittleEndian(System.ReadOnlySpan`1<System.Byte>,System.UInt64&)
WriteMachineEndian(System.Span`1<System.Byte>,T&)
TryWriteMachineEndian(System.Span`1<System.Byte>,T&)
WriteInt16BigEndian(System.Span`1<System.Byte>,System.Int16)
WriteInt32BigEndian(System.Span`1<System.Byte>,System.Int32)
WriteInt64BigEndian(System.Span`1<System.Byte>,System.Int64)
WriteUInt16BigEndian(System.Span`1<System.Byte>,System.UInt16)
WriteUInt32BigEndian(System.Span`1<System.Byte>,System.UInt32)
WriteUInt64BigEndian(System.Span`1<System.Byte>,System.UInt64)
TryWriteInt16BigEndian(System.Span`1<System.Byte>,System.Int16)
TryWriteInt32BigEndian(System.Span`1<System.Byte>,System.Int32)
TryWriteInt64BigEndian(System.Span`1<System.Byte>,System.Int64)
TryWriteUInt16BigEndian(System.Span`1<System.Byte>,System.UInt16)
TryWriteUInt32BigEndian(System.Span`1<System.Byte>,System.UInt32)
TryWriteUInt64BigEndian(System.Span`1<System.Byte>,System.UInt64)
WriteInt16LittleEndian(System.Span`1<System.Byte>,System.Int16)
WriteInt32LittleEndian(System.Span`1<System.Byte>,System.Int32)
WriteInt64LittleEndian(System.Span`1<System.Byte>,System.Int64)
WriteUInt16LittleEndian(System.Span`1<System.Byte>,System.UInt16)
WriteUInt32LittleEndian(System.Span`1<System.Byte>,System.UInt32)
WriteUInt64LittleEndian(System.Span`1<System.Byte>,System.UInt64)
TryWriteInt16LittleEndian(System.Span`1<System.Byte>,System.Int16)
TryWriteInt32LittleEndian(System.Span`1<System.Byte>,System.Int32)
TryWriteInt64LittleEndian(System.Span`1<System.Byte>,System.Int64)
TryWriteUInt16LittleEndian(System.Span`1<System.Byte>,System.UInt16)
TryWriteUInt32LittleEndian(System.Span`1<System.Byte>,System.UInt32)
TryWriteUInt64LittleEndian(System.Span`1<System.Byte>,System.UInt64)