DataStorage.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. using Island.StandardLib.Exceptions;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace Island.StandardLib.Storage
  6. {
  7. /// <summary>
  8. /// 表示一个提供读写操作的序列化数据容器
  9. /// </summary>
  10. public class DataStorage
  11. {
  12. List<byte> Data;
  13. public int Position { get; private set; }
  14. public DataStorage()
  15. {
  16. Data = new List<byte>();
  17. }
  18. public DataStorage(byte[] data)
  19. {
  20. Data = new List<byte>(data);
  21. }
  22. public byte[] Bytes => Data.ToArray();
  23. public int Size => Data.Count;
  24. public bool IsReachedEnd => Position >= Size;
  25. public void ReadInternal(byte[] data, int size)
  26. {
  27. for (int i = 0; i < size; i++)
  28. data[i] = Data[i + Position];
  29. Position += size;
  30. }
  31. public void WriteInternal(byte[] data)
  32. {
  33. for (int i = 0; i < data.Length; i++)
  34. Data.Add(data[i]);
  35. }
  36. void WriteInternal(List<byte> data)
  37. {
  38. for (int i = 0; i < data.Count; i++)
  39. Data.Add(data[i]);
  40. }
  41. public byte[] Read()
  42. {
  43. byte[] buf_size = new byte[4];
  44. ReadInternal(buf_size, 4);
  45. int size = BitConverter.ToInt32(buf_size, 0);
  46. byte[] buff = new byte[size];
  47. ReadInternal(buff, size);
  48. return buff;
  49. }
  50. public void Write<T>(T value)
  51. where T : IStorable
  52. {
  53. DataStorage typeInstance = new DataStorage();
  54. value.WriteToData(typeInstance);
  55. int size = typeInstance.Size;
  56. WriteInternal(BitConverter.GetBytes(size));
  57. WriteInternal(typeInstance.Data);
  58. }
  59. public void Write(byte[] bytes)
  60. {
  61. WriteInternal(BitConverter.GetBytes(bytes.Length));
  62. WriteInternal(bytes);
  63. }
  64. public void Write(int value)
  65. {
  66. WriteInternal(new byte[4] { 4, 0, 0, 0 });
  67. WriteInternal(BitConverter.GetBytes(value));
  68. }
  69. public void Write(uint value)
  70. {
  71. WriteInternal(new byte[4] { 4, 0, 0, 0 });
  72. WriteInternal(BitConverter.GetBytes(value));
  73. }
  74. public void Write(long value)
  75. {
  76. WriteInternal(new byte[4] { 8, 0, 0, 0 });
  77. WriteInternal(BitConverter.GetBytes(value));
  78. }
  79. public void Write(char value)
  80. {
  81. WriteInternal(new byte[4] { 2, 0, 0, 0 });
  82. WriteInternal(BitConverter.GetBytes(value));
  83. }
  84. public void Write(bool value)
  85. {
  86. WriteInternal(new byte[4] { 1, 0, 0, 0 });
  87. WriteInternal(BitConverter.GetBytes(value));
  88. }
  89. public void Write(float value)
  90. {
  91. WriteInternal(new byte[4] { 4, 0, 0, 0 });
  92. WriteInternal(BitConverter.GetBytes(value));
  93. }
  94. public void Write(double value)
  95. {
  96. WriteInternal(new byte[4] { 8, 0, 0, 0 });
  97. WriteInternal(BitConverter.GetBytes(value));
  98. }
  99. public void Write(string value)
  100. {
  101. if (value == null)
  102. value = "";
  103. byte[] data = Encoding.UTF8.GetBytes(value);
  104. WriteInternal(BitConverter.GetBytes(data.Length));
  105. WriteInternal(data);
  106. }
  107. public void WriteNullable<T>(T value) where T : class, IStorable, new()
  108. {
  109. WriteUncheck(value != null);
  110. if (value != null) Write(value);
  111. }
  112. public void ReadNullable<T>(out T value) where T : class, IStorable, new()
  113. {
  114. ReadUncheck(out bool hasValue);
  115. if (hasValue) Read(out value);
  116. else value = null;
  117. }
  118. public void WriteUncheck(byte value)
  119. {
  120. Data.Add(value);
  121. }
  122. public void WriteUncheck(int value)
  123. {
  124. WriteInternal(BitConverter.GetBytes(value));
  125. }
  126. public void WriteUncheck(uint value)
  127. {
  128. WriteInternal(BitConverter.GetBytes(value));
  129. }
  130. public void WriteUncheck(long value)
  131. {
  132. WriteInternal(BitConverter.GetBytes(value));
  133. }
  134. public void WriteUncheck(ulong value)
  135. {
  136. WriteInternal(BitConverter.GetBytes(value));
  137. }
  138. public void WriteUncheck(char value)
  139. {
  140. WriteInternal(BitConverter.GetBytes(value));
  141. }
  142. public void WriteUncheck(bool value)
  143. {
  144. WriteInternal(BitConverter.GetBytes(value));
  145. }
  146. public void WriteUncheck(float value)
  147. {
  148. WriteInternal(BitConverter.GetBytes(value));
  149. }
  150. public void WriteUncheck(double value)
  151. {
  152. WriteInternal(BitConverter.GetBytes(value));
  153. }
  154. public void WriteAuto(object value)
  155. {
  156. if (value is IStorable)
  157. Write((IStorable)value);
  158. else if (value is int)
  159. Write((int)value);
  160. else if (value is uint)
  161. Write((uint)value);
  162. else if (value is long)
  163. Write((long)value);
  164. else if (value is char)
  165. Write((char)value);
  166. else if (value is bool)
  167. Write((bool)value);
  168. else if (value is float)
  169. Write((float)value);
  170. else if (value is string)
  171. Write((string)value);
  172. else throw new DataStorageAutoException(DataStorageAutoException.Operation.WriteAuto, value);
  173. }
  174. public T Read<T>() where T : IStorable, new()
  175. {
  176. byte[] data = Read();
  177. DataStorage stor = new DataStorage(data);
  178. T t = new T();
  179. t.ReadFromData(stor);
  180. return t;
  181. }
  182. public void Read<T>(out T value) where T : IStorable, new()
  183. {
  184. value = Read<T>();
  185. }
  186. public void Read(out int value)
  187. {
  188. byte[] data = Read();
  189. if (data.Length != 4)
  190. throw new DataStorageReadException(4, data);
  191. value = BitConverter.ToInt32(data, 0);
  192. }
  193. public void Read(out uint value)
  194. {
  195. byte[] data = Read();
  196. if (data.Length != 4)
  197. throw new DataStorageReadException(4, data);
  198. value = BitConverter.ToUInt32(data, 0);
  199. }
  200. public void Read(out long value)
  201. {
  202. byte[] data = Read();
  203. if (data.Length != 8)
  204. throw new DataStorageReadException(8, data);
  205. value = BitConverter.ToInt64(data, 0);
  206. }
  207. public void Read(out char value)
  208. {
  209. byte[] data = Read();
  210. if (data.Length != 2)
  211. throw new DataStorageReadException(2, data);
  212. value = BitConverter.ToChar(data, 0);
  213. }
  214. public void Read(out bool value)
  215. {
  216. byte[] data = Read();
  217. if (data.Length != 1)
  218. throw new DataStorageReadException(1, data);
  219. value = BitConverter.ToBoolean(data, 0);
  220. }
  221. public void Read(out float value)
  222. {
  223. byte[] data = Read();
  224. if (data.Length != 4)
  225. throw new DataStorageReadException(4, data);
  226. value = BitConverter.ToSingle(data, 0);
  227. }
  228. public void Read(out double value)
  229. {
  230. byte[] data = Read();
  231. if (data.Length != 8)
  232. throw new DataStorageReadException(8, data);
  233. value = BitConverter.ToDouble(data, 0);
  234. }
  235. public void Read(out string value)
  236. {
  237. byte[] data = Read();
  238. value = Encoding.UTF8.GetString(data);
  239. }
  240. public void ReadUncheck(out int value)
  241. {
  242. byte[] data = new byte[4];
  243. ReadInternal(data, 4);
  244. value = BitConverter.ToInt32(data, 0);
  245. }
  246. public void ReadUncheck(out byte value)
  247. {
  248. value = Data[Position++];
  249. }
  250. public void ReadUncheck(out uint value)
  251. {
  252. byte[] data = new byte[4];
  253. ReadInternal(data, 4);
  254. value = BitConverter.ToUInt32(data, 0);
  255. }
  256. public void ReadUncheck(out long value)
  257. {
  258. byte[] data = new byte[8];
  259. ReadInternal(data, 8);
  260. value = BitConverter.ToInt64(data, 0);
  261. }
  262. public void ReadUncheck(out ulong value)
  263. {
  264. byte[] data = new byte[8];
  265. ReadInternal(data, 8);
  266. value = BitConverter.ToUInt64(data, 0);
  267. }
  268. public void ReadUncheck(out char value)
  269. {
  270. byte[] data = new byte[2];
  271. ReadInternal(data, 2);
  272. value = BitConverter.ToChar(data, 0);
  273. }
  274. public void ReadUncheck(out bool value)
  275. {
  276. byte[] data = new byte[1];
  277. ReadInternal(data, 1);
  278. value = BitConverter.ToBoolean(data, 0);
  279. }
  280. public void ReadUncheck(out float value)
  281. {
  282. byte[] data = new byte[4];
  283. ReadInternal(data, 4);
  284. value = BitConverter.ToSingle(data, 0);
  285. }
  286. public void ReadUncheck(out double value)
  287. {
  288. byte[] data = new byte[8];
  289. ReadInternal(data, 8);
  290. value = BitConverter.ToDouble(data, 0);
  291. }
  292. }
  293. }