BigEndianReader.cs 3.5 KB

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