BufferReader.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Diagnostics;
  6. namespace YooAsset
  7. {
  8. internal class BufferReader
  9. {
  10. private readonly byte[] _buffer;
  11. private int _index = 0;
  12. public BufferReader(byte[] data)
  13. {
  14. _buffer = data;
  15. }
  16. /// <summary>
  17. /// 是否有效
  18. /// </summary>
  19. public bool IsValid
  20. {
  21. get
  22. {
  23. if (_buffer == null || _buffer.Length == 0)
  24. return false;
  25. else
  26. return true;
  27. }
  28. }
  29. /// <summary>
  30. /// 缓冲区容量
  31. /// </summary>
  32. public int Capacity
  33. {
  34. get { return _buffer.Length; }
  35. }
  36. public byte[] ReadBytes(int count)
  37. {
  38. CheckReaderIndex(count);
  39. var data = new byte[count];
  40. Buffer.BlockCopy(_buffer, _index, data, 0, count);
  41. _index += count;
  42. return data;
  43. }
  44. public byte ReadByte()
  45. {
  46. CheckReaderIndex(1);
  47. return _buffer[_index++];
  48. }
  49. public bool ReadBool()
  50. {
  51. CheckReaderIndex(1);
  52. return _buffer[_index++] == 1;
  53. }
  54. public short ReadInt16()
  55. {
  56. CheckReaderIndex(2);
  57. if (BitConverter.IsLittleEndian)
  58. {
  59. short value = (short)((_buffer[_index]) | (_buffer[_index + 1] << 8));
  60. _index += 2;
  61. return value;
  62. }
  63. else
  64. {
  65. short value = (short)((_buffer[_index] << 8) | (_buffer[_index + 1]));
  66. _index += 2;
  67. return value;
  68. }
  69. }
  70. public ushort ReadUInt16()
  71. {
  72. return (ushort)ReadInt16();
  73. }
  74. public int ReadInt32()
  75. {
  76. CheckReaderIndex(4);
  77. if (BitConverter.IsLittleEndian)
  78. {
  79. int value = (_buffer[_index]) | (_buffer[_index + 1] << 8) | (_buffer[_index + 2] << 16) | (_buffer[_index + 3] << 24);
  80. _index += 4;
  81. return value;
  82. }
  83. else
  84. {
  85. int value = (_buffer[_index] << 24) | (_buffer[_index + 1] << 16) | (_buffer[_index + 2] << 8) | (_buffer[_index + 3]);
  86. _index += 4;
  87. return value;
  88. }
  89. }
  90. public uint ReadUInt32()
  91. {
  92. return (uint)ReadInt32();
  93. }
  94. public long ReadInt64()
  95. {
  96. CheckReaderIndex(8);
  97. if (BitConverter.IsLittleEndian)
  98. {
  99. int i1 = (_buffer[_index]) | (_buffer[_index + 1] << 8) | (_buffer[_index + 2] << 16) | (_buffer[_index + 3] << 24);
  100. int i2 = (_buffer[_index + 4]) | (_buffer[_index + 5] << 8) | (_buffer[_index + 6] << 16) | (_buffer[_index + 7] << 24);
  101. _index += 8;
  102. return (uint)i1 | ((long)i2 << 32);
  103. }
  104. else
  105. {
  106. int i1 = (_buffer[_index] << 24) | (_buffer[_index + 1] << 16) | (_buffer[_index + 2] << 8) | (_buffer[_index + 3]);
  107. int i2 = (_buffer[_index + 4] << 24) | (_buffer[_index + 5] << 16) | (_buffer[_index + 6] << 8) | (_buffer[_index + 7]);
  108. _index += 8;
  109. return (uint)i2 | ((long)i1 << 32);
  110. }
  111. }
  112. public ulong ReadUInt64()
  113. {
  114. return (ulong)ReadInt64();
  115. }
  116. public string ReadUTF8()
  117. {
  118. ushort count = ReadUInt16();
  119. if (count == 0)
  120. return string.Empty;
  121. CheckReaderIndex(count);
  122. string value = Encoding.UTF8.GetString(_buffer, _index, count);
  123. _index += count;
  124. return value;
  125. }
  126. public int[] ReadInt32Array()
  127. {
  128. ushort count = ReadUInt16();
  129. int[] values = new int[count];
  130. for (int i = 0; i < count; i++)
  131. {
  132. values[i] = ReadInt32();
  133. }
  134. return values;
  135. }
  136. public long[] ReadInt64Array()
  137. {
  138. ushort count = ReadUInt16();
  139. long[] values = new long[count];
  140. for (int i = 0; i < count; i++)
  141. {
  142. values[i] = ReadInt64();
  143. }
  144. return values;
  145. }
  146. public string[] ReadUTF8Array()
  147. {
  148. ushort count = ReadUInt16();
  149. string[] values = new string[count];
  150. for (int i = 0; i < count; i++)
  151. {
  152. values[i] = ReadUTF8();
  153. }
  154. return values;
  155. }
  156. [Conditional("DEBUG")]
  157. private void CheckReaderIndex(int length)
  158. {
  159. if (_index + length > Capacity)
  160. {
  161. throw new IndexOutOfRangeException();
  162. }
  163. }
  164. }
  165. }