BinaryReaders.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO
  6. {
  7. public static class BinaryReaders
  8. {
  9. public static byte[] ReadBytesFully(BinaryReader binaryReader, int count)
  10. {
  11. byte[] bytes = binaryReader.ReadBytes(count);
  12. if (bytes == null || bytes.Length != count)
  13. throw new EndOfStreamException();
  14. return bytes;
  15. }
  16. public static short ReadInt16BigEndian(BinaryReader binaryReader)
  17. {
  18. short n = binaryReader.ReadInt16();
  19. return BitConverter.IsLittleEndian ? Shorts.ReverseBytes(n) : n;
  20. }
  21. public static short ReadInt16LittleEndian(BinaryReader binaryReader)
  22. {
  23. short n = binaryReader.ReadInt16();
  24. return BitConverter.IsLittleEndian ? n : Shorts.ReverseBytes(n);
  25. }
  26. public static int ReadInt32BigEndian(BinaryReader binaryReader)
  27. {
  28. int n = binaryReader.ReadInt32();
  29. return BitConverter.IsLittleEndian ? Integers.ReverseBytes(n) : n;
  30. }
  31. public static int ReadInt32LittleEndian(BinaryReader binaryReader)
  32. {
  33. int n = binaryReader.ReadInt32();
  34. return BitConverter.IsLittleEndian ? n : Integers.ReverseBytes(n);
  35. }
  36. public static long ReadInt64BigEndian(BinaryReader binaryReader)
  37. {
  38. long n = binaryReader.ReadInt64();
  39. return BitConverter.IsLittleEndian ? Longs.ReverseBytes(n) : n;
  40. }
  41. public static long ReadInt64LittleEndian(BinaryReader binaryReader)
  42. {
  43. long n = binaryReader.ReadInt64();
  44. return BitConverter.IsLittleEndian ? n : Longs.ReverseBytes(n);
  45. }
  46. [CLSCompliant(false)]
  47. public static ushort ReadUInt16BigEndian(BinaryReader binaryReader)
  48. {
  49. ushort n = binaryReader.ReadUInt16();
  50. return BitConverter.IsLittleEndian ? Shorts.ReverseBytes(n) : n;
  51. }
  52. [CLSCompliant(false)]
  53. public static ushort ReadUInt16LittleEndian(BinaryReader binaryReader)
  54. {
  55. ushort n = binaryReader.ReadUInt16();
  56. return BitConverter.IsLittleEndian ? n : Shorts.ReverseBytes(n);
  57. }
  58. [CLSCompliant(false)]
  59. public static uint ReadUInt32BigEndian(BinaryReader binaryReader)
  60. {
  61. uint n = binaryReader.ReadUInt32();
  62. return BitConverter.IsLittleEndian ? Integers.ReverseBytes(n) : n;
  63. }
  64. [CLSCompliant(false)]
  65. public static uint ReadUInt32LittleEndian(BinaryReader binaryReader)
  66. {
  67. uint n = binaryReader.ReadUInt32();
  68. return BitConverter.IsLittleEndian ? n : Integers.ReverseBytes(n);
  69. }
  70. [CLSCompliant(false)]
  71. public static ulong ReadUInt64BigEndian(BinaryReader binaryReader)
  72. {
  73. ulong n = binaryReader.ReadUInt64();
  74. return BitConverter.IsLittleEndian ? Longs.ReverseBytes(n) : n;
  75. }
  76. [CLSCompliant(false)]
  77. public static ulong ReadUInt64LittleEndian(BinaryReader binaryReader)
  78. {
  79. ulong n = binaryReader.ReadUInt64();
  80. return BitConverter.IsLittleEndian ? n : Longs.ReverseBytes(n);
  81. }
  82. }
  83. }
  84. #pragma warning restore
  85. #endif