using System; using System.IO; namespace SabreTools.Numerics.Extensions { /// /// Extensions for BinaryReader /// public static class BinaryReaderExtensions { #region Exact Read /// /// Reads in both-endian format public static BothUInt8 ReadByteBothEndian(this BinaryReader reader) { byte le = reader.ReadByte(); byte be = reader.ReadByte(); return new BothUInt8(le, be); } /// /// Reads in both-endian format public static BothInt8 ReadSByteBothEndian(this BinaryReader reader) { sbyte le = reader.ReadSByte(); sbyte be = reader.ReadSByte(); return new BothInt8(le, be); } /// /// Reads in big-endian format public static short ReadInt16BigEndian(this BinaryReader reader) { byte[] buffer = reader.ReadBytes(2); return buffer.ToInt16BigEndian(); } /// /// Reads in little-endian format public static short ReadInt16LittleEndian(this BinaryReader reader) { byte[] buffer = reader.ReadBytes(2); return buffer.ToInt16LittleEndian(); } /// /// Reads in both-endian format public static BothInt16 ReadInt16BothEndian(this BinaryReader reader) { short le = reader.ReadInt16LittleEndian(); short be = reader.ReadInt16BigEndian(); return new BothInt16(le, be); } /// /// Reads in big-endian format public static ushort ReadUInt16BigEndian(this BinaryReader reader) { byte[] buffer = reader.ReadBytes(2); return buffer.ToUInt16BigEndian(); } /// /// Reads in little-endian format public static ushort ReadUInt16LittleEndian(this BinaryReader reader) { byte[] buffer = reader.ReadBytes(2); return buffer.ToUInt16LittleEndian(); } /// /// Reads in both-endian format public static BothUInt16 ReadUInt16BothEndian(this BinaryReader reader) { ushort le = reader.ReadUInt16LittleEndian(); ushort be = reader.ReadUInt16BigEndian(); return new BothUInt16(le, be); } /// /// Read a WORD (2-byte) from the base stream /// public static ushort ReadWORD(this BinaryReader reader) => reader.ReadUInt16(); /// /// Read a WORD (2-byte) from the base stream /// /// Reads in big-endian format public static ushort ReadWORDBigEndian(this BinaryReader reader) => reader.ReadUInt16BigEndian(); /// /// Read a WORD (2-byte) from the base stream /// /// Reads in little-endian format public static ushort ReadWORDLittleEndian(this BinaryReader reader) => reader.ReadUInt16LittleEndian(); /// /// Read a WORD (2-byte) from the base stream /// /// Reads in both-endian format public static BothUInt16 ReadWORDBothEndian(this BinaryReader reader) => reader.ReadUInt16BothEndian(); #if NET5_0_OR_GREATER /// /// Read a Half from the underlying stream /// /// Reads in machine native format public static Half ReadHalf(this BinaryReader reader) { if (BitConverter.IsLittleEndian) return reader.ReadHalfLittleEndian(); else return reader.ReadHalfBigEndian(); } /// /// Reads in big-endian format public static Half ReadHalfBigEndian(this BinaryReader reader) { byte[] buffer = reader.ReadBytes(2); return buffer.ToHalfBigEndian(); } /// /// Reads in little-endian format public static Half ReadHalfLittleEndian(this BinaryReader reader) { byte[] buffer = reader.ReadBytes(2); return buffer.ToHalfLittleEndian(); } #endif /// /// Read an Int24 from the base stream /// /// Reads in machine native format public static Int24 ReadInt24(this BinaryReader reader) { if (BitConverter.IsLittleEndian) return reader.ReadInt24LittleEndian(); else return reader.ReadInt24BigEndian(); } /// /// Read an Int24 from the base stream /// /// Reads in big-endian format public static Int24 ReadInt24BigEndian(this BinaryReader reader) { byte[] buffer = reader.ReadBytes(3); return buffer.ToInt24BigEndian(); } /// /// Read an Int24 from the base stream /// /// Reads in little-endian format public static Int24 ReadInt24LittleEndian(this BinaryReader reader) { byte[] buffer = reader.ReadBytes(3); return buffer.ToInt24LittleEndian(); } /// /// Read a UInt24 from the base stream /// /// Reads in machine native format public static UInt24 ReadUInt24(this BinaryReader reader) { if (BitConverter.IsLittleEndian) return reader.ReadUInt24LittleEndian(); else return reader.ReadUInt24BigEndian(); } /// /// Read a UInt24 from the base stream /// /// Reads in big-endian format public static UInt24 ReadUInt24BigEndian(this BinaryReader reader) { byte[] buffer = reader.ReadBytes(3); return buffer.ToUInt24BigEndian(); } /// /// Read a UInt24 from the base stream /// /// Reads in little-endian format public static UInt24 ReadUInt24LittleEndian(this BinaryReader reader) { byte[] buffer = reader.ReadBytes(3); return buffer.ToUInt24LittleEndian(); } /// /// Reads in big-endian format public static int ReadInt32BigEndian(this BinaryReader reader) { byte[] buffer = reader.ReadBytes(4); return buffer.ToInt32BigEndian(); } /// /// Reads in little-endian format public static int ReadInt32LittleEndian(this BinaryReader reader) { byte[] buffer = reader.ReadBytes(4); return buffer.ToInt32LittleEndian(); } /// /// Reads in both-endian format public static BothInt32 ReadInt32BothEndian(this BinaryReader reader) { int le = reader.ReadInt32LittleEndian(); int be = reader.ReadInt32BigEndian(); return new BothInt32(le, be); } /// /// Reads in big-endian format public static uint ReadUInt32BigEndian(this BinaryReader reader) { byte[] buffer = reader.ReadBytes(4); return buffer.ToUInt32BigEndian(); } /// /// Reads in little-endian format public static uint ReadUInt32LittleEndian(this BinaryReader reader) { byte[] buffer = reader.ReadBytes(4); return buffer.ToUInt32LittleEndian(); } /// /// Reads in both-endian format public static BothUInt32 ReadUInt32BothEndian(this BinaryReader reader) { uint le = reader.ReadUInt32LittleEndian(); uint be = reader.ReadUInt32BigEndian(); return new BothUInt32(le, be); } /// /// Read a DWORD (4-byte) from the base stream /// public static uint ReadDWORD(this BinaryReader reader) => reader.ReadUInt32(); /// /// Read a DWORD (4-byte) from the base stream /// /// Reads in big-endian format public static uint ReadDWORDBigEndian(this BinaryReader reader) => reader.ReadUInt32BigEndian(); /// /// Read a DWORD (4-byte) from the base stream /// /// Reads in little-endian format public static uint ReadDWORDLittleEndian(this BinaryReader reader) => reader.ReadUInt32LittleEndian(); /// /// Read a DWORD (4-byte) from the base stream /// /// Reads in both-endian format public static BothUInt32 ReadDWORDBothEndian(this BinaryReader reader) => reader.ReadUInt32BothEndian(); /// /// Reads in big-endian format public static float ReadSingleBigEndian(this BinaryReader reader) { byte[] buffer = reader.ReadBytes(4); return buffer.ToSingleBigEndian(); } /// /// Reads in little-endian format public static float ReadSingleLittleEndian(this BinaryReader reader) { byte[] buffer = reader.ReadBytes(4); return buffer.ToSingleLittleEndian(); } /// /// Read an Int48 from the base stream /// /// Reads in machine native format public static Int48 ReadInt48(this BinaryReader reader) { if (BitConverter.IsLittleEndian) return reader.ReadInt48LittleEndian(); else return reader.ReadInt48BigEndian(); } /// /// Read an Int48 from the base stream /// /// Reads in big-endian format public static Int48 ReadInt48BigEndian(this BinaryReader reader) { byte[] buffer = reader.ReadBytes(6); return buffer.ToInt48BigEndian(); } /// /// Read an Int48 from the base stream /// /// Reads in little-endian format public static Int48 ReadInt48LittleEndian(this BinaryReader reader) { byte[] buffer = reader.ReadBytes(6); return buffer.ToInt48LittleEndian(); } /// /// Read a UInt48 from the base stream /// /// Reads in machine native format public static UInt48 ReadUInt48(this BinaryReader reader) { if (BitConverter.IsLittleEndian) return reader.ReadUInt48LittleEndian(); else return reader.ReadUInt48BigEndian(); } /// /// Read a UInt48 from the base stream /// /// Reads in big-endian format public static UInt48 ReadUInt48BigEndian(this BinaryReader reader) { byte[] buffer = reader.ReadBytes(6); return buffer.ToUInt48BigEndian(); } /// /// Read an UInt48 from the base stream /// /// Reads in little-endian format public static UInt48 ReadUInt48LittleEndian(this BinaryReader reader) { byte[] buffer = reader.ReadBytes(6); return buffer.ToUInt48LittleEndian(); } /// /// Reads in big-endian format public static long ReadInt64BigEndian(this BinaryReader reader) { byte[] buffer = reader.ReadBytes(8); return buffer.ToInt64BigEndian(); } /// /// Reads in little-endian format public static long ReadInt64LittleEndian(this BinaryReader reader) { byte[] buffer = reader.ReadBytes(8); return buffer.ToInt64LittleEndian(); } /// /// Reads in both-endian format public static BothInt64 ReadInt64BothEndian(this BinaryReader reader) { long le = reader.ReadInt64LittleEndian(); long be = reader.ReadInt64BigEndian(); return new BothInt64(le, be); } /// /// Reads in big-endian format public static ulong ReadUInt64BigEndian(this BinaryReader reader) { byte[] buffer = reader.ReadBytes(8); return buffer.ToUInt64BigEndian(); } /// /// Reads in little-endian format public static ulong ReadUInt64LittleEndian(this BinaryReader reader) { byte[] buffer = reader.ReadBytes(8); return buffer.ToUInt64LittleEndian(); } /// /// Reads in both-endian format public static BothUInt64 ReadUInt64BothEndian(this BinaryReader reader) { ulong le = reader.ReadUInt64LittleEndian(); ulong be = reader.ReadUInt64BigEndian(); return new BothUInt64(le, be); } /// /// Read a QWORD (8-byte) from the base stream /// /// Reads in machine native format public static ulong ReadQWORD(this BinaryReader reader) => reader.ReadUInt64(); /// /// Read a QWORD (8-byte) from the base stream /// /// Reads in big-endian format public static ulong ReadQWORDBigEndian(this BinaryReader reader) => reader.ReadUInt64BigEndian(); /// /// Read a QWORD (8-byte) from the base stream /// /// Reads in little-endian format public static ulong ReadQWORDLittleEndian(this BinaryReader reader) => reader.ReadUInt64LittleEndian(); /// /// Read a QWORD (8-byte) from the base stream /// /// Reads in both-endian format public static BothUInt64 ReadQWORDBothEndian(this BinaryReader reader) => reader.ReadUInt64BothEndian(); /// /// Reads in big-endian format public static double ReadDoubleBigEndian(this BinaryReader reader) { byte[] buffer = reader.ReadBytes(8); return buffer.ToDoubleBigEndian(); } /// /// Reads in little-endian format public static double ReadDoubleLittleEndian(this BinaryReader reader) { byte[] buffer = reader.ReadBytes(8); return buffer.ToDoubleLittleEndian(); } /// /// Read a Guid from the underlying stream /// /// Reads in machine native format public static Guid ReadGuid(this BinaryReader reader) { if (BitConverter.IsLittleEndian) return reader.ReadGuidLittleEndian(); else return reader.ReadGuidBigEndian(); } /// /// Read a Guid from the underlying stream /// /// Reads in big-endian format public static Guid ReadGuidBigEndian(this BinaryReader reader) { byte[] buffer = reader.ReadBytes(16); return buffer.ToGuidBigEndian(); } /// /// Read a Guid from the underlying stream /// /// Reads in little-endian format public static Guid ReadGuidLittleEndian(this BinaryReader reader) { byte[] buffer = reader.ReadBytes(16); return buffer.ToGuidLittleEndian(); } #if NET7_0_OR_GREATER /// /// Read an Int128 from the underlying stream /// /// Reads in machine native format public static Int128 ReadInt128(this BinaryReader reader) { if (BitConverter.IsLittleEndian) return reader.ReadInt128LittleEndian(); else return reader.ReadInt128BigEndian(); } /// /// Read an Int128 from the underlying stream /// /// Reads in big-endian format public static Int128 ReadInt128BigEndian(this BinaryReader reader) { byte[] buffer = reader.ReadBytes(16); return buffer.ToInt128BigEndian(); } /// /// Read an Int128 from the underlying stream /// /// Reads in little-endian format public static Int128 ReadInt128LittleEndian(this BinaryReader reader) { byte[] buffer = reader.ReadBytes(16); return buffer.ToInt128LittleEndian(); } /// /// Read a UInt128 from the underlying stream /// /// Reads in machine native format public static UInt128 ReadUInt128(this BinaryReader reader) { if (BitConverter.IsLittleEndian) return reader.ReadUInt128LittleEndian(); else return reader.ReadUInt128BigEndian(); } /// /// Read a UInt128 from the underlying stream /// /// Reads in big-endian format public static UInt128 ReadUInt128BigEndian(this BinaryReader reader) { byte[] buffer = reader.ReadBytes(16); return buffer.ToUInt128BigEndian(); } /// /// Read a UInt128 from the underlying stream /// /// Reads in little-endian format public static UInt128 ReadUInt128LittleEndian(this BinaryReader reader) { byte[] buffer = reader.ReadBytes(16); return buffer.ToUInt128LittleEndian(); } #endif /// /// Reads in big-endian format public static decimal ReadDecimalBigEndian(this BinaryReader reader) { byte[] buffer = reader.ReadBytes(16); return buffer.ToDecimalBigEndian(); } /// /// Reads in little-endian format public static decimal ReadDecimalLittleEndian(this BinaryReader reader) { byte[] buffer = reader.ReadBytes(16); return buffer.ToDecimalLittleEndian(); } #endregion #region Peek Read /// /// Peek a UInt8 from the base stream /// /// Only works properly on seekable streams public static byte PeekByte(this BinaryReader reader) { byte value = reader.ReadByte(); reader.BaseStream.Seek(-1, SeekOrigin.Current); return value; } /// /// Peek a UInt8 from the base stream /// /// Only works properly on seekable streams public static byte PeekByteValue(this BinaryReader reader) => reader.PeekByte(); /// /// Peek a UInt8 from the base stream /// /// Reads in both-endian format /// Only works properly on seekable streams public static BothUInt8 PeekByteBothEndian(this BinaryReader reader) { BothUInt8 value = reader.ReadByteBothEndian(); reader.BaseStream.Seek(-2, SeekOrigin.Current); return value; } /// /// Peek a UInt8[] from the base stream /// /// Only works properly on seekable streams public static byte[] PeekBytes(this BinaryReader reader, int count) { byte[] value = reader.ReadBytes(count); reader.BaseStream.Seek(-count, SeekOrigin.Current); return value; } /// /// Peek an Int8 from the base stream /// /// Only works properly on seekable streams public static sbyte PeekSByte(this BinaryReader reader) { sbyte value = reader.ReadSByte(); reader.BaseStream.Seek(-1, SeekOrigin.Current); return value; } /// /// Peek a Int8 from the base stream /// /// Reads in both-endian format /// Only works properly on seekable streams public static BothInt8 PeekSByteBothEndian(this BinaryReader reader) { BothInt8 value = reader.ReadSByteBothEndian(); reader.BaseStream.Seek(-2, SeekOrigin.Current); return value; } /// /// Peek an Int16 from the base stream /// /// Reads in machine native format /// Only works properly on seekable streams public static short PeekInt16(this BinaryReader reader) { if (BitConverter.IsLittleEndian) return reader.PeekInt16LittleEndian(); else return reader.PeekInt16BigEndian(); } /// /// Peek an Int16 from the base stream /// /// Reads in big-endian format /// Only works properly on seekable streams public static short PeekInt16BigEndian(this BinaryReader reader) { short value = reader.ReadInt16BigEndian(); reader.BaseStream.Seek(-2, SeekOrigin.Current); return value; } /// /// Peek an Int16 from the base stream /// /// Reads in little-endian format /// Only works properly on seekable streams public static short PeekInt16LittleEndian(this BinaryReader reader) { short value = reader.ReadInt16LittleEndian(); reader.BaseStream.Seek(-2, SeekOrigin.Current); return value; } /// /// Peek a Int16 from the base stream /// /// Reads in both-endian format /// Only works properly on seekable streams public static BothInt16 PeekInt16BothEndian(this BinaryReader reader) { BothInt16 value = reader.ReadInt16BothEndian(); reader.BaseStream.Seek(-4, SeekOrigin.Current); return value; } /// /// Peek a UInt16 from the base stream /// /// Reads in machine native format /// Only works properly on seekable streams public static ushort PeekUInt16(this BinaryReader reader) { if (BitConverter.IsLittleEndian) return reader.PeekUInt16LittleEndian(); else return reader.PeekUInt16BigEndian(); } /// /// Peek a UInt16 from the base stream /// /// Reads in big-endian format /// Only works properly on seekable streams public static ushort PeekUInt16BigEndian(this BinaryReader reader) { ushort value = reader.ReadUInt16BigEndian(); reader.BaseStream.Seek(-2, SeekOrigin.Current); return value; } /// /// Peek a UInt16 from the base stream /// /// Reads in little-endian format /// Only works properly on seekable streams public static ushort PeekUInt16LittleEndian(this BinaryReader reader) { ushort value = reader.ReadUInt16LittleEndian(); reader.BaseStream.Seek(-2, SeekOrigin.Current); return value; } /// /// Peek a UInt16 from the base stream /// /// Reads in both-endian format /// Only works properly on seekable streams public static BothUInt16 PeekUInt16BothEndian(this BinaryReader reader) { BothUInt16 value = reader.ReadUInt16BothEndian(); reader.BaseStream.Seek(-4, SeekOrigin.Current); return value; } /// /// Peek a WORD (2-byte) from the base stream /// /// Reads in machine native format /// Only works properly on seekable streams public static ushort PeekWORD(this BinaryReader reader) => reader.PeekUInt16(); /// /// Peek a WORD (2-byte) from the base stream /// /// Reads in big-endian format /// Only works properly on seekable streams public static ushort PeekWORDBigEndian(this BinaryReader reader) => reader.PeekUInt16BigEndian(); /// /// Peek a WORD (2-byte) from the base stream /// /// Reads in little-endian format /// Only works properly on seekable streams public static ushort PeekWORDLittleEndian(this BinaryReader reader) => reader.PeekUInt16LittleEndian(); /// /// Peek a WORD (2-byte) from the base stream /// /// Reads in both-endian format /// Only works properly on seekable streams public static BothUInt16 PeekWORDBothEndian(this BinaryReader reader) => reader.PeekUInt16BothEndian(); #if NET5_0_OR_GREATER /// /// Peek a Half from the base stream /// /// Reads in machine native format /// Only works properly on seekable streams public static Half PeekHalf(this BinaryReader reader) { if (BitConverter.IsLittleEndian) return reader.PeekHalfLittleEndian(); else return reader.PeekHalfBigEndian(); } /// /// Peek a Half from the base stream /// /// Reads in big-endian format /// Only works properly on seekable streams public static Half PeekHalfBigEndian(this BinaryReader reader) { Half value = reader.ReadHalfBigEndian(); reader.BaseStream.Seek(-2, SeekOrigin.Current); return value; } /// /// Peek a Half from the base stream /// /// Reads in little-endian format /// Only works properly on seekable streams public static Half PeekHalfLittleEndian(this BinaryReader reader) { Half value = reader.ReadHalfLittleEndian(); reader.BaseStream.Seek(-2, SeekOrigin.Current); return value; } #endif /// /// Peek an Int24 from the base stream /// /// Reads in machine native format /// Only works properly on seekable streams public static Int24 PeekInt24(this BinaryReader reader) { if (BitConverter.IsLittleEndian) return reader.PeekInt24LittleEndian(); else return reader.PeekInt24BigEndian(); } /// /// Peek an Int24 from the base stream /// /// Reads in big-endian format /// Only works properly on seekable streams public static Int24 PeekInt24BigEndian(this BinaryReader reader) { Int24 value = reader.ReadInt24BigEndian(); reader.BaseStream.Seek(-3, SeekOrigin.Current); return value; } /// /// Peek an Int24 from the base stream /// /// Reads in little-endian format /// Only works properly on seekable streams public static Int24 PeekInt24LittleEndian(this BinaryReader reader) { Int24 value = reader.ReadInt24LittleEndian(); reader.BaseStream.Seek(-3, SeekOrigin.Current); return value; } /// /// Peek a UInt24 from the base stream /// /// Reads in machine native format /// Only works properly on seekable streams public static UInt24 PeekUInt24(this BinaryReader reader) { if (BitConverter.IsLittleEndian) return reader.PeekUInt24LittleEndian(); else return reader.PeekUInt24BigEndian(); } /// /// Peek a UInt24 from the base stream /// /// Reads in big-endian format /// Only works properly on seekable streams public static UInt24 PeekUInt24BigEndian(this BinaryReader reader) { UInt24 value = reader.ReadUInt24BigEndian(); reader.BaseStream.Seek(-3, SeekOrigin.Current); return value; } /// /// Peek a UInt24 from the base stream /// /// Reads in little-endian format /// Only works properly on seekable streams public static UInt24 PeekUInt24LittleEndian(this BinaryReader reader) { UInt24 value = reader.ReadUInt24LittleEndian(); reader.BaseStream.Seek(-3, SeekOrigin.Current); return value; } /// /// Peek an Int32 from the base stream /// /// Reads in machine native format /// Only works properly on seekable streams public static int PeekInt32(this BinaryReader reader) { if (BitConverter.IsLittleEndian) return reader.PeekInt32LittleEndian(); else return reader.PeekInt32BigEndian(); } /// /// Peek an Int32 from the base stream /// /// Reads in big-endian format /// Only works properly on seekable streams public static int PeekInt32BigEndian(this BinaryReader reader) { int value = reader.ReadInt32BigEndian(); reader.BaseStream.Seek(-4, SeekOrigin.Current); return value; } /// /// Peek an Int32 from the base stream /// /// Reads in little-endian format /// Only works properly on seekable streams public static int PeekInt32LittleEndian(this BinaryReader reader) { int value = reader.ReadInt32LittleEndian(); reader.BaseStream.Seek(-4, SeekOrigin.Current); return value; } /// /// Peek a Int32 from the base stream /// /// Reads in both-endian format /// Only works properly on seekable streams public static BothInt32 PeekInt32BothEndian(this BinaryReader reader) { BothInt32 value = reader.ReadInt32BothEndian(); reader.BaseStream.Seek(-8, SeekOrigin.Current); return value; } /// /// Peek a UInt32 from the base stream /// /// Reads in machine native format /// Only works properly on seekable streams public static uint PeekUInt32(this BinaryReader reader) { if (BitConverter.IsLittleEndian) return reader.PeekUInt32LittleEndian(); else return reader.PeekUInt32BigEndian(); } /// /// Peek a UInt32 from the base stream /// /// Reads in big-endian format /// Only works properly on seekable streams public static uint PeekUInt32BigEndian(this BinaryReader reader) { uint value = reader.ReadUInt32BigEndian(); reader.BaseStream.Seek(-4, SeekOrigin.Current); return value; } /// /// Peek a UInt32 from the base stream /// /// Reads in little-endian format /// Only works properly on seekable streams public static uint PeekUInt32LittleEndian(this BinaryReader reader) { uint value = reader.ReadUInt32LittleEndian(); reader.BaseStream.Seek(-4, SeekOrigin.Current); return value; } /// /// Peek a UInt32 from the base stream /// /// Reads in both-endian format /// Only works properly on seekable streams public static BothUInt32 PeekUInt32BothEndian(this BinaryReader reader) { BothUInt32 value = reader.ReadUInt32BothEndian(); reader.BaseStream.Seek(-8, SeekOrigin.Current); return value; } /// /// Peek a DWORD (4-byte) from the base stream /// /// Reads in machine native format /// Only works properly on seekable streams public static uint PeekDWORD(this BinaryReader reader) => reader.PeekUInt32(); /// /// Peek a DWORD (4-byte) from the base stream /// /// Reads in big-endian format /// Only works properly on seekable streams public static uint PeekDWORDBigEndian(this BinaryReader reader) => reader.PeekUInt32BigEndian(); /// /// Peek a DWORD (4-byte) from the base stream /// /// Reads in little-endian format /// Only works properly on seekable streams public static uint PeekDWORDLittleEndian(this BinaryReader reader) => reader.PeekUInt32LittleEndian(); /// /// Peek a DWORD (4-byte) from the base stream /// /// Reads in both-endian format /// Only works properly on seekable streams public static BothUInt32 PeekDWORDBothEndian(this BinaryReader reader) => reader.PeekUInt32BothEndian(); /// /// Peek a Single from the base stream /// /// Reads in machine native format /// Only works properly on seekable streams public static float PeekSingle(this BinaryReader reader) { if (BitConverter.IsLittleEndian) return reader.PeekSingleLittleEndian(); else return reader.PeekSingleBigEndian(); } /// /// Peek a Single from the base stream /// /// Reads in big-endian format /// Only works properly on seekable streams public static float PeekSingleBigEndian(this BinaryReader reader) { float value = reader.ReadSingleBigEndian(); reader.BaseStream.Seek(-4, SeekOrigin.Current); return value; } /// /// Peek a Single from the base stream /// /// Reads in little-endian format /// Only works properly on seekable streams public static float PeekSingleLittleEndian(this BinaryReader reader) { float value = reader.ReadSingleLittleEndian(); reader.BaseStream.Seek(-4, SeekOrigin.Current); return value; } /// /// Peek an Int48 from the base stream /// /// Reads in machine native format /// Only works properly on seekable streams public static Int48 PeekInt48(this BinaryReader reader) { if (BitConverter.IsLittleEndian) return reader.PeekInt48LittleEndian(); else return reader.PeekInt48BigEndian(); } /// /// Peek an Int48 from the base stream /// /// Reads in big-endian format /// Only works properly on seekable streams public static Int48 PeekInt48BigEndian(this BinaryReader reader) { Int48 value = reader.ReadInt48BigEndian(); reader.BaseStream.Seek(-6, SeekOrigin.Current); return value; } /// /// Peek an Int48 from the base stream /// /// Reads in little-endian format /// Only works properly on seekable streams public static Int48 PeekInt48LittleEndian(this BinaryReader reader) { Int48 value = reader.ReadInt48LittleEndian(); reader.BaseStream.Seek(-6, SeekOrigin.Current); return value; } /// /// Peek a UInt48 from the base stream /// /// Reads in machine native format /// Only works properly on seekable streams public static UInt48 PeekUInt48(this BinaryReader reader) { if (BitConverter.IsLittleEndian) return reader.PeekUInt48LittleEndian(); else return reader.PeekUInt48BigEndian(); } /// /// Peek an UInt48 from the base stream /// /// Reads in big-endian format /// Only works properly on seekable streams public static UInt48 PeekUInt48BigEndian(this BinaryReader reader) { UInt48 value = reader.ReadUInt48BigEndian(); reader.BaseStream.Seek(-6, SeekOrigin.Current); return value; } /// /// Peek an UInt48 from the base stream /// /// Reads in little-endian format /// Only works properly on seekable streams public static UInt48 PeekUInt48LittleEndian(this BinaryReader reader) { UInt48 value = reader.ReadUInt48LittleEndian(); reader.BaseStream.Seek(-6, SeekOrigin.Current); return value; } /// /// Peek an Int64 from the base stream /// /// Reads in machine native format /// Only works properly on seekable streams public static long PeekInt64(this BinaryReader reader) { if (BitConverter.IsLittleEndian) return reader.PeekInt64LittleEndian(); else return reader.PeekInt64BigEndian(); } /// /// Peek an Int64 from the base stream /// /// Reads in big-endian format /// Only works properly on seekable streams public static long PeekInt64BigEndian(this BinaryReader reader) { long value = reader.ReadInt64BigEndian(); reader.BaseStream.Seek(-8, SeekOrigin.Current); return value; } /// /// Peek an Int64 from the base stream /// /// Reads in big-endian format /// Only works properly on seekable streams public static long PeekInt64LittleEndian(this BinaryReader reader) { long value = reader.ReadInt64LittleEndian(); reader.BaseStream.Seek(-8, SeekOrigin.Current); return value; } /// /// Peek a Int64 from the base stream /// /// Reads in both-endian format /// Only works properly on seekable streams public static BothInt64 PeekInt64BothEndian(this BinaryReader reader) { BothInt64 value = reader.ReadInt64BothEndian(); reader.BaseStream.Seek(-16, SeekOrigin.Current); return value; } /// /// Peek a UInt64 from the base stream /// /// Reads in machine native format /// Only works properly on seekable streams public static ulong PeekUInt64(this BinaryReader reader) { if (BitConverter.IsLittleEndian) return reader.PeekUInt64LittleEndian(); else return reader.PeekUInt64BigEndian(); } /// /// Peek a UInt64 from the base stream /// /// Reads in big-endian format /// Only works properly on seekable streams public static ulong PeekUInt64BigEndian(this BinaryReader reader) { ulong value = reader.ReadUInt64BigEndian(); reader.BaseStream.Seek(-8, SeekOrigin.Current); return value; } /// /// Peek a UInt64 from the base stream /// /// Reads in little-endian format /// Only works properly on seekable streams public static ulong PeekUInt64LittleEndian(this BinaryReader reader) { ulong value = reader.ReadUInt64LittleEndian(); reader.BaseStream.Seek(-8, SeekOrigin.Current); return value; } /// /// Peek a UInt64 from the base stream /// /// Reads in both-endian format /// Only works properly on seekable streams public static BothUInt64 PeekUInt64BothEndian(this BinaryReader reader) { BothUInt64 value = reader.ReadUInt64BothEndian(); reader.BaseStream.Seek(-16, SeekOrigin.Current); return value; } /// /// Peek a QWORD (8-byte) from the base stream /// /// Reads in machine native format /// Only works properly on seekable streams public static ulong PeekQWORD(this BinaryReader reader) => reader.PeekUInt64(); /// /// Peek a QWORD (8-byte) from the base stream /// /// Reads in big-endian format /// Only works properly on seekable streams public static ulong PeekQWORDBigEndian(this BinaryReader reader) => reader.PeekUInt64BigEndian(); /// /// Peek a QWORD (8-byte) from the base stream /// /// Reads in little-endian format /// Only works properly on seekable streams public static ulong PeekQWORDLittleEndian(this BinaryReader reader) => reader.PeekUInt64LittleEndian(); /// /// Peek a QWORD (8-byte) from the base stream /// /// Reads in both-endian format /// Only works properly on seekable streams public static BothUInt64 PeekQWORDBothEndian(this BinaryReader reader) => reader.PeekUInt64BothEndian(); /// /// Peek a Double from the base stream /// /// Reads in machine native format /// Only works properly on seekable streams public static double PeekDouble(this BinaryReader reader) { if (BitConverter.IsLittleEndian) return reader.PeekDoubleLittleEndian(); else return reader.PeekDoubleBigEndian(); } /// /// Peek a Double from the base stream /// /// Reads in big-endian format /// Only works properly on seekable streams public static double PeekDoubleBigEndian(this BinaryReader reader) { double value = reader.ReadDoubleBigEndian(); reader.BaseStream.Seek(-8, SeekOrigin.Current); return value; } /// /// Peek a Double from the base stream /// /// Reads in little-endian format /// Only works properly on seekable streams public static double PeekDoubleLittleEndian(this BinaryReader reader) { double value = reader.ReadDoubleLittleEndian(); reader.BaseStream.Seek(-8, SeekOrigin.Current); return value; } /// /// Peek a Guid from the base stream /// /// Reads in machine native format /// Only works properly on seekable streams public static Guid PeekGuid(this BinaryReader reader) { if (BitConverter.IsLittleEndian) return reader.PeekGuidLittleEndian(); else return reader.PeekGuidBigEndian(); } /// /// Peek a Guid from the base stream /// /// Reads in big-endian format /// Only works properly on seekable streams public static Guid PeekGuidBigEndian(this BinaryReader reader) { Guid value = reader.ReadGuidBigEndian(); reader.BaseStream.Seek(-16, SeekOrigin.Current); return value; } /// /// Peek a Guid from the base stream /// /// Reads in little-endian format /// Only works properly on seekable streams public static Guid PeekGuidLittleEndian(this BinaryReader reader) { Guid value = reader.ReadGuidLittleEndian(); reader.BaseStream.Seek(-16, SeekOrigin.Current); return value; } #if NET7_0_OR_GREATER /// /// Peek an Int128 from the base stream /// /// Reads in machine native format /// Only works properly on seekable streams public static Int128 PeekInt128(this BinaryReader reader) { if (BitConverter.IsLittleEndian) return reader.PeekInt128LittleEndian(); else return reader.PeekInt128BigEndian(); } /// /// Peek an Int128 from the base stream /// /// Reads in big-endian format /// Only works properly on seekable streams public static Int128 PeekInt128BigEndian(this BinaryReader reader) { Int128 value = reader.ReadInt128BigEndian(); reader.BaseStream.Seek(-16, SeekOrigin.Current); return value; } /// /// Peek an Int128 from the base stream /// /// Reads in little-endian format /// Only works properly on seekable streams public static Int128 PeekInt128LittleEndian(this BinaryReader reader) { Int128 value = reader.ReadInt128LittleEndian(); reader.BaseStream.Seek(-16, SeekOrigin.Current); return value; } /// /// Peek a UInt128 from the base stream /// /// Reads in machine native format /// Only works properly on seekable streams public static UInt128 PeekUInt128(this BinaryReader reader) { if (BitConverter.IsLittleEndian) return reader.PeekUInt128LittleEndian(); else return reader.PeekUInt128BigEndian(); } /// /// Peek a UInt128 from the base stream /// /// Reads in big-endian format /// Only works properly on seekable streams public static UInt128 PeekUInt128BigEndian(this BinaryReader reader) { UInt128 value = reader.ReadUInt128BigEndian(); reader.BaseStream.Seek(-16, SeekOrigin.Current); return value; } /// /// Peek a UInt128 from the base stream /// /// Reads in little-endian format /// Only works properly on seekable streams public static UInt128 PeekUInt128LittleEndian(this BinaryReader reader) { UInt128 value = reader.ReadUInt128LittleEndian(); reader.BaseStream.Seek(-16, SeekOrigin.Current); return value; } #endif /// /// Peek a Decimal from the base stream /// /// Reads in machine native format /// Only works properly on seekable streams public static decimal PeekDecimal(this BinaryReader reader) { if (BitConverter.IsLittleEndian) return reader.PeekDecimalLittleEndian(); else return reader.PeekDecimalBigEndian(); } /// /// Peek a Decimal from the base stream /// /// Reads in big-endian format /// Only works properly on seekable streams public static decimal PeekDecimalBigEndian(this BinaryReader reader) { decimal value = reader.ReadDecimalBigEndian(); reader.BaseStream.Seek(-16, SeekOrigin.Current); return value; } /// /// Peek a Decimal from the base stream /// /// Reads in little-endian format /// Only works properly on seekable streams public static decimal PeekDecimalLittleEndian(this BinaryReader reader) { decimal value = reader.ReadDecimalLittleEndian(); reader.BaseStream.Seek(-16, SeekOrigin.Current); return value; } #endregion #region Try Read /// /// Read a UInt8 from the base stream /// public static bool TryReadByteValue(this BinaryReader reader, out byte value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 1) { value = default; return false; } value = reader.ReadByte(); return true; } /// /// Read a UInt8 from the base stream /// /// Reads in both-endian format public static bool TryReadByteBothEndian(this BinaryReader reader, out BothUInt8 value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 2) { value = default(byte); return false; } value = reader.ReadByteBothEndian(); return true; } /// /// Read a UInt8[] from the base stream /// public static bool TryReadBytes(this BinaryReader reader, int count, out byte[] value) { if (reader.BaseStream.Position > reader.BaseStream.Length - count) { value = []; return false; } value = reader.ReadBytes(count); return true; } /// /// Read an Int8 from the base stream /// public static bool TryReadSByte(this BinaryReader reader, out sbyte value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 1) { value = default; return false; } value = reader.ReadSByte(); return true; } /// /// Read a Int8 from the base stream /// /// Reads in both-endian format public static bool TryReadSByteBothEndian(this BinaryReader reader, out BothInt8 value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 2) { value = default(sbyte); return false; } value = reader.ReadSByteBothEndian(); return true; } /// /// Read a Char from the base stream /// public static bool TryReadChar(this BinaryReader reader, out char value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 1) { value = default; return false; } value = reader.ReadChar(); return true; } /// /// Read an Int16 from the base stream /// /// Reads in machine native format public static bool TryReadInt16(this BinaryReader reader, out short value) { if (BitConverter.IsLittleEndian) return reader.TryReadInt16LittleEndian(out value); else return reader.TryReadInt16BigEndian(out value); } /// /// Read an Int16 from the base stream /// /// Reads in big-endian format public static bool TryReadInt16BigEndian(this BinaryReader reader, out short value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 2) { value = default; return false; } value = reader.ReadInt16BigEndian(); return true; } /// /// Read an Int16 from the base stream /// /// Reads in little-endian format public static bool TryReadInt16LittleEndian(this BinaryReader reader, out short value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 2) { value = default; return false; } value = reader.ReadInt16LittleEndian(); return true; } /// /// Read a Int16 from the base stream /// /// Reads in both-endian format public static bool TryReadInt16BothEndian(this BinaryReader reader, out BothInt16 value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 4) { value = default(short); return false; } value = reader.ReadInt16BothEndian(); return true; } /// /// Read a UInt16 from the base stream /// /// Reads in machine native format public static bool TryReadUInt16(this BinaryReader reader, out ushort value) { if (BitConverter.IsLittleEndian) return reader.TryReadUInt16LittleEndian(out value); else return reader.TryReadUInt16BigEndian(out value); } /// /// Read a UInt16 from the base stream /// /// Reads in big-endian format public static bool TryReadUInt16BigEndian(this BinaryReader reader, out ushort value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 2) { value = default; return false; } value = reader.ReadUInt16BigEndian(); return true; } /// /// Read a UInt16 from the base stream /// /// Reads in little-endian format public static bool TryReadUInt16LittleEndian(this BinaryReader reader, out ushort value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 2) { value = default; return false; } value = reader.ReadUInt16LittleEndian(); return true; } /// /// Read a UInt16 from the base stream /// /// Reads in both-endian format public static bool TryReadUInt16BothEndian(this BinaryReader reader, out BothUInt16 value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 4) { value = default(ushort); return false; } value = reader.ReadUInt16BothEndian(); return true; } /// /// Read a WORD (2-byte) from the base stream /// /// Reads in machine native format public static bool TryReadWORD(this BinaryReader reader, out ushort value) => reader.TryReadUInt16(out value); /// /// Read a WORD (2-byte) from the base stream /// /// Reads in big-endian format public static bool TryReadWORDBigEndian(this BinaryReader reader, out ushort value) => reader.TryReadUInt16BigEndian(out value); /// /// Read a WORD (2-byte) from the base stream /// /// Reads in little-endian format public static bool TryReadWORDLittleEndian(this BinaryReader reader, out ushort value) => reader.TryReadUInt16LittleEndian(out value); /// /// Read a WORD (2-byte) from the base stream /// /// Reads in both-endian format public static bool TryReadWORDBothEndian(this BinaryReader reader, out BothUInt16 value) => reader.TryReadUInt16BothEndian(out value); #if NET5_0_OR_GREATER /// /// Read a Half from the base stream /// /// Reads in machine native format public static bool TryReadHalf(this BinaryReader reader, out Half value) { if (BitConverter.IsLittleEndian) return reader.TryReadHalfLittleEndian(out value); else return reader.TryReadHalfBigEndian(out value); } /// /// Read a Half from the base stream /// /// Reads in big-endian format public static bool TryReadHalfBigEndian(this BinaryReader reader, out Half value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 2) { value = default; return false; } value = reader.ReadHalfBigEndian(); return true; } /// /// Read a Half from the base stream /// /// Reads in little-endian format public static bool TryReadHalfLittleEndian(this BinaryReader reader, out Half value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 2) { value = default; return false; } value = reader.ReadHalfLittleEndian(); return true; } #endif /// /// Read an Int24 from the base stream /// /// Reads in machine native format public static bool TryReadInt24(this BinaryReader reader, out Int24 value) { if (BitConverter.IsLittleEndian) return reader.TryReadInt24LittleEndian(out value); else return reader.TryReadInt24BigEndian(out value); } /// /// Read an Int24 from the base stream /// /// Reads in big-endian format public static bool TryReadInt24BigEndian(this BinaryReader reader, out Int24 value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 3) { value = new Int24(); return false; } value = reader.ReadInt24BigEndian(); return true; } /// /// Read an Int24 from the base stream /// /// Reads in little-endian format public static bool TryReadInt24LittleEndian(this BinaryReader reader, out Int24 value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 3) { value = new Int24(); return false; } value = reader.ReadInt24LittleEndian(); return true; } /// /// Read a UInt24 from the base stream /// /// Reads in machine native format public static bool TryReadUInt24(this BinaryReader reader, out UInt24 value) { if (BitConverter.IsLittleEndian) return reader.TryReadUInt24LittleEndian(out value); else return reader.TryReadUInt24BigEndian(out value); } /// /// Read a UInt24 from the base stream /// /// Reads in big-endian format public static bool TryReadUInt24BigEndian(this BinaryReader reader, out UInt24 value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 3) { value = new UInt24(); return false; } value = reader.ReadUInt24BigEndian(); return true; } /// /// Read a UInt24 from the base stream /// /// Reads in little-endian format public static bool TryReadUInt24LittleEndian(this BinaryReader reader, out UInt24 value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 3) { value = new UInt24(); return false; } value = reader.ReadUInt24LittleEndian(); return true; } /// /// Read an Int32 from the base stream /// /// Reads in machine native format public static bool TryReadInt32(this BinaryReader reader, out int value) { if (BitConverter.IsLittleEndian) return reader.TryReadInt32LittleEndian(out value); else return reader.TryReadInt32BigEndian(out value); } /// /// Read an Int32 from the base stream /// /// Reads in big-endian format public static bool TryReadInt32BigEndian(this BinaryReader reader, out int value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 4) { value = default; return false; } value = reader.ReadInt32BigEndian(); return true; } /// /// Read an Int32 from the base stream /// /// Reads in little-endian format public static bool TryReadInt32LittleEndian(this BinaryReader reader, out int value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 4) { value = default; return false; } value = reader.ReadInt32LittleEndian(); return true; } /// /// Read a Int32 from the base stream /// /// Reads in both-endian format public static bool TryReadInt32BothEndian(this BinaryReader reader, out BothInt32 value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 8) { value = default(int); return false; } value = reader.ReadInt32BothEndian(); return true; } /// /// Read a UInt32 from the base stream /// /// Reads in machine native format public static bool TryReadUInt32(this BinaryReader reader, out uint value) { if (BitConverter.IsLittleEndian) return reader.TryReadUInt32LittleEndian(out value); else return reader.TryReadUInt32BigEndian(out value); } /// /// Read a UInt32 from the base stream /// /// Reads in big-endian format public static bool TryReadUInt32BigEndian(this BinaryReader reader, out uint value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 4) { value = default; return false; } value = reader.ReadUInt32BigEndian(); return true; } /// /// Read a UInt32 from the base stream /// /// Reads in little-endian format public static bool TryReadUInt32LittleEndian(this BinaryReader reader, out uint value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 4) { value = default; return false; } value = reader.ReadUInt32LittleEndian(); return true; } /// /// Read a UInt32 from the base stream /// /// Reads in both-endian format public static bool TryReadUInt32BothEndian(this BinaryReader reader, out BothUInt32 value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 8) { value = default(uint); return false; } value = reader.ReadUInt32BothEndian(); return true; } /// /// Read a DWORD (4-byte) from the base stream /// /// Reads in machine native format public static bool TryReadDWORD(this BinaryReader reader, out uint value) => reader.TryReadUInt32(out value); /// /// Read a DWORD (4-byte) from the base stream /// /// Reads in big-endian format public static bool TryReadDWORDBigEndian(this BinaryReader reader, out uint value) => reader.TryReadUInt32BigEndian(out value); /// /// Read a DWORD (4-byte) from the base stream /// /// Reads in little-endian format public static bool TryReadDWORDLittleEndian(this BinaryReader reader, out uint value) => reader.TryReadUInt32LittleEndian(out value); /// /// Read a DWORD (4-byte) from the base stream /// /// Reads in both-endian format public static bool TryReadDWORDBothEndian(this BinaryReader reader, out BothUInt32 value) => reader.TryReadUInt32BothEndian(out value); /// /// Read a Single from the base stream /// /// Reads in machine native format public static bool TryReadSingle(this BinaryReader reader, out float value) { if (BitConverter.IsLittleEndian) return reader.TryReadSingleLittleEndian(out value); else return reader.TryReadSingleBigEndian(out value); } /// /// Read a Single from the base stream /// /// Reads in big-endian format public static bool TryReadSingleBigEndian(this BinaryReader reader, out float value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 4) { value = default; return false; } value = reader.ReadSingleBigEndian(); return true; } /// /// Read a Single from the base stream /// /// Reads in little-endian format public static bool TryReadSingleLittleEndian(this BinaryReader reader, out float value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 4) { value = default; return false; } value = reader.ReadSingleLittleEndian(); return true; } /// /// Read an Int48 from the base stream /// /// Reads in machine native format public static bool TryReadInt48(this BinaryReader reader, out Int48 value) { if (BitConverter.IsLittleEndian) return reader.TryReadInt48LittleEndian(out value); else return reader.TryReadInt48BigEndian(out value); } /// /// Read an Int48 from the base stream /// /// Reads in big-endian format public static bool TryReadInt48BigEndian(this BinaryReader reader, out Int48 value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 6) { value = new Int48(); return false; } value = reader.ReadInt48BigEndian(); return true; } /// /// Read an Int48 from the base stream /// /// Reads in little-endian format public static bool TryReadInt48LittleEndian(this BinaryReader reader, out Int48 value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 6) { value = new Int48(); return false; } value = reader.ReadInt48LittleEndian(); return true; } /// /// Read a UInt48 from the base stream /// /// Reads in machine native format public static bool TryReadUInt48(this BinaryReader reader, out UInt48 value) { if (BitConverter.IsLittleEndian) return reader.TryReadUInt48LittleEndian(out value); else return reader.TryReadUInt48BigEndian(out value); } /// /// Read a UInt48 from the base stream /// /// Reads in big-endian format public static bool TryReadUInt48BigEndian(this BinaryReader reader, out UInt48 value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 6) { value = new UInt48(); return false; } value = reader.ReadUInt48BigEndian(); return true; } /// /// Read an UInt48 from the base stream /// /// Reads in little-endian format public static bool TryReadUInt48LittleEndian(this BinaryReader reader, out UInt48 value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 6) { value = new UInt48(); return false; } value = reader.ReadUInt48LittleEndian(); return true; } /// /// Read an Int64 from the base stream /// /// Reads in machine native format public static bool TryReadInt64(this BinaryReader reader, out long value) { if (BitConverter.IsLittleEndian) return reader.TryReadInt64LittleEndian(out value); else return reader.TryReadInt64BigEndian(out value); } /// /// Read an Int64 from the base stream /// /// Reads in big-endian format public static bool TryReadInt64BigEndian(this BinaryReader reader, out long value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 8) { value = default; return false; } value = reader.ReadInt64BigEndian(); return true; } /// /// Read an Int64 from the base stream /// /// Reads in little-endian format public static bool TryReadInt64LittleEndian(this BinaryReader reader, out long value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 8) { value = default; return false; } value = reader.ReadInt64BigEndian(); return true; } /// /// Read a Int64 from the base stream /// /// Reads in both-endian format public static bool TryReadInt64BothEndian(this BinaryReader reader, out BothInt64 value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 16) { value = default(long); return false; } value = reader.ReadInt64BothEndian(); return true; } /// /// Read a UInt64 from the base stream /// /// Reads in machine native format public static bool TryReadUInt64(this BinaryReader reader, out ulong value) { if (BitConverter.IsLittleEndian) return reader.TryReadUInt64LittleEndian(out value); else return reader.TryReadUInt64BigEndian(out value); } /// /// Read a UInt64 from the base stream /// /// Reads in big-endian format public static bool TryReadUInt64BigEndian(this BinaryReader reader, out ulong value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 8) { value = default; return false; } value = reader.ReadUInt64BigEndian(); return true; } /// /// Read a UInt64 from the base stream /// /// Reads in little-endian format public static bool TryReadUInt64LittleEndian(this BinaryReader reader, out ulong value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 8) { value = default; return false; } value = reader.ReadUInt64LittleEndian(); return true; } /// /// Read a UInt64 from the base stream /// /// Reads in both-endian format public static bool TryReadUInt64BothEndian(this BinaryReader reader, out BothUInt64 value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 16) { value = default(ulong); return false; } value = reader.ReadUInt64BothEndian(); return true; } /// /// Read a QWORD (8-byte) from the base stream /// /// Reads in machine native format public static bool TryReadQWORD(this BinaryReader reader, out ulong value) => reader.TryReadUInt64(out value); /// /// Read a QWORD (8-byte) from the base stream /// /// Reads in big-endian format public static bool TryReadQWORDBigEndian(this BinaryReader reader, out ulong value) => reader.TryReadUInt64BigEndian(out value); /// /// Read a QWORD (8-byte) from the base stream /// /// Reads in little-endian format public static bool TryReadQWORDLittleEndian(this BinaryReader reader, out ulong value) => reader.TryReadUInt64LittleEndian(out value); /// /// Read a QWORD (8-byte) from the base stream /// /// Reads in both-endian format public static bool TryReadQWORDBothEndian(this BinaryReader reader, out BothUInt64 value) => reader.TryReadUInt64BothEndian(out value); /// /// Read a Double from the base stream /// /// Reads in machine native format public static bool TryReadDouble(this BinaryReader reader, out double value) { if (BitConverter.IsLittleEndian) return reader.TryReadDoubleLittleEndian(out value); else return reader.TryReadDoubleBigEndian(out value); } /// /// Read a Double from the base stream /// /// Reads in big-endian format public static bool TryReadDoubleBigEndian(this BinaryReader reader, out double value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 8) { value = default; return false; } value = reader.ReadDoubleBigEndian(); return true; } /// /// Read a Double from the base stream /// /// Reads in little-endian format public static bool TryReadDoubleLittleEndian(this BinaryReader reader, out double value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 8) { value = default; return false; } value = reader.ReadDoubleLittleEndian(); return true; } /// /// Read a Guid from the base stream /// /// Reads in machine native format public static bool TryReadGuid(this BinaryReader reader, out Guid value) { if (BitConverter.IsLittleEndian) return reader.TryReadGuidLittleEndian(out value); else return reader.TryReadGuidBigEndian(out value); } /// /// Read a Guid from the base stream /// /// Reads in big-endian format public static bool TryReadGuidBigEndian(this BinaryReader reader, out Guid value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 16) { value = default; return false; } value = reader.ReadGuidBigEndian(); return true; } /// /// Read a Guid from the base stream /// /// Reads in big-endian format public static bool TryReadGuidLittleEndian(this BinaryReader reader, out Guid value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 16) { value = default; return false; } value = reader.ReadGuidLittleEndian(); return true; } #if NET7_0_OR_GREATER /// /// Read an Int128 from the base stream /// /// Reads in machine native format public static bool TryReadInt128(this BinaryReader reader, out Int128 value) { if (BitConverter.IsLittleEndian) return reader.TryReadInt128LittleEndian(out value); else return reader.TryReadInt128BigEndian(out value); } /// /// Read an Int128 from the base stream /// /// Reads in big-endian format public static bool TryReadInt128BigEndian(this BinaryReader reader, out Int128 value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 16) { value = default; return false; } value = reader.ReadInt128BigEndian(); return true; } /// /// Read an Int128 from the base stream /// /// Reads in little-endian format public static bool TryReadInt128LittleEndian(this BinaryReader reader, out Int128 value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 16) { value = default; return false; } value = reader.ReadInt128LittleEndian(); return true; } /// /// Read a UInt128 from the base stream /// /// Reads in machine native format public static bool TryReadUInt128(this BinaryReader reader, out UInt128 value) { if (BitConverter.IsLittleEndian) return reader.TryReadUInt128LittleEndian(out value); else return reader.TryReadUInt128BigEndian(out value); } /// /// Read a UInt128 from the base stream /// /// Reads in big-endian format public static bool TryReadUInt128BigEndian(this BinaryReader reader, out UInt128 value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 16) { value = default; return false; } value = reader.ReadUInt128BigEndian(); return true; } /// /// Read a UInt128 from the base stream /// /// Reads in little-endian format public static bool TryReadUInt128LittleEndian(this BinaryReader reader, out UInt128 value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 16) { value = default; return false; } value = reader.ReadUInt128LittleEndian(); return true; } #endif /// /// Read a Decimal from the base stream /// /// Reads in machine native format public static bool TryReadDecimal(this BinaryReader reader, out decimal value) { if (BitConverter.IsLittleEndian) return reader.TryReadDecimalLittleEndian(out value); else return reader.TryReadDecimalBigEndian(out value); } /// /// Read a Decimal from the base stream /// /// Reads in big-endian format public static bool TryReadDecimalBigEndian(this BinaryReader reader, out decimal value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 16) { value = default; return false; } value = reader.ReadDecimalBigEndian(); return true; } /// /// Read a Decimal from the base stream /// /// Reads in little-endian format public static bool TryReadDecimalLittleEndian(this BinaryReader reader, out decimal value) { if (reader.BaseStream.Position > reader.BaseStream.Length - 16) { value = default; return false; } value = reader.ReadDecimalLittleEndian(); return true; } #endregion } }