Byte Buffer Reader


SUBMITTED BY: Guest

DATE: Aug. 1, 2014, 10:02 a.m.

FORMAT: C#

SIZE: 5.4 kB

HITS: 25020

  1. using System;
  2. using System.Text;
  3. namespace Gibbed.IO
  4. {
  5. public class ByteBufferReader
  6. {
  7. private readonly byte[] _Buffer;
  8. private readonly Endian _Endian;
  9. private readonly int _Offset;
  10. public ByteBufferReader(byte[] buffer)
  11. : this(buffer, 0, Endian.Little)
  12. {
  13. }
  14. public ByteBufferReader(byte[] buffer, int offset)
  15. : this(buffer, offset, Endian.Little)
  16. {
  17. }
  18. public ByteBufferReader(byte[] buffer, int offset, Endian endian)
  19. {
  20. if (buffer == null)
  21. {
  22. throw new ArgumentNullException("buffer");
  23. }
  24. if (offset < 0 || offset >= buffer.Length)
  25. {
  26. throw new ArgumentOutOfRangeException("offset");
  27. }
  28. this._Buffer = buffer;
  29. this._Offset = offset;
  30. this._Endian = endian;
  31. }
  32. public ByteBufferReader this[int index]
  33. {
  34. get
  35. {
  36. if (index == 0)
  37. {
  38. return this;
  39. }
  40. return new ByteBufferReader(this._Buffer, this._Offset + index, this._Endian);
  41. }
  42. }
  43. public bool ReadValueB8()
  44. {
  45. return this._Buffer[this._Offset] != 0;
  46. }
  47. public bool ReadValueB32()
  48. {
  49. // no need for swap
  50. return BitConverter.ToInt32(this._Buffer, this._Offset) != 0;
  51. }
  52. public sbyte ReadValueS8()
  53. {
  54. return (sbyte)this._Buffer[this._Offset];
  55. }
  56. public byte ReadValueU8()
  57. {
  58. return this._Buffer[this._Offset];
  59. }
  60. public short ReadValueS16()
  61. {
  62. var value = BitConverter.ToInt16(this._Buffer, this._Offset);
  63. if (StreamHelpers.ShouldSwap(this._Endian) == true)
  64. {
  65. value = value.Swap();
  66. }
  67. return value;
  68. }
  69. public ushort ReadValueU16()
  70. {
  71. var value = BitConverter.ToUInt16(this._Buffer, this._Offset);
  72. if (StreamHelpers.ShouldSwap(this._Endian) == true)
  73. {
  74. value = value.Swap();
  75. }
  76. return value;
  77. }
  78. public int ReadValueS32()
  79. {
  80. var value = BitConverter.ToInt32(this._Buffer, this._Offset);
  81. if (StreamHelpers.ShouldSwap(this._Endian) == true)
  82. {
  83. value = value.Swap();
  84. }
  85. return value;
  86. }
  87. public uint ReadValueU32()
  88. {
  89. var value = BitConverter.ToUInt32(this._Buffer, this._Offset);
  90. if (StreamHelpers.ShouldSwap(this._Endian) == true)
  91. {
  92. value = value.Swap();
  93. }
  94. return value;
  95. }
  96. public long ReadValueS64()
  97. {
  98. var value = BitConverter.ToInt64(this._Buffer, this._Offset);
  99. if (StreamHelpers.ShouldSwap(this._Endian) == true)
  100. {
  101. value = value.Swap();
  102. }
  103. return value;
  104. }
  105. public ulong ReadValueU64()
  106. {
  107. var value = BitConverter.ToUInt64(this._Buffer, this._Offset);
  108. if (StreamHelpers.ShouldSwap(this._Endian) == true)
  109. {
  110. value = value.Swap();
  111. }
  112. return value;
  113. }
  114. public float ReadValueF32()
  115. {
  116. if (StreamHelpers.ShouldSwap(this._Endian) == true)
  117. {
  118. return
  119. BitConverter.ToSingle(
  120. BitConverter.GetBytes(BitConverter.ToInt32(this._Buffer, this._Offset).Swap()), 0);
  121. }
  122. return BitConverter.ToSingle(this._Buffer, this._Offset);
  123. }
  124. public double ReadValueF64()
  125. {
  126. if (StreamHelpers.ShouldSwap(this._Endian) == true)
  127. {
  128. return BitConverter.Int64BitsToDouble(BitConverter.ToInt64(this._Buffer, this._Offset).Swap());
  129. }
  130. return BitConverter.ToDouble(this._Buffer, this._Offset);
  131. }
  132. public string ReadString(int size, bool trailingNull, Encoding encoding)
  133. {
  134. if (size < 0 || this._Offset + size > this._Buffer.Length)
  135. {
  136. throw new ArgumentOutOfRangeException("size");
  137. }
  138. string value = encoding.GetString(this._Buffer, this._Offset, size);
  139. if (trailingNull == true)
  140. {
  141. var position = value.IndexOf('\0');
  142. if (position >= 0)
  143. {
  144. value = value.Substring(0, position);
  145. }
  146. }
  147. return value;
  148. }
  149. }
  150. }

comments powered by Disqus