IO.Stream.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. using CommonLang.ByteOrder;
  2. using CommonLang.Xml;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Xml;
  9. namespace CommonLang.IO
  10. {
  11. public class InputStream : IInputStream
  12. {
  13. private Stream _stream;
  14. private byte[] _buff = new byte[8];
  15. public InputStream(Stream stream, IExternalizableFactory factory) : base(factory)
  16. {
  17. _stream = stream;
  18. }
  19. public void SetStream(Stream stream)
  20. {
  21. _stream = stream;
  22. }
  23. public override bool GetBool()
  24. {
  25. IOUtil.ReadToEnd(_stream, _buff, 0, 1);
  26. return _buff[0] != 0;
  27. }
  28. public override byte GetU8()
  29. {
  30. IOUtil.ReadToEnd(_stream, _buff, 0, 1);
  31. return _buff[0];
  32. }
  33. public override sbyte GetS8()
  34. {
  35. IOUtil.ReadToEnd(_stream, _buff, 0, 1);
  36. return (sbyte)_buff[0];
  37. }
  38. public override ushort GetU16()
  39. {
  40. IOUtil.ReadToEnd(_stream, _buff, 0, 2);
  41. int ret = 0;
  42. ret |= (((int)_buff[0]));
  43. ret |= (((int)_buff[1]) << 8);
  44. return (ushort)ret;
  45. }
  46. public override short GetS16()
  47. {
  48. IOUtil.ReadToEnd(_stream, _buff, 0, 2);
  49. int ret = 0;
  50. ret |= (((int)_buff[0]));
  51. ret |= (((int)_buff[1]) << 8);
  52. return (short)ret;
  53. }
  54. public override uint GetU32()
  55. {
  56. IOUtil.ReadToEnd(_stream, _buff, 0, 4);
  57. int ret = 0;
  58. ret |= (((int)_buff[0]));
  59. ret |= (((int)_buff[1]) << 8);
  60. ret |= (((int)_buff[2]) << 16);
  61. ret |= (((int)_buff[3]) << 24);
  62. return (uint)ret;
  63. }
  64. public override int GetS32()
  65. {
  66. IOUtil.ReadToEnd(_stream, _buff, 0, 4);
  67. int ret = 0;
  68. ret |= (((int)_buff[0]));
  69. ret |= (((int)_buff[1]) << 8);
  70. ret |= (((int)_buff[2]) << 16);
  71. ret |= (((int)_buff[3]) << 24);
  72. return ret;
  73. }
  74. public override ulong GetU64()
  75. {
  76. IOUtil.ReadToEnd(_stream, _buff, 0, 8);
  77. ulong ret = 0;
  78. ret |= (((ulong)_buff[0]));
  79. ret |= (((ulong)_buff[1]) << 8);
  80. ret |= (((ulong)_buff[2]) << 16);
  81. ret |= (((ulong)_buff[3]) << 24);
  82. ret |= (((ulong)_buff[4]) << 32);
  83. ret |= (((ulong)_buff[5]) << 40);
  84. ret |= (((ulong)_buff[6]) << 48);
  85. ret |= (((ulong)_buff[7]) << 56);
  86. return ret;
  87. }
  88. public override long GetS64()
  89. {
  90. IOUtil.ReadToEnd(_stream, _buff, 0, 8);
  91. long ret = 0;
  92. ret |= (((long)_buff[0]));
  93. ret |= (((long)_buff[1]) << 8);
  94. ret |= (((long)_buff[2]) << 16);
  95. ret |= (((long)_buff[3]) << 24);
  96. ret |= (((long)_buff[4]) << 32);
  97. ret |= (((long)_buff[5]) << 40);
  98. ret |= (((long)_buff[6]) << 48);
  99. ret |= (((long)_buff[7]) << 56);
  100. return ret;
  101. }
  102. public override float GetF32()
  103. {
  104. IOUtil.ReadToEnd(_stream, _buff, 0, 4);
  105. return System.BitConverter.ToSingle(_buff, 0);
  106. }
  107. public override char GetUnicode()
  108. {
  109. IOUtil.ReadToEnd(_stream, _buff, 0, 2);
  110. return System.BitConverter.ToChar(_buff, 0);
  111. }
  112. public override double GetF64()
  113. {
  114. IOUtil.ReadToEnd(_stream, _buff, 0, 8);
  115. return System.BitConverter.ToDouble(_buff, 0);
  116. }
  117. public override string GetUTF()
  118. {
  119. int len = GetU16();
  120. if (len == UInt16.MaxValue)
  121. {
  122. return null;
  123. }
  124. if (len > 0)
  125. {
  126. byte[] buff = new byte[len];
  127. IOUtil.ReadToEnd(_stream, buff, 0, len);
  128. return utfenc.GetString(buff, 0, len);
  129. }
  130. return "";
  131. }
  132. public override byte[] GetBytes()
  133. {
  134. int len = GetS32();
  135. if (len > BYTES_LIMIT) { throw new IOException("Bytes overflow : " + len); }
  136. if (len < 0) return null;
  137. if (len == 0) return new byte[0];
  138. byte[] ret = new byte[len];
  139. IOUtil.ReadToEnd(_stream, ret, 0, len);
  140. return ret;
  141. }
  142. public override void GetRawData(byte[] buff, int offset, int count)
  143. {
  144. IOUtil.ReadToEnd(_stream, buff, offset, count);
  145. }
  146. }
  147. public class OutputStream : IOutputStream
  148. {
  149. private Stream _stream;
  150. public OutputStream(Stream stream, IExternalizableFactory factory):base(factory)
  151. {
  152. _stream = stream;
  153. }
  154. public override long GetBuffPos()
  155. {
  156. return (this._stream == null) ? 0 : this._stream.Position;
  157. }
  158. public void SetStream(Stream stream)
  159. {
  160. _stream = stream;
  161. }
  162. public override void PutBool(bool value)
  163. {
  164. _stream.WriteByte((byte)(value ? 1 : 0));
  165. }
  166. public override void PutU8(byte value)
  167. {
  168. _stream.WriteByte(value);
  169. }
  170. public override void PutS8(sbyte value)
  171. {
  172. _stream.WriteByte((byte)value);
  173. }
  174. public override void PutU16(ushort value)
  175. {
  176. _stream.WriteByte((byte)(value));
  177. _stream.WriteByte((byte)(value >> 8));
  178. }
  179. public override void PutS16(short value)
  180. {
  181. _stream.WriteByte((byte)(value));
  182. _stream.WriteByte((byte)(value >> 8));
  183. }
  184. public override void PutU32(uint value)
  185. {
  186. _stream.WriteByte((byte)(value));
  187. _stream.WriteByte((byte)(value >> 8));
  188. _stream.WriteByte((byte)(value >> 16));
  189. _stream.WriteByte((byte)(value >> 24));
  190. }
  191. public override void PutS32(int value)
  192. {
  193. _stream.WriteByte((byte)(value));
  194. _stream.WriteByte((byte)(value >> 8));
  195. _stream.WriteByte((byte)(value >> 16));
  196. _stream.WriteByte((byte)(value >> 24));
  197. }
  198. public override void PutU64(ulong value)
  199. {
  200. _stream.WriteByte((byte)(value));
  201. _stream.WriteByte((byte)(value >> 8));
  202. _stream.WriteByte((byte)(value >> 16));
  203. _stream.WriteByte((byte)(value >> 24));
  204. _stream.WriteByte((byte)(value >> 32));
  205. _stream.WriteByte((byte)(value >> 40));
  206. _stream.WriteByte((byte)(value >> 48));
  207. _stream.WriteByte((byte)(value >> 56));
  208. }
  209. public override void PutS64(long value)
  210. {
  211. _stream.WriteByte((byte)(value));
  212. _stream.WriteByte((byte)(value >> 8));
  213. _stream.WriteByte((byte)(value >> 16));
  214. _stream.WriteByte((byte)(value >> 24));
  215. _stream.WriteByte((byte)(value >> 32));
  216. _stream.WriteByte((byte)(value >> 40));
  217. _stream.WriteByte((byte)(value >> 48));
  218. _stream.WriteByte((byte)(value >> 56));
  219. }
  220. public override void PutF32(float value)
  221. {
  222. byte[] buff = BitConverter.GetBytes(value);
  223. _stream.Write(buff, 0, 4);
  224. }
  225. public override void PutF64(double value)
  226. {
  227. byte[] buff = BitConverter.GetBytes(value);
  228. _stream.Write(buff, 0, 8);
  229. }
  230. public override void PutUnicode(char value)
  231. {
  232. byte[] buff = BitConverter.GetBytes(value);
  233. _stream.Write(buff, 0, 2);
  234. }
  235. public override void PutUTF(string str)
  236. {
  237. if (str == null)
  238. {
  239. PutU16(UInt16.MaxValue);
  240. }
  241. else if (str.Length == 0)
  242. {
  243. PutU16(0);
  244. }
  245. else
  246. {
  247. byte[] buff = utfenc.GetBytes(str);
  248. if (buff.Length >= UInt16.MaxValue)
  249. {
  250. throw new IOException("PutUTF overflow : " + str + "\nSize=" + buff.Length);
  251. }
  252. PutU16((ushort)buff.Length);
  253. _stream.Write(buff, 0, buff.Length);
  254. }
  255. }
  256. public override void PutBytes(byte[] bytes)
  257. {
  258. if (bytes == null)
  259. {
  260. PutS32(-1);
  261. }
  262. else if (bytes.Length == 0)
  263. {
  264. PutS32(0);
  265. }
  266. else
  267. {
  268. int len = bytes.Length;
  269. if (len > BYTES_LIMIT) { throw new IOException("Bytes overflow : " + len); }
  270. PutS32(bytes.Length);
  271. _stream.Write(bytes, 0, bytes.Length);
  272. }
  273. }
  274. public override void PutRawData(byte[] bytes, int offset, int count)
  275. {
  276. _stream.Write(bytes, offset, count);
  277. }
  278. }
  279. }