From 34ee60437b8391e34aa9504d1d79f34050644bbf Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Wed, 3 Mar 2021 18:39:38 +0000 Subject: [PATCH] (#28) Add floating point read support Introduces: - ReadDecimal([Endianness]) - ReadDouble([Endianness]) - ReadSingle([Endianness]) --- X10D/src/StreamExtensions/StreamExtensions.cs | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/X10D/src/StreamExtensions/StreamExtensions.cs b/X10D/src/StreamExtensions/StreamExtensions.cs index eaa33ab..18af5e2 100644 --- a/X10D/src/StreamExtensions/StreamExtensions.cs +++ b/X10D/src/StreamExtensions/StreamExtensions.cs @@ -53,6 +53,68 @@ namespace X10D.StreamExtensions return crypt.ComputeHash(stream); } + /// + /// Reads a decimal value from the current stream using the system's default endian encoding, and advances the stream + /// position by sixteen bytes. + /// + /// The stream to read. + /// A sixteen-byte decimal value read from the stream. + public static decimal ReadDecimal(this Stream stream) + { + return stream.ReadDecimal(DefaultEndianness); + } + + /// + /// Reads a decimal value from the current stream using a specified endian encoding, and advances the stream position + /// by sixteen bytes. + /// + /// The stream from which the value should be read. + /// The endian encoding to use. + /// A decimal value read from the stream. + public static decimal ReadDecimal(this Stream stream, Endianness endianness) + { + const int decimalSize = sizeof(decimal); + const int int32Size = sizeof(int); + const int partitionSize = decimalSize / int32Size; + + var buffer = new byte[decimalSize]; + stream.Read(buffer, 0, decimalSize); + + Util.SwapIfNeeded(ref buffer, endianness); + + var bits = new int[partitionSize]; + for (var index = 0; index < partitionSize; index += int32Size) + { + Array.Copy(buffer, index, bits, 0, int32Size); + } + + return new decimal(bits); + } + + /// + /// Reads a double-precision floating point value from the current stream using the system's default endian encoding, + /// and advances the stream position by eight bytes. + /// + /// The stream from which the value should be read. + /// A double-precision floating point value read from the stream. + public static double ReadDouble(this Stream stream) + { + return stream.ReadDouble(DefaultEndianness); + } + + /// + /// Reads a double-precision floating point value from the current stream using a specified endian encoding, and + /// advances the stream position by eight bytes. + /// + /// The stream from which the value should be read. + /// The endian encoding to use. + /// A double-precision floating point value read from the stream. + public static double ReadDouble(this Stream stream, Endianness endianness) + { + var value = ReadInternal(stream, endianness); + return BitConverter.ToDouble(value, 0); + } + /// /// Reads a two-byte signed integer from the current stream using the system's default endian encoding, and advances /// the stream position by two bytes. @@ -125,6 +187,30 @@ namespace X10D.StreamExtensions return BitConverter.ToInt64(value, 0); } + /// + /// Reads a single-precision floating point value from the current stream using the system's default endian encoding, + /// and advances the stream position by four bytes. + /// + /// The stream from which the value should be read. + /// A single-precision floating point value read from the stream. + public static double ReadSingle(this Stream stream) + { + return stream.ReadSingle(DefaultEndianness); + } + + /// + /// Reads a double-precision floating point value from the current stream using a specified endian encoding, and + /// advances the stream position by four bytes. + /// + /// The stream from which the value should be read. + /// The endian encoding to use. + /// A single-precision floating point value read from the stream. + public static double ReadSingle(this Stream stream, Endianness endianness) + { + var value = ReadInternal(stream, endianness); + return BitConverter.ToSingle(value, 0); + } + /// /// Reads a two-byte unsigned integer from the current stream using the system's default endian encoding, and advances /// the stream position by two bytes.