BigEndianReader.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 int ReadInt32(byte[] buffer, ref int offset)
  19. {
  20. offset += 4;
  21. return BigEndianConverter.ToInt32(buffer, offset - 4);
  22. }
  23. public static uint ReadUInt32(byte[] buffer, ref int offset)
  24. {
  25. offset += 4;
  26. return BigEndianConverter.ToUInt32(buffer, offset - 4);
  27. }
  28. public static long ReadInt64(byte[] buffer, ref int offset)
  29. {
  30. offset += 8;
  31. return BigEndianConverter.ToInt64(buffer, offset - 8);
  32. }
  33. public static ulong ReadUInt64(byte[] buffer, ref int offset)
  34. {
  35. offset += 8;
  36. return BigEndianConverter.ToUInt64(buffer, offset - 8);
  37. }
  38. public static Guid ReadGuidBytes(byte[] buffer, ref int offset)
  39. {
  40. offset += 16;
  41. return BigEndianConverter.ToGuid(buffer, offset - 16);
  42. }
  43. public static short ReadInt16(Stream stream)
  44. {
  45. byte[] buffer = new byte[2];
  46. stream.Read(buffer, 0, 2);
  47. return BigEndianConverter.ToInt16(buffer, 0);
  48. }
  49. public static ushort ReadUInt16(Stream stream)
  50. {
  51. byte[] buffer = new byte[2];
  52. stream.Read(buffer, 0, 2);
  53. return BigEndianConverter.ToUInt16(buffer, 0);
  54. }
  55. public static int ReadInt32(Stream stream)
  56. {
  57. byte[] buffer = new byte[4];
  58. stream.Read(buffer, 0, 4);
  59. return BigEndianConverter.ToInt32(buffer, 0);
  60. }
  61. public static uint ReadUInt32(Stream stream)
  62. {
  63. byte[] buffer = new byte[4];
  64. stream.Read(buffer, 0, 4);
  65. return BigEndianConverter.ToUInt32(buffer, 0);
  66. }
  67. public static long ReadInt64(Stream stream)
  68. {
  69. byte[] buffer = new byte[8];
  70. stream.Read(buffer, 0, 8);
  71. return BigEndianConverter.ToInt64(buffer, 0);
  72. }
  73. public static ulong ReadUInt64(Stream stream)
  74. {
  75. byte[] buffer = new byte[8];
  76. stream.Read(buffer, 0, 8);
  77. return BigEndianConverter.ToUInt64(buffer, 0);
  78. }
  79. public static Guid ReadGuidBytes(Stream stream)
  80. {
  81. byte[] buffer = new byte[16];
  82. stream.Read(buffer, 0, 16);
  83. return BigEndianConverter.ToGuid(buffer, 0);
  84. }
  85. }
  86. }