BufferWriter.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Diagnostics;
  6. using System.IO;
  7. namespace YooAsset
  8. {
  9. /// <summary>
  10. /// 数据存储以小端字节序为标准
  11. /// </summary>
  12. internal class BufferWriter
  13. {
  14. private readonly byte[] _buffer;
  15. private int _index = 0;
  16. public BufferWriter(int capacity)
  17. {
  18. _buffer = new byte[capacity];
  19. }
  20. /// <summary>
  21. /// 缓冲区容量
  22. /// </summary>
  23. public int Capacity
  24. {
  25. get { return _buffer.Length; }
  26. }
  27. /// <summary>
  28. /// 清空缓冲区
  29. /// </summary>
  30. public void Clear()
  31. {
  32. _index = 0;
  33. }
  34. /// <summary>
  35. /// 将有效数据写入文件流
  36. /// </summary>
  37. public void WriteToStream(FileStream fileStream)
  38. {
  39. fileStream.Write(_buffer, 0, _index);
  40. }
  41. public void WriteBytes(byte[] data)
  42. {
  43. int count = data.Length;
  44. CheckWriterIndex(count);
  45. Buffer.BlockCopy(data, 0, _buffer, _index, count);
  46. _index += count;
  47. }
  48. public void WriteByte(byte value)
  49. {
  50. CheckWriterIndex(1);
  51. _buffer[_index++] = value;
  52. }
  53. public void WriteBool(bool value)
  54. {
  55. WriteByte((byte)(value ? 1 : 0));
  56. }
  57. public void WriteInt16(short value)
  58. {
  59. WriteUInt16((ushort)value);
  60. }
  61. public void WriteUInt16(ushort value)
  62. {
  63. CheckWriterIndex(2);
  64. _buffer[_index++] = (byte)value;
  65. _buffer[_index++] = (byte)(value >> 8);
  66. }
  67. public void WriteInt32(int value)
  68. {
  69. WriteUInt32((uint)value);
  70. }
  71. public void WriteUInt32(uint value)
  72. {
  73. CheckWriterIndex(4);
  74. _buffer[_index++] = (byte)value;
  75. _buffer[_index++] = (byte)(value >> 8);
  76. _buffer[_index++] = (byte)(value >> 16);
  77. _buffer[_index++] = (byte)(value >> 24);
  78. }
  79. public void WriteInt64(long value)
  80. {
  81. WriteUInt64((ulong)value);
  82. }
  83. public void WriteUInt64(ulong value)
  84. {
  85. CheckWriterIndex(8);
  86. _buffer[_index++] = (byte)value;
  87. _buffer[_index++] = (byte)(value >> 8);
  88. _buffer[_index++] = (byte)(value >> 16);
  89. _buffer[_index++] = (byte)(value >> 24);
  90. _buffer[_index++] = (byte)(value >> 32);
  91. _buffer[_index++] = (byte)(value >> 40);
  92. _buffer[_index++] = (byte)(value >> 48);
  93. _buffer[_index++] = (byte)(value >> 56);
  94. }
  95. public void WriteUTF8(string value)
  96. {
  97. if (string.IsNullOrEmpty(value))
  98. {
  99. WriteUInt16(0);
  100. }
  101. else
  102. {
  103. byte[] bytes = Encoding.UTF8.GetBytes(value);
  104. int count = bytes.Length;
  105. if (count > ushort.MaxValue)
  106. throw new FormatException($"Write string length cannot be greater than {ushort.MaxValue} !");
  107. WriteUInt16(Convert.ToUInt16(count));
  108. WriteBytes(bytes);
  109. }
  110. }
  111. public void WriteInt32Array(int[] values)
  112. {
  113. if (values == null)
  114. {
  115. WriteUInt16(0);
  116. }
  117. else
  118. {
  119. int count = values.Length;
  120. if (count > ushort.MaxValue)
  121. throw new FormatException($"Write array length cannot be greater than {ushort.MaxValue} !");
  122. WriteUInt16(Convert.ToUInt16(count));
  123. for (int i = 0; i < count; i++)
  124. {
  125. WriteInt32(values[i]);
  126. }
  127. }
  128. }
  129. public void WriteInt64Array(long[] values)
  130. {
  131. if (values == null)
  132. {
  133. WriteUInt16(0);
  134. }
  135. else
  136. {
  137. int count = values.Length;
  138. if (count > ushort.MaxValue)
  139. throw new FormatException($"Write array length cannot be greater than {ushort.MaxValue} !");
  140. WriteUInt16(Convert.ToUInt16(count));
  141. for (int i = 0; i < count; i++)
  142. {
  143. WriteInt64(values[i]);
  144. }
  145. }
  146. }
  147. public void WriteUTF8Array(string[] values)
  148. {
  149. if (values == null)
  150. {
  151. WriteUInt16(0);
  152. }
  153. else
  154. {
  155. int count = values.Length;
  156. if (count > ushort.MaxValue)
  157. throw new FormatException($"Write array length cannot be greater than {ushort.MaxValue} !");
  158. WriteUInt16(Convert.ToUInt16(count));
  159. for (int i = 0; i < count; i++)
  160. {
  161. WriteUTF8(values[i]);
  162. }
  163. }
  164. }
  165. [Conditional("DEBUG")]
  166. private void CheckWriterIndex(int length)
  167. {
  168. if (_index + length > Capacity)
  169. {
  170. throw new IndexOutOfRangeException();
  171. }
  172. }
  173. }
  174. }