BigEndianReader.cs 2.9 KB

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