MemoryStreamWriter.cs

MemoryStream Writer Class

using System;
using System.Collections.Generic;
using System.IO;
using System.Numerics;
using System.Reflection;
using System.Text;
public class MemoryStreamWriter : IDisposable
{
    public static string[] Readerwritertypes =
    {
        "BOOLN       ", //0
        "CHAR        ", //1
        "BYTE        ", //2
        "SBYTE       ", //3
        "SHORT       ", //4
        "USHORT      ", //5
        "INTEGER     ", //6
        "UINTEGER    ", //7
        "LONG        ", //8
        "ULONG       ", //9
        "STRING      ", //10
        "FLOAT       ", //11
        "DOUBLE      ", //12
        "DECIMAL     ", //13
        "BOOLARRY    ", //14
        "CHARARRY    ", //15
        "BYTEARRY    ", //16
        "SBYTEARRY   ", //17
        "SHORTARRY   ", //18
        "USHORTARRY  ", //19
        "INTARRY     ", //20
        "UINTARRY    ", //21
        "LONGARRY    ", //22
        "ULONGARRY   ", //23
        "STRINGARRY  ", //24
        "FLOATARRY   ", //25
        "DOUBLEARRY  ", //26
        "DECIMALARRY ", //27
        "BIGINTEGER  ", //28
        "BIGINTARRY  ", //29
        "BIGRATIONAL ", //30
        "BIGRATARRY  ", //31
        "DATETIME    ", //32
        "DATETIMEARRY", //33
        "REFVALUETYPE"  //34
    };
    private readonly Encoder  _encoder;
    private readonly Encoding _encoding;
    public MemoryStreamWriter() : this(null, new UTF8Encoding(false, true))
    {
    }
    public MemoryStreamWriter(Stream input) : this(input, new UTF8Encoding())
    {
    }
    public MemoryStreamWriter(Stream input, Encoding encoding)
    {
        if (input == null)
            BaseStream = new MemoryStream();
        else
            BaseStream = input;
        _encoding = encoding;
        _encoder  = _encoding.GetEncoder();
    }
    public Stream BaseStream
    {
        get;
    }
    public static int PrimitiveOverHead => sizeof(int);
    public static int ArrayOverHead     => sizeof(int) * 2;
    public void Dispose()
    {
        if (BaseStream == null) return;


        if (BaseStream.CanWrite)
        {
            BaseStream.Flush();
        }
        BaseStream.Dispose();
    }
    public long Seek(int offset, SeekOrigin origin)
    {
        return BaseStream.Seek(offset, origin);
    }
    public void SetData(IEnumerable<object> obja)
    {
        if (obja != null)
            foreach (var o in obja)
                Write(o);
    }
    public void Write(object obj)
    {
        var ltype = obj.GetType();
        switch (ltype.Name.Trim('[', ']'))
        {
            case "Byte":
                if (!ltype.IsArray)
                    Write((byte) obj);
                else
                    Write((byte[]) obj);
                return;
            case "Boolean":
                if (!ltype.IsArray)
                    Write((bool) obj);
                else
                    Write((bool[]) obj);
                return;
            case "SByte":
                if (!ltype.IsArray)
                    Write((sbyte) obj);
                else
                    Write((sbyte[]) obj);
                return;
            case "Char":
                if (!ltype.IsArray)
                    Write((char) obj);
                else
                    Write((char[]) obj);
                return;
            case "Int16":
                if (!ltype.IsArray)
                    Write((short) obj);
                else
                    Write((short[]) obj);
                return;
            case "UInt16":
                if (!ltype.IsArray)
                    Write((ushort) obj);
                else
                    Write((ushort[]) obj);
                return;
            case "Int32":
                if (!ltype.IsArray)
                    Write((int) obj);
                else
                    Write((int[]) obj);
                return;
            case "UInt32":
                if (!ltype.IsArray)
                    Write((uint) obj);
                else
                    Write((uint[]) obj);
                return;
            case "Int64":
                if (!ltype.IsArray)
                    Write((long) obj);
                else
                    Write((long[]) obj);
                return;
            case "UInt64":
                if (!ltype.IsArray)
                    Write((ulong) obj);
                else
                    Write((ulong[]) obj);
                return;
            case "Single":
                if (!ltype.IsArray)
                    Write((float) obj);
                else
                    Write((float[]) obj);
                return;
            case "Double":
                if (!ltype.IsArray)
                    Write((double) obj);
                else
                    Write((double[]) obj);
                return;
            case "String":
                if (!ltype.IsArray)
                    Write((string) obj);
                else
                    Write((string[]) obj);
                return;
            case "Decimal":
                if (!ltype.IsArray)
                    Write((decimal) obj);
                else
                    Write((decimal[]) obj);
                return;
            case "DateTime":
                if (!ltype.IsArray)
                    Write((DateTime) obj);
                else
                    Write((DateTime[]) obj);
                return;

            case "BigInteger":
                if (!ltype.IsArray)
                    Write((BigInteger)obj);
                else
                    Write((BigInteger[])obj);
                return;
        }
        if (ltype.IsSerializable)
        {
            var flds = ltype.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy);
            if (flds.Length > 0)
            {
                BaseStream.Write(Readerwritertypes[34].GetBytes(), 0, 4);
                var buf = obj.ObjectToByteArray();
                BaseStream.Write(buf.Length.GetBytes(), 0, 4);
                BaseStream.Write(buf,                   0, buf.Length);
                return;
            }
            throw new Exception("Serializable Value and Reference Types, no meaningful data can be found.");
        }
        throw new Exception("Unsupported Type.");
    }
    private void Write(bool value)
    {
        var head = _encoding.GetBytes(Readerwritertypes[0]);
        BaseStream.Write(head, 0, head.Length);
        var buf = value.GetBytes();
        BaseStream.Write(buf, 0, buf.Length);
    }
    private unsafe void Write(char value)
    {
        var head = _encoding.GetBytes(Readerwritertypes[1]);
        BaseStream.Write(head, 0, head.Length);
        var buf = new byte[2];
        int len;
        fixed (byte* bytes2 = buf)
        {
            len = _encoder.GetBytes(&value, 1, bytes2, buf.Length, true);
        }
        BaseStream.Write(buf, 0, len);
    }
    private void Write(byte value)
    {
        var head = _encoding.GetBytes(Readerwritertypes[2]);
        BaseStream.Write(head, 0, head.Length);
        var buf = value.GetBytes();
        BaseStream.Write(buf, 0, buf.Length);
    }
    private void Write(sbyte value)
    {
        var head = _encoding.GetBytes(Readerwritertypes[3]);
        BaseStream.Write(head, 0, head.Length);
        var buf = value.GetBytes();
        BaseStream.Write(buf, 0, buf.Length);
    }
    private void Write(short value)
    {
        var head = _encoding.GetBytes(Readerwritertypes[4]);
        BaseStream.Write(head, 0, head.Length);
        var buf = value.GetBytes();
        BaseStream.Write(buf, 0, buf.Length);
    }
    private void Write(ushort value)
    {
        var head = _encoding.GetBytes(Readerwritertypes[5]);
        BaseStream.Write(head, 0, head.Length);
        var buf = value.GetBytes();
        BaseStream.Write(buf, 0, buf.Length);
    }
    private void Write(int value)
    {
        var head = _encoding.GetBytes(Readerwritertypes[6]);
        BaseStream.Write(head, 0, head.Length);
        var buf = value.GetBytes();
        BaseStream.Write(buf, 0, buf.Length);
    }
    private void Write(uint value)
    {
        var head = _encoding.GetBytes(Readerwritertypes[7]);
        BaseStream.Write(head, 0, head.Length);
        var buf = value.GetBytes();
        BaseStream.Write(buf, 0, buf.Length);
    }
    private void Write(long value)
    {
        var head = _encoding.GetBytes(Readerwritertypes[8]);
        BaseStream.Write(head, 0, head.Length);
        var buf = value.GetBytes();
        BaseStream.Write(buf, 0, buf.Length);
    }
    private void Write(ulong value)
    {
        var head = _encoding.GetBytes(Readerwritertypes[9]);
        BaseStream.Write(head, 0, head.Length);
        var buf = value.GetBytes();
        BaseStream.Write(buf, 0, buf.Length);
    }
    private void Write(string value)
    {
        var head = _encoding.GetBytes(Readerwritertypes[10]);
        BaseStream.Write(head, 0, head.Length);
        var count = _encoding.GetByteCount(value);
        var cbuf  = count.GetBytes();
        BaseStream.Write(cbuf, 0, cbuf.Length);
        var LBuffer = new byte[count];
        _encoding.GetBytes(value, 0, value.Length, LBuffer, 0);
        BaseStream.Write(LBuffer, 0, LBuffer.Length);
    }
    private void Write(float value)
    {
        var head = _encoding.GetBytes(Readerwritertypes[11]);
        BaseStream.Write(head, 0, head.Length);
        var buf = value.GetBytes();
        BaseStream.Write(buf, 0, buf.Length);
    }
    private void Write(double value)
    {
        var head = _encoding.GetBytes(Readerwritertypes[12]);
        BaseStream.Write(head, 0, head.Length);
        var buf = value.GetBytes();
        BaseStream.Write(buf, 0, buf.Length);
    }
    private void Write(decimal value)
    {
        var head = _encoding.GetBytes(Readerwritertypes[13]);
        BaseStream.Write(head, 0, head.Length);
        var buf = value.GetBytes();
        BaseStream.Write(buf, 0, buf.Length);
    }
    private void Write(BigInteger value)
    {
        var head = _encoding.GetBytes(Readerwritertypes[28]);
        BaseStream.Write(head, 0, head.Length);
        var buf  = value.ToByteArray();
        var lbuf = buf.Length;
        var labb = lbuf.GetBytes();
        BaseStream.Write(labb, 0, labb.Length);
        BaseStream.Write(buf,  0, buf.Length);
    }
    private void Write(BigRational value)
    {
        var head = _encoding.GetBytes(Readerwritertypes[30]);
        BaseStream.Write(head, 0, head.Length);
        var Num  = value.Numerator.ToByteArray();
        var lnum = Num.Length;
        var lnab = lnum.GetBytes();
        BaseStream.Write(lnab, 0, lnab.Length);
        BaseStream.Write(Num,  0, Num.Length);
        var Den  = value.Denominator.ToByteArray();
        var lden = Den.Length;
        var ldab = lden.GetBytes();
        BaseStream.Write(ldab, 0, ldab.Length);
        BaseStream.Write(Den,  0, Den.Length);
    }
    private void Write(DateTime value)
    {
        var head = _encoding.GetBytes(Readerwritertypes[32]);
        BaseStream.Write(head, 0, head.Length);
        var asl   = value.ToBinary();
        var vbuf  = BitConverter.GetBytes(asl);
        var lbuf  = vbuf.Length;
        var lbufb = lbuf.GetBytes();
        BaseStream.Write(lbufb, 0, lbufb.Length);
        BaseStream.Write(vbuf,  0, vbuf.Length);
    }
    private void Write(bool[] value)
    {
        var head = _encoding.GetBytes(Readerwritertypes[14]);
        BaseStream.Write(head, 0, head.Length);
        var buf = value.GetBytes();
        BaseStream.Write(buf.Length.GetBytes(), 0, 4);
        BaseStream.Write(buf,                   0, buf.Length);
    }
    private void Write(char[] value)
    {
        var head = _encoding.GetBytes(Readerwritertypes[15]);
        BaseStream.Write(head, 0, head.Length);
        var bytes = _encoding.GetBytes(value, 0, value.Length);
        var bbuf  = bytes.Length.GetBytes();
        BaseStream.Write(bbuf,  0, bbuf.Length);
        BaseStream.Write(bytes, 0, bytes.Length);
    }
    private void Write(byte[] value)
    {
        var head = _encoding.GetBytes(Readerwritertypes[16]);
        BaseStream.Write(head,                    0, head.Length);
        BaseStream.Write(value.Length.GetBytes(), 0, 4);
        BaseStream.Write(value,                   0, value.Length);
    }
    private void Write(sbyte[] value)
    {
        var head = _encoding.GetBytes(Readerwritertypes[17]);
        BaseStream.Write(head, 0, head.Length);
        var buf = value.GetBytes();
        BaseStream.Write(buf.Length.GetBytes(), 0, 4);
        BaseStream.Write(buf,                   0, buf.Length);
    }
    private void Write(short[] value)
    {
        var head = _encoding.GetBytes(Readerwritertypes[18]);
        BaseStream.Write(head, 0, head.Length);
        var buf = value.GetBytes();
        BaseStream.Write(buf.Length.GetBytes(), 0, 4);
        BaseStream.Write(buf,                   0, buf.Length);
    }
    private void Write(ushort[] value)
    {
        var head = _encoding.GetBytes(Readerwritertypes[19]);
        BaseStream.Write(head, 0, head.Length);
        var buf = value.GetBytes();
        BaseStream.Write(buf.Length.GetBytes(), 0, 4);
        BaseStream.Write(buf,                   0, buf.Length);
    }
    private void Write(int[] value)
    {
        var head = _encoding.GetBytes(Readerwritertypes[20]);
        BaseStream.Write(head, 0, head.Length);
        var buf = value.GetBytes();
        BaseStream.Write(buf.Length.GetBytes(), 0, 4);
        BaseStream.Write(buf,                   0, buf.Length);
    }
    private void Write(uint[] value)
    {
        var head = _encoding.GetBytes(Readerwritertypes[21]);
        BaseStream.Write(head, 0, head.Length);
        var buf = value.GetBytes();
        BaseStream.Write(buf.Length.GetBytes(), 0, 4);
        BaseStream.Write(buf,                   0, buf.Length);
    }
    private void Write(long[] value)
    {
        var head = _encoding.GetBytes(Readerwritertypes[22]);
        BaseStream.Write(head, 0, head.Length);
        var buf = value.GetBytes();
        BaseStream.Write(buf.Length.GetBytes(), 0, 4);
        BaseStream.Write(buf,                   0, buf.Length);
    }
    private void Write(ulong[] value)
    {
        var head = _encoding.GetBytes(Readerwritertypes[23]);
        BaseStream.Write(head, 0, head.Length);
        var buf = value.GetBytes();
        BaseStream.Write(buf.Length.GetBytes(), 0, 4);
        BaseStream.Write(buf,                   0, buf.Length);
    }
    private void Write(string[] value)
    {
        var head = _encoding.GetBytes(Readerwritertypes[24]);
        BaseStream.Write(head,                    0, head.Length);
        BaseStream.Write(value.Length.GetBytes(), 0, 4);
        foreach (var v in value)
        {
            var count = _encoding.GetByteCount(v);
            var cbuf  = count.GetBytes();
            BaseStream.Write(cbuf, 0, cbuf.Length);
            var LBuffer = new byte[count];
            _encoding.GetBytes(v, 0, v.Length, LBuffer, 0);
            BaseStream.Write(LBuffer, 0, LBuffer.Length);
        }
    }
    private void Write(float[] value)
    {
        var head = _encoding.GetBytes(Readerwritertypes[25]);
        BaseStream.Write(head, 0, head.Length);
        var buf = value.GetBytes();
        BaseStream.Write(buf.Length.GetBytes(), 0, 4);
        BaseStream.Write(buf,                   0, buf.Length);
    }
    private void Write(double[] value)
    {
        var head = _encoding.GetBytes(Readerwritertypes[26]);
        BaseStream.Write(head, 0, head.Length);
        var buf = value.GetBytes();
        BaseStream.Write(buf.Length.GetBytes(), 0, 4);
        BaseStream.Write(buf,                   0, buf.Length);
    }
    private void Write(decimal[] value)
    {
        var head = _encoding.GetBytes(Readerwritertypes[27]);
        BaseStream.Write(head, 0, head.Length);
        var buf = value.GetBytes();
        BaseStream.Write(buf.Length.GetBytes(), 0, 4);
        BaseStream.Write(buf,                   0, buf.Length);
    }
    private void Write(BigInteger[] value)
    {
        var head = _encoding.GetBytes(Readerwritertypes[29]);
        BaseStream.Write(head, 0, head.Length);
        var larr   = value.Length;
        var larrab = larr.GetBytes();
        BaseStream.Write(larrab, 0, larrab.Length);
        foreach (var v in value)
        {
            var buf = v.GetBytes();
            BaseStream.Write(buf.Length.GetBytes(), 0, 4);
            BaseStream.Write(buf,                   0, buf.Length);
        }
    }
    private void Write(BigRational[] value)
    {
        var head = _encoding.GetBytes(Readerwritertypes[31]);
        BaseStream.Write(head, 0, head.Length);
        foreach (var v in value)
        {
            var buf = v.GetBytes();
            BaseStream.Write(buf.Length.GetBytes(), 0, 4);
            BaseStream.Write(buf,                   0, buf.Length);
        }
    }
    private void Write(DateTime[] value)
    {
        var head = _encoding.GetBytes(Readerwritertypes[33]);
        BaseStream.Write(head, 0, head.Length);
        foreach (var v in value)
        {
            var buf = v.Ticks.GetBytes();
            BaseStream.Write(buf.Length.GetBytes(), 0, 4);
            BaseStream.Write(buf,                   0, buf.Length);
        }
    }
}