LittleEndianReader.cs 2.9 KB

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