EndianBinaryWriter.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 BinaryWriter to support BigEndian destinations.
  27. /// When writing multibyte values, the bytes are reordered appropriately.
  28. /// </summary>
  29. public class EndianBinaryWriter : BinaryWriter
  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 EndianBinaryWriter(Stream output, Encoding encoding, bool isBigEndian)
  43. : base(output, encoding)
  44. {
  45. this.isBigEndian = isBigEndian;
  46. }
  47. public EndianBinaryWriter(Stream output, bool isBigEndian)
  48. : this(output, Encoding.UTF8, isBigEndian)
  49. {
  50. }
  51. #endregion
  52. #region Methods
  53. public override void Write(short value)
  54. {
  55. if (!IsBigEndian)
  56. {
  57. base.Write(value);
  58. return;
  59. }
  60. byte[] buffer = BitConverter.GetBytes(value);
  61. Array.Reverse(buffer);
  62. base.Write(buffer, 0, 2);
  63. }
  64. public override void Write(ushort value)
  65. {
  66. if (!IsBigEndian)
  67. {
  68. base.Write(value);
  69. return;
  70. }
  71. byte[] buffer = BitConverter.GetBytes(value);
  72. Array.Reverse(buffer);
  73. base.Write(buffer, 0, 2);
  74. }
  75. public override void Write(int value)
  76. {
  77. if (!IsBigEndian)
  78. {
  79. base.Write(value);
  80. return;
  81. }
  82. byte[] buffer = BitConverter.GetBytes(value);
  83. Array.Reverse(buffer);
  84. base.Write(buffer, 0, 4);
  85. }
  86. public override void Write(uint value)
  87. {
  88. if (!IsBigEndian)
  89. {
  90. base.Write(value);
  91. return;
  92. }
  93. byte[] buffer = BitConverter.GetBytes(value);
  94. Array.Reverse(buffer);
  95. base.Write(buffer, 0, 4);
  96. }
  97. public override void Write(long value)
  98. {
  99. if (!IsBigEndian)
  100. {
  101. base.Write(value);
  102. return;
  103. }
  104. byte[] buffer = BitConverter.GetBytes(value);
  105. Array.Reverse(buffer);
  106. base.Write(buffer, 0, 8);
  107. }
  108. public override void Write(ulong value)
  109. {
  110. if (!IsBigEndian)
  111. {
  112. base.Write(value);
  113. return;
  114. }
  115. byte[] buffer = BitConverter.GetBytes(value);
  116. Array.Reverse(buffer);
  117. base.Write(buffer, 0, 8);
  118. }
  119. public override void Write(float value)
  120. {
  121. if (!IsBigEndian)
  122. {
  123. base.Write(value);
  124. return;
  125. }
  126. byte[] buffer = BitConverter.GetBytes(value);
  127. Array.Reverse(buffer);
  128. base.Write(buffer, 0, 4);
  129. }
  130. public override void Write(double value)
  131. {
  132. if (!IsBigEndian)
  133. {
  134. base.Write(value);
  135. return;
  136. }
  137. byte[] buffer = BitConverter.GetBytes(value);
  138. Array.Reverse(buffer);
  139. base.Write(buffer, 0, 8);
  140. }
  141. #endregion
  142. }
  143. } // namespace