VinnoStreamReader.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. namespace fis.Vid
  5. {
  6. public class VinnoStreamReader
  7. {
  8. private readonly Stream _stream;
  9. public VinnoStreamReader(Stream stream)
  10. {
  11. _stream = stream;
  12. }
  13. /// <summary>
  14. /// Read string from stream.
  15. /// </summary>
  16. /// <returns></returns>
  17. public string ReadString()
  18. {
  19. var dataLength = ReadInt();
  20. var data = new byte[dataLength];
  21. _stream.Read(data, 0, dataLength);
  22. return Encoding.Unicode.GetString(data,0,data.Length);
  23. }
  24. /// <summary>
  25. /// Read a int32 value from the stream.
  26. /// </summary>
  27. /// <returns></returns>
  28. public int ReadInt()
  29. {
  30. var data = new byte[sizeof(int)];
  31. _stream.Read(data, 0, sizeof(int));
  32. return BitConverter.ToInt32(data, 0);
  33. }
  34. /// <summary>
  35. /// Read a short value from the stream.
  36. /// </summary>
  37. /// <returns></returns>
  38. public short ReadShort()
  39. {
  40. var data = new byte[sizeof(short)];
  41. _stream.Read(data, 0, sizeof(short));
  42. return BitConverter.ToInt16(data, 0);
  43. }
  44. /// <summary>
  45. /// Read a Int64 value from the stream.
  46. /// </summary>
  47. /// <returns></returns>
  48. public long ReadLong()
  49. {
  50. var data = new byte[sizeof(long)];
  51. _stream.Read(data, 0, sizeof(long));
  52. return BitConverter.ToInt64(data, 0);
  53. }
  54. /// <summary>
  55. /// Read a float value from the stream.
  56. /// </summary>
  57. /// <returns></returns>
  58. public float ReadFloat()
  59. {
  60. var data = new byte[sizeof(float)];
  61. _stream.Read(data, 0, sizeof(float));
  62. return BitConverter.ToSingle(data, 0);
  63. }
  64. /// <summary>
  65. /// Read a double value from the stream.
  66. /// </summary>
  67. /// <returns></returns>
  68. public double ReadDouble()
  69. {
  70. var data = new byte[sizeof(double)];
  71. _stream.Read(data, 0, sizeof(double));
  72. return BitConverter.ToDouble(data, 0);
  73. }
  74. /// <summary>
  75. /// Read a bool value from the stream.
  76. /// </summary>
  77. /// <returns></returns>
  78. public bool ReadBool()
  79. {
  80. var data = new byte[sizeof(bool)];
  81. _stream.Read(data, 0, sizeof(bool));
  82. return BitConverter.ToBoolean(data, 0);
  83. }
  84. /// <summary>
  85. /// Read a byte value from the stream.
  86. /// </summary>
  87. /// <returns></returns>
  88. public byte ReadByte()
  89. {
  90. return (byte)(_stream.ReadByte());
  91. }
  92. /// <summary>
  93. /// Read a byte array from the stream.
  94. /// </summary>
  95. /// <returns></returns>
  96. public byte[] ReadBytes()
  97. {
  98. var size = ReadInt();
  99. var data = new byte[size];
  100. _stream.Read(data, 0, size);
  101. return data;
  102. }
  103. public long[] ReadLongs()
  104. {
  105. var data = ReadBytes();
  106. var result = new long[data.Length / sizeof(long)];
  107. Buffer.BlockCopy(data, 0, result, 0, data.Length);
  108. return result;
  109. }
  110. }
  111. }