CByteArray.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.IO.Compression;
  7. namespace Engine.CLoader
  8. {
  9. /// <summary>配置文件字节流解析对象</summary>
  10. public class CByteArray
  11. {
  12. public const int BYTE_LEN = 1;
  13. public const int SHORT16_LEN = 2;
  14. public const int INT32_LEN = 4;
  15. public const int FLOAT_LEN = 4;
  16. public const int LONG_LEN = 8;
  17. public const int DOUBLE_LEN = 8;
  18. private byte[] data;
  19. private int byteIndex = 0;
  20. public CByteArray(byte[] bytes = null)
  21. {
  22. data = (bytes == null) ? new byte[0] : bytes;
  23. }
  24. /// <summary>byte[] 数据原</summary>
  25. public byte[] Data
  26. {
  27. get { return data; }
  28. set { data = value; }
  29. }
  30. /// <summary>可读字节数,修改读取位置可修改此值</summary>
  31. public int ByteLength
  32. {
  33. get { return data.Length - byteIndex; }
  34. }
  35. /// <summary>总字节数据</summary>
  36. public int Length
  37. {
  38. get { return data.Length; }
  39. }
  40. /// <summary>当前读取位置</summary>
  41. public int Position
  42. {
  43. get { return byteIndex; }
  44. set { byteIndex = value; }
  45. }
  46. public byte ReadByte()
  47. {
  48. return this.ReadBytes(byteIndex)[0];
  49. }
  50. public bool ReadBool()
  51. {
  52. byte bt = this.ReadByte();
  53. return (bt == (byte)1) ? true : false;
  54. }
  55. public short ReadShort()
  56. {
  57. return BitConverter.ToInt16(this.ReadBytes(SHORT16_LEN), 0);
  58. }
  59. public int ReadInt()
  60. {
  61. return BitConverter.ToInt32(this.ReadBytes(INT32_LEN), 0);
  62. }
  63. public long ReadLong()
  64. {
  65. return BitConverter.ToInt64(this.ReadBytes(LONG_LEN), 0);
  66. }
  67. public float ReadFloat()
  68. {
  69. return BitConverter.ToSingle(this.ReadBytes(FLOAT_LEN), 0);
  70. }
  71. public double ReadDouble()
  72. {
  73. return BitConverter.ToDouble(this.ReadBytes(DOUBLE_LEN), 0);
  74. }
  75. public string ReadString()
  76. {
  77. return Encoding.UTF8.GetString(this.ReadBytes(this.ReadShort()));
  78. }
  79. public CByteArray ReadUnit(int leng)
  80. {
  81. return new CByteArray(this.ReadBytes(leng));
  82. }
  83. public byte[] ReadBytes(int leng)
  84. {
  85. if (leng > this.ByteLength)
  86. {
  87. throw new Exception("超出字节流长度");
  88. }
  89. //这里重复开辟了内存,会产生内存垃圾,可能会有性能问题
  90. byte[] byets = new byte[leng];
  91. for (int i = 0; i < leng; i++)
  92. {
  93. byets[i] = data[byteIndex];
  94. byteIndex++;
  95. }
  96. return byets;
  97. }
  98. public void WriteByte(byte by)
  99. {
  100. byte[] bytes = new byte[BYTE_LEN];
  101. bytes[0] = by;
  102. this.WriteBytes(bytes);
  103. }
  104. public void WriteBool(bool flag)
  105. {
  106. byte b = (byte)0;
  107. if (flag) b = (byte)1;
  108. this.WriteByte(b);
  109. }
  110. public void WriteShort(short value)
  111. {
  112. byte[] bytes = BitConverter.GetBytes(value);
  113. this.WriteBytes(bytes);
  114. }
  115. public void WriteInt(int value)
  116. {
  117. byte[] bytes = BitConverter.GetBytes(value);
  118. this.WriteBytes(bytes);
  119. }
  120. public void WriteLong(int value)
  121. {
  122. byte[] bytes = BitConverter.GetBytes(value);
  123. this.WriteBytes(bytes);
  124. }
  125. public void WriteDouble(int value)
  126. {
  127. byte[] bytes = BitConverter.GetBytes(value);
  128. this.WriteBytes(bytes);
  129. }
  130. public void WriteFloat(int value)
  131. {
  132. byte[] bytes = BitConverter.GetBytes(value);
  133. this.WriteBytes(bytes);
  134. }
  135. public void WriteString(string str)
  136. {
  137. short len = (short)Encoding.UTF8.GetByteCount(str);
  138. this.WriteShort(len);
  139. byte[] bytes = new byte[len];
  140. Encoding.UTF8.GetBytes(str, 0, str.Length, bytes, 0);
  141. this.WriteBytes(bytes);
  142. }
  143. public void WriteUnit(byte[] bytes)
  144. {
  145. this.WriteInt(bytes.Length);
  146. this.WriteBytes(bytes);
  147. }
  148. public void WriteBytes(byte[] bytes)
  149. {
  150. byteIndex = data.Length;
  151. int leng = bytes.Length;
  152. //这里重复开辟了内存,会产生内存垃圾,可能会有性能问题
  153. byte[] newData = new byte[byteIndex + leng];
  154. data.CopyTo(newData, 0);
  155. Array.Copy(bytes, 0, newData, byteIndex, leng);
  156. data = newData;
  157. byteIndex = data.Length;
  158. }
  159. public void Dispose()
  160. {
  161. data = null;
  162. }
  163. /// <summary>压缩字节流 zip压缩</summary>
  164. public void Compress()
  165. {
  166. }
  167. /// <summary>解压缩字节流</summary>
  168. public void Decompress()
  169. {
  170. }
  171. }
  172. }