Writer.cs

Simplified Binary Writer

Example Code:

using System;
using System.IO;
using System.Numerics;
using System.Text;
/// <inheritdoc />
/// <summary>
///     Alternate Binary Writer created to enhance usability and ease coding complexity.
/// </summary>
public class Writer : IDisposable
{
    /// <inheritdoc />
    /// <summary>
    ///     Initializes a new instance of the Writer class based on a specified path name, a default FileMode of Create, and
    ///     defaulting to UTF-8 encoding.
    /// </summary>
    public Writer(string path) : this(path, FileMode.Create, new UTF8Encoding(false, true))
    {
    }
    /// <inheritdoc />
    /// <summary>
    ///     Initializes a new instance of the Writer class based on a specified path name, a specified FileMode and defaulting
    ///     to UTF-8 encoding.
    /// </summary>
    public Writer(string path, FileMode fm) : this(path, fm, new UTF8Encoding(false, true))
    {
    }
    /// <summary>
    ///     Initializes a new instance of the Writer class based on a specified path name, a specified FileMode and a specified
    ///     encoding.
    /// </summary>
    public Writer(string path, FileMode fm, Encoding encoding)
    {
        if (string.IsNullOrEmpty(path))
            throw new Exception($"Path {path} cannot be null or empty.");
        BaseMode = fm;
        try
        {
            if (File.Exists(path))
                File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Normal);
            BaseStream = new FileStream(path, BaseMode, FileAccess.Write);
            if (BaseStream?.CanWrite == true)
                BaseWriter = new BinaryWriter(BaseStream, encoding, true);
            else throw new Exception($"The FileStream for path:{path} is null.");
        }
        catch (Exception e)
        {
            throw new Exception($"Error: {e.Message}");
        }
    }
    public Writer(Stream stream) : this(stream, new UTF8Encoding(false, true))
    {
    }
    public Writer(Stream strm, Encoding encoding)
    {
        try
        {
            if (strm?.CanWrite == true)
                BaseWriter = new BinaryWriter(strm, encoding, true);
            else throw new Exception("The Stream is null.");
        }
        catch (Exception e)
        {
            throw new Exception($"Error: {e.Message}");
        }
    }
    /// <summary>
    ///     Expose the underlying BinaryWriter
    /// </summary>
    public BinaryWriter BaseWriter { get; }
    /// <summary>
    ///     Expose the underlying FileMode
    /// </summary>
    public FileMode BaseMode { get; }
    /// <summary>
    ///     Expose the underlying FileStream
    /// </summary>
    public FileStream BaseStream { get; }
    /// <summary>
    ///     Each time a primitive is written there is one additional integer written to signify its type.
    ///     This can be used to compute the final size of the stream bytes needed.
    /// </summary>
    public static int PrimitiveOverHead => sizeof(int);
    /// <summary>
    ///     Each time a array is written there are two additional integers written one to signify its type and the other its
    ///     size.
    ///     This can be used to compute the final size of the stream bytes needed.
    /// </summary>
    public static int ArrayOverHead => sizeof(int) * 2;
    public void Dispose()
    {
        if (BaseStream == null) return;
        BaseStream.Flush();
        BaseStream.Dispose();
    }
    /// <summary>
    ///     Sets the position to offset relative to origin within the stream.
    /// </summary>
    public long Seek(int offset, SeekOrigin origin)
    {
        return BaseStream.Seek(offset, origin);
    }
    public void WriteBool(bool value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.Boolean);
        BaseWriter.Write(value);
    }
    public void WriteChar(char value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.Character);
        BaseWriter.Write(value);
    }
    public void WriteByte(byte value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.Byte);
        BaseWriter.Write(value);
    }
    public void WriteSByte(sbyte value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.ShortByte);
        BaseWriter.Write(value);
    }
    public void WriteShort(short value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.Short);
        BaseWriter.Write(value);
    }
    public void WriteUShort(ushort value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.UnsignedShort);
        BaseWriter.Write(value);
    }
    public void WriteInt(int value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.Integer);
        BaseWriter.Write(value);
    }
    public void WriteUInt(uint value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.UnsignedInteger);
        BaseWriter.Write(value);
    }
    public void WriteLOng(long value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.Long);
        BaseWriter.Write(value);
    }
    public void WriteULong(ulong value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.UnsignedLong);
        BaseWriter.Write(value);
    }
    public void WriteString(string value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.String);
        BaseWriter.Write(value);
    }
    public void WriteFloat(float value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.Float);
        BaseWriter.Write(value);
    }
    public void WriteDouble(double value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.Double);
        BaseWriter.Write(value);
    }
    public void WriteDecimal(decimal value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.Decimal);
        BaseWriter.Write(value);
    }
    public void WriteBigInteger(BigInteger value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.BigInteger);
        WriteBytes(value.ToByteArray());
    }
    public void WriteBigRational(BigRational value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.BigRational);
        var Num = value.Numerator.ToByteArray();
        var Den = value.Denominator.ToByteArray();
        WriteBytes(Num);
        WriteBytes(Den);
    }
    public void WriteBools(bool[] value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.BooleanArray);
        var len = value.Length;
        BaseWriter.Write(len);
        foreach (var v in value)
            BaseWriter.Write(v);
    }
    public void WriteChars(char[] value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.CharacterArray);
        var len = value.Length;
        BaseWriter.Write(len);
        foreach (var v in value)
            BaseWriter.Write(v);
    }
    public void WriteBytes(byte[] value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.ByteArray);
        var len = value.Length;
        BaseWriter.Write(len);
        foreach (var v in value)
            BaseWriter.Write(v);
    }
    public void WriteSBytes(sbyte[] value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.ShortByteArray);
        var len = value.Length;
        BaseWriter.Write(len);
        foreach (var v in value)
            BaseWriter.Write(v);
    }
    public void WriteShorts(short[] value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.ShortArray);
        var len = value.Length;
        BaseWriter.Write(len);
        foreach (var v in value)
            BaseWriter.Write(v);
    }
    public void WriteUShorts(ushort[] value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.UnsignedShortArray);
        var len = value.Length;
        BaseWriter.Write(len);
        foreach (var v in value)
            BaseWriter.Write(v);
    }
    public void WriteInts(int[] value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.IntegerArray);
        var len = value.Length;
        BaseWriter.Write(len);
        foreach (var v in value)
            BaseWriter.Write(v);
    }
    public void WriteUInts(uint[] value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.UnsignedIntegerArray);
        var len = value.Length;
        BaseWriter.Write(len);
        foreach (var v in value)
            BaseWriter.Write(v);
    }
    public void WriteLongs(long[] value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.LongArray);
        var len = value.Length;
        BaseWriter.Write(len);
        foreach (var v in value)
            BaseWriter.Write(v);
    }
    public void WriteULongs(ulong[] value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.UnsignedLongArray);
        var len = value.Length;
        BaseWriter.Write(len);
        foreach (var v in value)
            BaseWriter.Write(v);
    }
    public void WriteStrings(string[] value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.StringArray);
        var len = value.Length;
        BaseWriter.Write(len);
        foreach (var v in value)
            BaseWriter.Write(v);
    }
    public void WriteFloats(float[] value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.FloatArray);
        var len = value.Length;
        BaseWriter.Write(len);
        foreach (var v in value)
            BaseWriter.Write(v);
    }
    public void WriteDoubles(double[] value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.DoubleArray);
        var len = value.Length;
        BaseWriter.Write(len);
        foreach (var v in value)
            BaseWriter.Write(v);
    }
    public void WriteDecimals(decimal[] value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.DecimalArray);
        var len = value.Length;
        BaseWriter.Write(len);
        foreach (var v in value)
            BaseWriter.Write(v);
    }
    public void WriteBigIntegers(BigInteger[] value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.BigIntegerArray);
        var len = value.Length;
        BaseWriter.Write(len);
        foreach (var v in value)
            WriteBigInteger(v);
    }
    public void WriteBigRationals(BigRational[] value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.BigRationalArray);
        var len = value.Length;
        BaseWriter.Write(len);
        foreach (var v in value)
            WriteBigRational(v);
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *