1
0

EndianBinaryReader.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #region Copyright
  2. ////////////////////////////////////////////////////////////////////////////////
  3. // The following FIT Protocol software provided may be used with FIT protocol
  4. // devices only and remains the copyrighted property of Dynastream Innovations Inc.
  5. // The software is being provided on an "as-is" basis and as an accommodation,
  6. // and therefore all warranties, representations, or guarantees of any kind
  7. // (whether express, implied or statutory) including, without limitation,
  8. // warranties of merchantability, non-infringement, or fitness for a particular
  9. // purpose, are specifically disclaimed.
  10. //
  11. // Copyright 2016 Dynastream Innovations Inc.
  12. ////////////////////////////////////////////////////////////////////////////////
  13. // ****WARNING**** This file is auto-generated! Do NOT edit this file.
  14. // Profile Version = 16.60Release
  15. // Tag = production-akw-16.60.00-0-g5d3d436
  16. ////////////////////////////////////////////////////////////////////////////////
  17. #endregion
  18. using System;
  19. using System.Collections.Generic;
  20. using System.Diagnostics;
  21. using System.Text;
  22. using System.IO;
  23. namespace Dynastream.Utility
  24. {
  25. /// <summary>
  26. /// Extend framework BinaryReader to support BigEndian datasources.
  27. /// When reading multibyte values, the bytes are reordered appropriately.
  28. /// </summary>
  29. public class EndianBinaryReader : BinaryReader
  30. {
  31. #region Fields
  32. private bool isBigEndian = false;
  33. #endregion
  34. #region Properties
  35. public bool IsBigEndian
  36. {
  37. get { return isBigEndian; }
  38. set { isBigEndian = value; }
  39. }
  40. #endregion
  41. #region Constructors
  42. public EndianBinaryReader(Stream input, Encoding encoding, bool isBigEndian)
  43. : base(input, encoding)
  44. {
  45. this.isBigEndian = isBigEndian;
  46. }
  47. public EndianBinaryReader(Stream input, bool isBigEndian)
  48. : this(input, Encoding.UTF8, isBigEndian)
  49. {
  50. }
  51. #endregion
  52. #region Methods
  53. public override short ReadInt16()
  54. {
  55. if (!IsBigEndian)
  56. {
  57. return base.ReadInt16();
  58. }
  59. byte[] buffer = new byte[2];
  60. Read(buffer, 0, 2);
  61. Array.Reverse(buffer);
  62. return BitConverter.ToInt16(buffer, 0);
  63. }
  64. public override ushort ReadUInt16()
  65. {
  66. if (!IsBigEndian)
  67. {
  68. return base.ReadUInt16();
  69. }
  70. byte[] buffer = new byte[2];
  71. Read(buffer, 0, 2);
  72. Array.Reverse(buffer);
  73. return BitConverter.ToUInt16(buffer, 0);
  74. }
  75. public override int ReadInt32()
  76. {
  77. if (!IsBigEndian)
  78. {
  79. return base.ReadInt32();
  80. }
  81. byte[] buffer = new byte[4];
  82. Read(buffer, 0, 4);
  83. Array.Reverse(buffer);
  84. return BitConverter.ToInt32(buffer, 0);
  85. }
  86. public override uint ReadUInt32()
  87. {
  88. if (!IsBigEndian)
  89. {
  90. return base.ReadUInt32();
  91. }
  92. byte[] buffer = new byte[4];
  93. Read(buffer, 0, 4);
  94. Array.Reverse(buffer);
  95. return BitConverter.ToUInt32(buffer, 0);
  96. }
  97. public override long ReadInt64()
  98. {
  99. if (!IsBigEndian)
  100. {
  101. return base.ReadInt64();
  102. }
  103. byte[] buffer = new byte[8];
  104. Read(buffer, 0, 8);
  105. Array.Reverse(buffer);
  106. return BitConverter.ToInt64(buffer, 0);
  107. }
  108. public override ulong ReadUInt64()
  109. {
  110. if (!IsBigEndian)
  111. {
  112. return base.ReadUInt64();
  113. }
  114. byte[] buffer = new byte[8];
  115. Read(buffer, 0, 8);
  116. Array.Reverse(buffer);
  117. return BitConverter.ToUInt64(buffer, 0);
  118. }
  119. public override float ReadSingle()
  120. {
  121. if (!IsBigEndian)
  122. {
  123. return base.ReadSingle();
  124. }
  125. byte[] buffer = new byte[4];
  126. Read(buffer, 0, 4);
  127. Array.Reverse(buffer);
  128. return BitConverter.ToSingle(buffer, 0);
  129. }
  130. public override double ReadDouble()
  131. {
  132. if (!IsBigEndian)
  133. {
  134. return base.ReadDouble();
  135. }
  136. byte[] buffer = new byte[8];
  137. Read(buffer, 0, 8);
  138. Array.Reverse(buffer);
  139. return BitConverter.ToDouble(buffer, 0);
  140. }
  141. #endregion
  142. }
  143. } // namespace