FastBinaryReader.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using System.Text;
  3. namespace InABox.Core
  4. {
  5. public class FastBinaryReader
  6. {
  7. private readonly byte[] _data;
  8. private int index = -1;
  9. public FastBinaryReader(byte[] data)
  10. {
  11. _data = data;
  12. index = 0;
  13. }
  14. public bool ReadBoolean()
  15. {
  16. var result = _data[index] != 0;
  17. index++;
  18. return result;
  19. }
  20. public byte ReadByte()
  21. {
  22. var result = _data[index];
  23. index++;
  24. return result;
  25. }
  26. public short ReadInt16()
  27. {
  28. var result = BitConverter.ToInt16(_data, index);
  29. index += 2;
  30. return result;
  31. }
  32. public int ReadInt32()
  33. {
  34. var result = BitConverter.ToInt32(_data, index);
  35. index += 4;
  36. return result;
  37. }
  38. public long ReadInt64()
  39. {
  40. var result = BitConverter.ToInt64(_data, index);
  41. index += 8;
  42. return result;
  43. }
  44. public double ReadFloat()
  45. {
  46. var result = BitConverter.ToSingle(_data, index);
  47. index += 4;
  48. return result;
  49. }
  50. public double ReadDouble()
  51. {
  52. var result = BitConverter.ToDouble(_data, index);
  53. index += 8;
  54. return result;
  55. }
  56. protected internal int Read7BitEncodedInt()
  57. {
  58. // Read out an Int32 7 bits at a time. The high bit
  59. // of the byte when on means to continue reading more bytes.
  60. var count = 0;
  61. var shift = 0;
  62. byte b;
  63. do
  64. {
  65. // Check for a corrupted stream. Read a max of 5 bytes.
  66. // In a future version, add a DataFormatException.
  67. if (shift == 5 * 7) // 5 bytes max per Int32, shift += 7
  68. throw new FormatException("Format_Bad7BitInt32");
  69. // ReadByte handles end of stream cases for us.
  70. b = ReadByte();
  71. count |= (b & 0x7F) << shift;
  72. shift += 7;
  73. } while ((b & 0x80) != 0);
  74. return count;
  75. }
  76. public string ReadString()
  77. {
  78. var length = Read7BitEncodedInt();
  79. var result = Encoding.UTF8.GetString(_data, index, length);
  80. index += length;
  81. return result;
  82. }
  83. public DateTime ReadDateTime()
  84. {
  85. var result = ReadInt64();
  86. return DateTime.FromBinary(result);
  87. }
  88. public TimeSpan ReadTimeSpan()
  89. {
  90. var result = ReadInt64();
  91. return new TimeSpan(result);
  92. }
  93. public Guid ReadGuid()
  94. {
  95. var buf = new byte[16];
  96. Buffer.BlockCopy(_data, index, buf, 0, 16);
  97. var result = new Guid(buf);
  98. index += 16;
  99. return result;
  100. }
  101. public byte[] ReadBytes(int length)
  102. {
  103. var result = new byte[length];
  104. Buffer.BlockCopy(_data, index, result, 0, length);
  105. index += length;
  106. return result;
  107. }
  108. }
  109. }