BigEndianReader.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. namespace Utilities
  5. {
  6. public class BigEndianReader
  7. {
  8. public static short ReadInt16(byte[] buffer, ref int offset)
  9. {
  10. offset += 2;
  11. return BigEndianConverter.ToInt16(buffer, offset - 2);
  12. }
  13. public static ushort ReadUInt16(byte[] buffer, ref int offset)
  14. {
  15. offset += 2;
  16. return BigEndianConverter.ToUInt16(buffer, offset - 2);
  17. }
  18. public static uint ReadUInt24(byte[] buffer, int offset)
  19. {
  20. return (uint)((buffer[offset + 0] << 16) | (buffer[offset + 1] << 8) | (buffer[offset + 2] << 0));
  21. }
  22. public static uint ReadUInt24(byte[] buffer, ref int offset)
  23. {
  24. offset += 3;
  25. return ReadUInt24(buffer, offset - 3);
  26. }
  27. public static int ReadInt32(byte[] buffer, ref int offset)
  28. {
  29. offset += 4;
  30. return BigEndianConverter.ToInt32(buffer, offset - 4);
  31. }
  32. public static uint ReadUInt32(byte[] buffer, ref int offset)
  33. {
  34. offset += 4;
  35. return BigEndianConverter.ToUInt32(buffer, offset - 4);
  36. }
  37. public static long ReadInt64(byte[] buffer, ref int offset)
  38. {
  39. offset += 8;
  40. return BigEndianConverter.ToInt64(buffer, offset - 8);
  41. }
  42. public static ulong ReadUInt64(byte[] buffer, ref int offset)
  43. {
  44. offset += 8;
  45. return BigEndianConverter.ToUInt64(buffer, offset - 8);
  46. }
  47. public static Guid ReadGuidBytes(byte[] buffer, ref int offset)
  48. {
  49. offset += 16;
  50. return BigEndianConverter.ToGuid(buffer, offset - 16);
  51. }
  52. public static short ReadInt16(Stream stream)
  53. {
  54. byte[] buffer = new byte[2];
  55. stream.Read(buffer, 0, 2);
  56. return BigEndianConverter.ToInt16(buffer, 0);
  57. }
  58. public static ushort ReadUInt16(Stream stream)
  59. {
  60. byte[] buffer = new byte[2];
  61. stream.Read(buffer, 0, 2);
  62. return BigEndianConverter.ToUInt16(buffer, 0);
  63. }
  64. public static uint ReadUInt24(Stream stream)
  65. {
  66. byte[] buffer = new byte[4];
  67. stream.Read(buffer, 1, 3);
  68. return BigEndianConverter.ToUInt32(buffer, 0);
  69. }
  70. public static int ReadInt32(Stream stream)
  71. {
  72. byte[] buffer = new byte[4];
  73. stream.Read(buffer, 0, 4);
  74. return BigEndianConverter.ToInt32(buffer, 0);
  75. }
  76. public static uint ReadUInt32(Stream stream)
  77. {
  78. byte[] buffer = new byte[4];
  79. stream.Read(buffer, 0, 4);
  80. return BigEndianConverter.ToUInt32(buffer, 0);
  81. }
  82. public static long ReadInt64(Stream stream)
  83. {
  84. byte[] buffer = new byte[8];
  85. stream.Read(buffer, 0, 8);
  86. return BigEndianConverter.ToInt64(buffer, 0);
  87. }
  88. public static ulong ReadUInt64(Stream stream)
  89. {
  90. byte[] buffer = new byte[8];
  91. stream.Read(buffer, 0, 8);
  92. return BigEndianConverter.ToUInt64(buffer, 0);
  93. }
  94. public static Guid ReadGuidBytes(Stream stream)
  95. {
  96. byte[] buffer = new byte[16];
  97. stream.Read(buffer, 0, 16);
  98. return BigEndianConverter.ToGuid(buffer, 0);
  99. }
  100. }
  101. }