ByteDataArray.cs 4.6 KB

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