MemoryStreamReader.cs

MemoryStream Reader Class

using System;
using System.Collections.Generic;
using System.IO;
using System.Numerics;
using System.Text;
public class MemoryStreamReader : 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 Decoder  _decoder;
    private readonly Encoder  _encoder;
    private readonly Encoding _encoding;
    public MemoryStreamReader() : this(null, new UTF8Encoding(false, true))
    {
    }
    public MemoryStreamReader(Stream input) : this(input, new UTF8Encoding())
    {
    }
    public MemoryStreamReader(Stream input, Encoding encoding)
    {
        if (input == null)
        {
            BaseStream = new MemoryStream();
        }
        else
        {
            BaseStream          = input;
            BaseStream.Position = 0;
        }
        if (encoding == null)
            _encoding = new UTF8Encoding(false, true);
        else
            _encoding = encoding;
        _encoder = _encoding.GetEncoder();
        _decoder = _encoding.GetDecoder();
    }
    public Stream BaseStream
    {
        get;
        set;
    }
    private bool IsUnicode => _encoding is UnicodeEncoding;
    public void Dispose()
    {
        Dispose(true);
    }
    public void Close()
    {
        Dispose(true);
    }
    protected virtual void Dispose(bool disposing)
    {
        if (disposing)
            BaseStream.Close();
        BaseStream = null;
    }
    public IEnumerable<object> GetDataList()
    {
        var list = new List<object>();
        while (true)
        {
            var obj = Read();
            if (obj == null)
                break;
            list.Add(obj);
        }
        return list;
    }
    public KeyValuePair<string, object>[] GetDataKP()
    {
        var kpl = new List<KeyValuePair<string, object>>();
        while (true)
        {
            var obj = Read();
            if (obj == null)
                break;
            kpl.Add(new KeyValuePair<string, object>(obj.GetType().Name, obj));
        }
        return kpl.ToArray();
    }
    public object Read()
    {
        var head = GetNextHeader();
        if (head == null)
            return null;
        if (head.IndexOf(Readerwritertypes[0], StringComparison.Ordinal) != -1)
        {
            var vbuf = new byte[1];
            BaseStream.Read(vbuf, 0, 1);
            var obj = BitConverter.ToBoolean(vbuf, 0);
            return obj;
        }
        if (head.IndexOf(Readerwritertypes[1], StringComparison.Ordinal) != -1)
        {
            var cl   = IsUnicode ? 2 : 1;
            var vbuf = new byte[cl];
            BaseStream.Read(vbuf, 0, cl);
            var obj = _encoding.GetString(vbuf).ToCharArray()[0];
            return obj;
        }
        if (head.IndexOf(Readerwritertypes[2], StringComparison.Ordinal) != -1)
        {
            var vbuf = new byte[1];
            BaseStream.Read(vbuf, 0, 1);
            return vbuf;
        }
        if (head.IndexOf(Readerwritertypes[3], StringComparison.Ordinal) != -1)
        {
            var vbuf = new byte[1];
            BaseStream.Read(vbuf, 0, 1);
            return (sbyte) vbuf[0];
        }
        if (head.IndexOf(Readerwritertypes[4], StringComparison.Ordinal) != -1)
        {
            var vbuf = new byte[2];
            BaseStream.Read(vbuf, 0, 2);
            var obj = BitConverter.ToInt16(vbuf, 0);
            return obj;
        }
        if (head.IndexOf(Readerwritertypes[5], StringComparison.Ordinal) != -1)
        {
            var vbuf = new byte[2];
            BaseStream.Read(vbuf, 0, 2);
            var obj = BitConverter.ToUInt16(vbuf, 0);
            return obj;
        }
        if (head.IndexOf(Readerwritertypes[6], StringComparison.Ordinal) != -1)
        {
            var vbuf = new byte[4];
            BaseStream.Read(vbuf, 0, 4);
            var obj = BitConverter.ToInt32(vbuf, 0);
            return obj;
        }
        if (head.IndexOf(Readerwritertypes[7], StringComparison.Ordinal) != -1)
        {
            var vbuf = new byte[4];
            BaseStream.Read(vbuf, 0, 4);
            var obj = BitConverter.ToUInt32(vbuf, 0);
            return obj;
        }
        if (head.IndexOf(Readerwritertypes[8], StringComparison.Ordinal) != -1)
        {
            var vbuf = new byte[8];
            BaseStream.Read(vbuf, 0, 8);
            var obj = BitConverter.ToInt64(vbuf, 0);
            return obj;
        }
        if (head.IndexOf(Readerwritertypes[9], StringComparison.Ordinal) != -1)
        {
            var vbuf = new byte[8];
            BaseStream.Read(vbuf, 0, 8);
            var obj = BitConverter.ToUInt64(vbuf, 0);
            return obj;
        }
        if (head.IndexOf(Readerwritertypes[10], StringComparison.Ordinal) != -1)
        {
            var lbuf = new byte[4];
            BaseStream.Read(lbuf, 0, 4);
            var len  = lbuf.ToInt();
            var vbuf = new byte[len];
            BaseStream.Read(vbuf, 0, len);
            var obj = _encoding.GetString(vbuf);
            return obj;
        }
        if (head.IndexOf(Readerwritertypes[11], StringComparison.Ordinal) != -1)
        {
            var vbuf = new byte[4];
            BaseStream.Read(vbuf, 0, 4);
            var obj = BitConverter.ToSingle(vbuf, 0);
            return obj;
        }
        if (head.IndexOf(Readerwritertypes[12], StringComparison.Ordinal) != -1)
        {
            var vbuf = new byte[8];
            BaseStream.Read(vbuf, 0, 8);
            var obj = BitConverter.ToDouble(vbuf, 0);
            return obj;
        }
        if (head.IndexOf(Readerwritertypes[13], StringComparison.Ordinal) != -1)
        {
            var vbuf = new byte[16];
            BaseStream.Read(vbuf, 0, 16);
            var obj = vbuf.ToDecimal();
            return obj;
        }
        if (head.IndexOf(Readerwritertypes[28], StringComparison.Ordinal) != -1)
        {
            var lbuf = new byte[4];
            BaseStream.Read(lbuf, 0, 4);
            var len  = lbuf.ToInt();
            var vbuf = new byte[len];
            BaseStream.Read(vbuf, 0, len);
            var obj = new BigInteger(vbuf);
            return obj;
        }
        if (head.IndexOf(Readerwritertypes[30], StringComparison.Ordinal) != -1)
        {
            var nlbuf = new byte[4];
            BaseStream.Read(nlbuf, 0, 4);
            var nlen = nlbuf.ToInt();
            var nbuf = new byte[nlen];
            BaseStream.Read(nbuf, 0, nlen);
            var dlbuf = new byte[4];
            BaseStream.Read(dlbuf, 0, 4);
            var dlen = dlbuf.ToInt();
            var dbuf = new byte[dlen];
            BaseStream.Read(dbuf, 0, dlen);
            var obj = new BigRational(new BigInteger(nbuf), new BigInteger(dbuf));
            return obj;
        }
        if (head.IndexOf(Readerwritertypes[32], StringComparison.Ordinal) != -1)
        {
            var lbuf = new byte[4];
            BaseStream.Read(lbuf, 0, 4);
            var len  = lbuf.ToInt();
            var vbuf = new byte[len];
            BaseStream.Read(vbuf, 0, len);
            var ll  = BitConverter.ToInt64(vbuf, 0);
            var obj = DateTime.FromBinary(ll);
            return obj;
        }
        if (head.IndexOf(Readerwritertypes[34], StringComparison.Ordinal) != -1)
        {
            var lbuf = new byte[4];
            BaseStream.Read(lbuf, 0, 4);
            var len  = lbuf.ToInt();
            var vbuf = new byte[len];
            BaseStream.Read(vbuf, 0, len);
            var obj = vbuf.ByteArrayToObject();
            return obj;
        }

        ////start the array section tomorrow ** MJS ** Snow tonight and tomorrow 9 inches? BULL maybe 4 ** MJS ** 01/15/21
        if (head.IndexOf(Readerwritertypes[14], StringComparison.Ordinal) != -1)
        {
            var lbuf = new byte[4];
            BaseStream.Read(lbuf, 0, 4);
            var len  = lbuf.ToInt();
            var vbuf = new byte[len];
            BaseStream.Read(vbuf, 0, len);
            var obj = vbuf.ToBooleanArray();
            return obj;
        }
        if (head.IndexOf(Readerwritertypes[15], StringComparison.Ordinal) != -1)
        {
            var lbuf = new byte[4];
            BaseStream.Read(lbuf, 0, 4);
            var len  = lbuf.ToInt();
            var vbuf = new byte[len];
            BaseStream.Read(vbuf, 0, len);
            var obj = _encoding.GetString(vbuf).ToCharArray();
            return obj;
        }
        if (head.IndexOf(Readerwritertypes[16], StringComparison.Ordinal) != -1)
        {
            var lbuf = new byte[4];
            BaseStream.Read(lbuf, 0, 4);
            var len  = lbuf.ToInt();
            var vbuf = new byte[len];
            BaseStream.Read(vbuf, 0, len);
            return vbuf;
        }
        if (head.IndexOf(Readerwritertypes[17], StringComparison.Ordinal) != -1)
        {
            var lbuf = new byte[4];
            BaseStream.Read(lbuf, 0, 4);
            var len  = lbuf.ToInt();
            var vbuf = new byte[len];
            BaseStream.Read(vbuf, 0, len);
            return (sbyte[]) vbuf.Clone();
        }
        if (head.IndexOf(Readerwritertypes[18], StringComparison.Ordinal) != -1)
        {
            var lbuf = new byte[4];
            BaseStream.Read(lbuf, 0, 4);
            var len  = lbuf.ToInt();
            var vbuf = new byte[len];
            BaseStream.Read(vbuf, 0, len);
            var obj = vbuf.ToShortArray();
            return obj;
        }
        if (head.IndexOf(Readerwritertypes[19], StringComparison.Ordinal) != -1)
        {
            var lbuf = new byte[4];
            BaseStream.Read(lbuf, 0, 4);
            var len  = lbuf.ToInt();
            var vbuf = new byte[len];
            BaseStream.Read(vbuf, 0, len);
            var obj = vbuf.ToUShortArray();
            return obj;
        }
        if (head.IndexOf(Readerwritertypes[20], StringComparison.Ordinal) != -1)
        {
            var lbuf = new byte[4];
            BaseStream.Read(lbuf, 0, 4);
            var len  = lbuf.ToInt();
            var vbuf = new byte[len];
            BaseStream.Read(vbuf, 0, len);
            var obj = vbuf.ToIntArray();
            return obj;
        }
        if (head.IndexOf(Readerwritertypes[21], StringComparison.Ordinal) != -1)
        {
            var lbuf = new byte[4];
            BaseStream.Read(lbuf, 0, 4);
            var len  = lbuf.ToInt();
            var vbuf = new byte[len];
            BaseStream.Read(vbuf, 0, len);
            var obj = vbuf.ToUIntArray();
            return obj;
        }
        if (head.IndexOf(Readerwritertypes[22], StringComparison.Ordinal) != -1)
        {
            var lbuf = new byte[4];
            BaseStream.Read(lbuf, 0, 4);
            var len  = lbuf.ToInt();
            var vbuf = new byte[len];
            BaseStream.Read(vbuf, 0, len);
            var obj = vbuf.ToLongArray();
            return obj;
        }
        if (head.IndexOf(Readerwritertypes[23], StringComparison.Ordinal) != -1)
        {
            var lbuf = new byte[4];
            BaseStream.Read(lbuf, 0, 4);
            var len  = lbuf.ToInt();
            var vbuf = new byte[len];
            BaseStream.Read(vbuf, 0, len);
            var obj = vbuf.ToULongArray();
            return obj;
        }
        if (head.IndexOf(Readerwritertypes[23], StringComparison.Ordinal) != -1)
        {
            var lbuf = new byte[4];
            BaseStream.Read(lbuf, 0, 4);
            var len  = lbuf.ToInt();
            var vbuf = new byte[len];
            BaseStream.Read(vbuf, 0, len);
            var obj = vbuf.ToULongArray();
            return obj;
        }
        if (head.IndexOf(Readerwritertypes[24], StringComparison.Ordinal) != -1)
        {
            var albuf = new byte[4];
            BaseStream.Read(albuf, 0, 4);
            var alen = albuf.ToInt();
            var rstr = new string[alen];
            for (var i = 0; i < alen; ++i)
            {
                var slbuf = new byte[4];
                BaseStream.Read(slbuf, 0, 4);
                var slen = slbuf.ToInt();
                var sbuf = new byte[slen];
                var str  = _encoding.GetString(sbuf, 0, slen);
                rstr[i] = str;
            }
            return rstr;
        }
        if (head.IndexOf(Readerwritertypes[25], StringComparison.Ordinal) != -1)
        {
            var lbuf = new byte[4];
            BaseStream.Read(lbuf, 0, 4);
            var len  = lbuf.ToInt();
            var vbuf = new byte[len];
            BaseStream.Read(vbuf, 0, len);
            var obj = vbuf.ToFloatArray();
            return obj;
        }
        if (head.IndexOf(Readerwritertypes[26], StringComparison.Ordinal) != -1)
        {
            var lbuf = new byte[4];
            BaseStream.Read(lbuf, 0, 4);
            var len  = lbuf.ToInt();
            var vbuf = new byte[len];
            BaseStream.Read(vbuf, 0, len);
            var obj = vbuf.ToDoubleArray();
            return obj;
        }
        if (head.IndexOf(Readerwritertypes[27], StringComparison.Ordinal) != -1)
        {
            var lbuf = new byte[4];
            BaseStream.Read(lbuf, 0, 4);
            var len  = lbuf.ToInt();
            var vbuf = new byte[len];
            BaseStream.Read(vbuf, 0, len);
            var obj = vbuf.ToDecimalArray();
            return obj;
        }

        if (head.IndexOf(Readerwritertypes[29], StringComparison.Ordinal) != -1)
        {
            var lbuf = new byte[4];
            BaseStream.Read(lbuf, 0, 4);
            var len  = lbuf.ToInt();
            var objr = new BigInteger[len];

            for (var i = 0; i < len; ++i)
            {
                var ilb = new byte[4];
                BaseStream.Read(ilb, 0, 4);
                var blen = lbuf.ToInt();


                var vbuf = new byte[blen];
                BaseStream.Read(vbuf, 0, len);
                objr[i] = new BigInteger(vbuf);
            }
            return objr;
        }
        return null;
    }
    private string GetNextHeader()
    {
        if (PeekChar() != -1)
        {
            var buf = new byte[Readerwritertypes[0].Length];
            BaseStream.Read(buf, 0, buf.Length);
            var head = _encoding.GetString(buf);
            return head;
        }
        return null;
    }
    public int PeekChar()
    {
        if (BaseStream == null)
            throw new Exception("Input Stream is Null");
        if (!BaseStream.CanSeek)
            return -1;
        var position = BaseStream.Position;
        var coec     = ReadOne();
        BaseStream.Position = position;
        return coec;
    }
    private int ReadOne()
    {
        if (BaseStream == null)
            throw new Exception("Input Stream is Null");
        var bytes = new byte[IsUnicode ? 2 : 1];
        var sc    = new char[1];
        var br    = _decoder.GetChars(bytes, 0, bytes.Length, sc, 0);
        return br == 0 ? -1 : sc[0];
    }
}

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);
        }
    }
}