Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ArStreamString.cs 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. namespace uqac_ia_aspirobot.Common
  6. {
  7. public class ArStreamString
  8. {
  9. private readonly Stream ioStream;
  10. private readonly UnicodeEncoding streamEncoding;
  11. private readonly IList<string> _writtenStrings = new List<string>();
  12. private readonly IList<string> _readStrings = new List<string>();
  13. public ArStreamString(Stream ioStream)
  14. {
  15. this.ioStream = ioStream;
  16. streamEncoding = new UnicodeEncoding();
  17. }
  18. public string ReadString()
  19. {
  20. int len = ioStream.ReadByte() * 256;
  21. len += ioStream.ReadByte();
  22. byte[] inBuffer = new byte[len];
  23. ioStream.Read(inBuffer, 0, len);
  24. var str = streamEncoding.GetString(inBuffer);
  25. _readStrings.Add(str);
  26. return str;
  27. }
  28. public int ReadInt()
  29. {
  30. var str = ReadString();
  31. return int.Parse(str);
  32. }
  33. public T ReadEnum<T>()
  34. {
  35. var str = ReadString();
  36. return (T)Enum.Parse(typeof(T), str);
  37. }
  38. public int Write(string outString)
  39. {
  40. _writtenStrings.Add(outString);
  41. byte[] outBuffer = streamEncoding.GetBytes(outString);
  42. int len = outBuffer.Length;
  43. if (len > UInt16.MaxValue)
  44. {
  45. len = (int)UInt16.MaxValue;
  46. }
  47. ioStream.WriteByte((byte)(len / 256));
  48. ioStream.WriteByte((byte)(len & 255));
  49. ioStream.Write(outBuffer, 0, len);
  50. ioStream.Flush();
  51. return outBuffer.Length + 2;
  52. }
  53. public int Write(int outInt)
  54. {
  55. return Write(outInt.ToString());
  56. }
  57. public int Write(Enum outEnum)
  58. {
  59. return Write(outEnum.ToString());
  60. }
  61. }
  62. }