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