using System.Buffers.Binary; using System.Text; namespace AVEVA.Historian.Client.Protocol; internal static class HistorianBinaryPrimitives { public static long ToFileTimeUtc(DateTime value) { return value.Kind == DateTimeKind.Unspecified ? DateTime.SpecifyKind(value, DateTimeKind.Utc).ToFileTimeUtc() : value.ToUniversalTime().ToFileTimeUtc(); } public static void WriteUInt16LittleEndian(Stream stream, ushort value) { Span buffer = stackalloc byte[sizeof(ushort)]; BinaryPrimitives.WriteUInt16LittleEndian(buffer, value); stream.Write(buffer); } public static void WriteUInt32LittleEndian(Stream stream, uint value) { Span buffer = stackalloc byte[sizeof(uint)]; BinaryPrimitives.WriteUInt32LittleEndian(buffer, value); stream.Write(buffer); } public static void WriteUInt64LittleEndian(Stream stream, ulong value) { Span buffer = stackalloc byte[sizeof(ulong)]; BinaryPrimitives.WriteUInt64LittleEndian(buffer, value); stream.Write(buffer); } public static void WriteFileTimeUtc(Stream stream, DateTime value) { WriteUInt64LittleEndian(stream, unchecked((ulong)ToFileTimeUtc(value))); } public static void WriteUtf16NullTerminated(Stream stream, string value) { ArgumentNullException.ThrowIfNull(value); byte[] bytes = Encoding.Unicode.GetBytes(value); stream.Write(bytes); stream.WriteByte(0); stream.WriteByte(0); } }