using System.IO;
using System.Text;
namespace Vision.Core.Streams
{
public class WriterStream : VisionObject
{
private readonly MemoryStream _stream;
private readonly BinaryWriter _writer;
public WriterStream(int size) : this(new byte[size]) { }
public WriterStream(long size) : this(new byte[(int)size]) { }
public WriterStream(ref MemoryStream stream)
{
_stream = stream;
_writer = new BinaryWriter(_stream);
}
public WriterStream(byte[] buffer)
{
_stream = new MemoryStream(buffer, 0, buffer.Length, true, true);
_writer = new BinaryWriter(_stream);
}
public void SetOffset(long offset) => _stream.Position = offset;
public long GetOffset() => _stream.Position;
public byte[] GetBuffer() => _stream.GetBuffer();
///
/// Fills the number of bytes with the value.
///
/// The number of bytes to fill.
/// The value to fill with.
public void Fill(int count, byte value)
{
for (var i = 0; i < count; i++)
{
_writer.Write(value);
}
}
#region Write
///
/// Writes a boolean value to the current stream.
///
/// The value to write.
public void Write(bool value) => _writer.Write(value);
///
/// Writes a byte value to the current stream.
///
/// The value to write.
public void Write(byte value) => _writer.Write(value);
///
/// Writes a signed byte value to the current stream.
///
/// The value to write.
public void Write(sbyte value) => _writer.Write(value);
///
/// Writes a byte array to the current stream.
///
/// The value to write.
public void Write(byte[] value) => _writer.Write(value);
///
/// Writes a byte array to the current stream, up to the supplied length.
///
/// The value to write.
/// The amount of bytes to write
public void Write(byte[] value, int len)
{
for (var i = 0; i < len; i++)
{
_writer.Write(value[i]);
}
}
///
/// Writes a 16-bit integer value to the current stream.
///
/// The value to write.
public void Write(short value) => _writer.Write(value);
///
/// Writes a 32-bit integer value to the current stream.
///
/// The value to write.
public void Write(int value) => _writer.Write(value);
///
/// Writes a 64-bit integer value to the current stream.
///
/// The value to write.
public void Write(long value) => _writer.Write(value);
///
/// Writes an unsigned 16-bit integer value to the current stream.
///
/// The value to write.
public void Write(ushort value) => _writer.Write(value);
///
/// Writes an unsigned 32-bit integer value to the current stream.
///
/// The value to write.
public void Write(uint value) => _writer.Write(value);
///
/// Writes an unsigned 64-bit integer value to the current stream.
///
/// The value to write.
public void Write(ulong value) => _writer.Write(value);
///
/// Writes a double value to the current stream.
///
/// The value to write.
public void Write(double value) => _writer.Write(value);
///
/// Writes a decimal value to the current stream.
///
/// The value to write.
public void Write(decimal value) => _writer.Write(value);
///
/// Writes a float value to the current stream.
///
/// The value to write.
public void Write(float value) => _writer.Write(value);
///
/// Writes a string value to the current stream.
///
/// The value to write.
public void Write(string value) => Write(value, value.Length);
///
/// Writes a padded string value to the current stream.
///
/// The value to write.
/// The length of the string.
public void Write(string value, int length)
{
var buffer = Encoding.ASCII.GetBytes(value);
Write(buffer);
for (var i = 0; i < length - buffer.Length; i++)
{
Write((byte)0);
}
}
#endregion
///
/// Destroys the instance.
///
protected override void Destroy()
{
_writer?.Close();
_stream?.Close();
}
}
}