IO.Text.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using System.Globalization;
  6. using CommonLang.Xml;
  7. using System.Xml;
  8. namespace CommonLang.IO
  9. {
  10. public class TextOutputStream : IOutputStream
  11. {
  12. private StringWriter output;
  13. public TextOutputStream(StringWriter output, IExternalizableFactory factory = null)
  14. : base(factory)
  15. {
  16. this.output = output;
  17. }
  18. private void PutNext(Object src)
  19. {
  20. output.Write(src + ",");
  21. }
  22. public override long GetBuffPos()
  23. {
  24. return 0;
  25. }
  26. public override void PutUTF(String str)
  27. {
  28. if (string.IsNullOrEmpty(str))
  29. {
  30. output.Write("0,,");
  31. }
  32. else
  33. {
  34. output.Write(str.Length + ",");
  35. output.Write(str + ",");
  36. }
  37. }
  38. public override void PutS8(sbyte value)
  39. {
  40. PutNext(value);
  41. }
  42. public override void PutU8(byte value)
  43. {
  44. PutNext(value);
  45. }
  46. public override void PutBool(bool value)
  47. {
  48. PutNext(value);
  49. }
  50. public override void PutS16(short value)
  51. {
  52. PutNext(value);
  53. }
  54. public override void PutU16(ushort value)
  55. {
  56. PutNext(value);
  57. }
  58. public override void PutS32(int value)
  59. {
  60. PutNext(value);
  61. }
  62. public override void PutU32(uint value)
  63. {
  64. PutNext(value);
  65. }
  66. public override void PutS64(long value)
  67. {
  68. PutNext(value);
  69. }
  70. public override void PutU64(ulong value)
  71. {
  72. PutNext(value);
  73. }
  74. public override void PutF32(float value)
  75. {
  76. PutNext(value);
  77. }
  78. public override void PutF64(double value)
  79. {
  80. PutNext(value);
  81. }
  82. public override void PutUnicode(char value)
  83. {
  84. PutNext(value);
  85. }
  86. public override void PutBytes(byte[] bytes)
  87. {
  88. PutS32(bytes.Length);
  89. if (bytes != null && bytes.Length > 0)
  90. {
  91. StringBuilder sb = new StringBuilder();
  92. for (int i = 0; i < bytes.Length; i++)
  93. {
  94. sb.Append(bytes[i].ToString());
  95. if (i < bytes.Length - 1)
  96. {
  97. sb.Append(',');
  98. }
  99. }
  100. PutUTF(sb.ToString());
  101. }
  102. }
  103. public override void PutRawData(byte[] bytes, int offset, int count)
  104. {
  105. throw new NotImplementedException();
  106. }
  107. }
  108. public class TextInputStream : IInputStream
  109. {
  110. private StringReader input;
  111. public TextInputStream(StringReader input, IExternalizableFactory factory = null)
  112. : base(factory)
  113. {
  114. this.input = input;
  115. }
  116. private String GetNext()
  117. {
  118. StringBuilder sb = new StringBuilder();
  119. while (true)
  120. {
  121. int r = input.Read();
  122. if (r == -1)
  123. break;
  124. if (r == ',')
  125. break;
  126. sb.Append((char)r);
  127. }
  128. return sb.ToString();
  129. }
  130. public override String GetUTF()
  131. {
  132. int stringLen = GetS32();
  133. char[] chars = new char[stringLen];
  134. input.Read(chars, 0, stringLen);
  135. if (input.Read() != ',')
  136. {
  137. //System.err.println("bat ending for : " + in);
  138. }
  139. return new String(chars);
  140. }
  141. public override sbyte GetS8()
  142. {
  143. return sbyte.Parse(GetNext());
  144. }
  145. public override byte GetU8()
  146. {
  147. return byte.Parse(GetNext());
  148. }
  149. public override bool GetBool()
  150. {
  151. String read = GetNext();
  152. try
  153. {
  154. return bool.Parse(read);
  155. }
  156. catch (Exception err)
  157. {
  158. Console.WriteLine("GetBool 1: " + read + ", catch: " + err);
  159. return Byte.Parse(read) != 0;
  160. }
  161. }
  162. public override short GetS16()
  163. {
  164. return short.Parse(GetNext());
  165. }
  166. public override ushort GetU16()
  167. {
  168. return ushort.Parse(GetNext());
  169. }
  170. public override int GetS32()
  171. {
  172. return int.Parse(GetNext());
  173. }
  174. public override uint GetU32()
  175. {
  176. return uint.Parse(GetNext());
  177. }
  178. public override long GetS64()
  179. {
  180. return long.Parse(GetNext(), System.Globalization.CultureInfo.InvariantCulture);
  181. }
  182. public override ulong GetU64()
  183. {
  184. return ulong.Parse(GetNext());
  185. }
  186. public override float GetF32()
  187. {
  188. return float.Parse(GetNext(), System.Globalization.CultureInfo.InvariantCulture);
  189. }
  190. public override double GetF64()
  191. {
  192. return double.Parse(GetNext(), System.Globalization.CultureInfo.InvariantCulture);
  193. }
  194. public override char GetUnicode()
  195. {
  196. return char.Parse(GetNext());
  197. }
  198. public override byte[] GetBytes()
  199. {
  200. int count = GetS32();
  201. byte[] ret = new byte[count];
  202. string utf = GetUTF();
  203. string[] utfs = utf.Split(',');
  204. for (int i = 0; i < utfs.Length; i++)
  205. {
  206. ret[i] = byte.Parse(utfs[i]);
  207. }
  208. return ret;
  209. }
  210. public override void GetRawData(byte[] buff, int offset, int count)
  211. {
  212. throw new NotImplementedException();
  213. }
  214. }
  215. }