123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- using System;
- using System.IO;
- using System.Text;
- namespace fis.Vid
- {
- public class VinnoStreamReader
- {
- private readonly Stream _stream;
- public VinnoStreamReader(Stream stream)
- {
- _stream = stream;
- }
- /// <summary>
- /// Read string from stream.
- /// </summary>
- /// <returns></returns>
- public string ReadString()
- {
- var dataLength = ReadInt();
- var data = new byte[dataLength];
- _stream.Read(data, 0, dataLength);
- return Encoding.Unicode.GetString(data,0,data.Length);
- }
- /// <summary>
- /// Read a int32 value from the stream.
- /// </summary>
- /// <returns></returns>
- public int ReadInt()
- {
- var data = new byte[sizeof(int)];
- _stream.Read(data, 0, sizeof(int));
- return BitConverter.ToInt32(data, 0);
- }
- /// <summary>
- /// Read a short value from the stream.
- /// </summary>
- /// <returns></returns>
- public short ReadShort()
- {
- var data = new byte[sizeof(short)];
- _stream.Read(data, 0, sizeof(short));
- return BitConverter.ToInt16(data, 0);
- }
- /// <summary>
- /// Read a Int64 value from the stream.
- /// </summary>
- /// <returns></returns>
- public long ReadLong()
- {
- var data = new byte[sizeof(long)];
- _stream.Read(data, 0, sizeof(long));
- return BitConverter.ToInt64(data, 0);
- }
- /// <summary>
- /// Read a float value from the stream.
- /// </summary>
- /// <returns></returns>
- public float ReadFloat()
- {
- var data = new byte[sizeof(float)];
- _stream.Read(data, 0, sizeof(float));
- return BitConverter.ToSingle(data, 0);
- }
- /// <summary>
- /// Read a double value from the stream.
- /// </summary>
- /// <returns></returns>
- public double ReadDouble()
- {
- var data = new byte[sizeof(double)];
- _stream.Read(data, 0, sizeof(double));
- return BitConverter.ToDouble(data, 0);
- }
- /// <summary>
- /// Read a bool value from the stream.
- /// </summary>
- /// <returns></returns>
- public bool ReadBool()
- {
- var data = new byte[sizeof(bool)];
- _stream.Read(data, 0, sizeof(bool));
- return BitConverter.ToBoolean(data, 0);
- }
- /// <summary>
- /// Read a byte value from the stream.
- /// </summary>
- /// <returns></returns>
- public byte ReadByte()
- {
- return (byte)(_stream.ReadByte());
- }
- /// <summary>
- /// Read a byte array from the stream.
- /// </summary>
- /// <returns></returns>
- public byte[] ReadBytes()
- {
- var size = ReadInt();
- var data = new byte[size];
- _stream.Read(data, 0, size);
- return data;
- }
- public long[] ReadLongs()
- {
- var data = ReadBytes();
- var result = new long[data.Length / sizeof(long)];
- Buffer.BlockCopy(data, 0, result, 0, data.Length);
- return result;
- }
- }
- }
|