RawMemoryStreamWriter.cs

Writes Raw Interpreted Primitive Values to a Memory Stream

It is not possible to recode the raw data stream once encoded, thus there is no RawMemoryStreamReader.

using System;
using System.IO;
using System.Security;
using System.Text;
[Serializable]
public class RawMemoryStreamWriter : IDisposable
{
    private readonly Encoder      _encoder;
    private readonly Encoding     _encoding;
    private          byte[]       _buffer;
    private          MemoryStream _outStream;
    public RawMemoryStreamWriter()
    {
        _outStream = new MemoryStream();
        _buffer    = new byte[8];
        _encoding  = new UTF8Encoding(false, true);
        _encoder   = _encoding.GetEncoder();
    }
    public MemoryStream BaseStream
    {
        get
        {
            Flush();
            return _outStream;
        }
    }
    public void Dispose()
    {
        Dispose(true);
    }
    public void Close()
    {
        Dispose(true);
    }
    protected void Dispose(bool disposing)
    {
        if (!disposing)
            return;
        _outStream.Close();
    }
    public void Flush()
    {
        _outStream.Flush();
    }
    public long Seek(int offset, SeekOrigin origin)
    {
        return _outStream.Seek(offset, origin);
    }
    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);
                break;
            case "Boolean":
                if (!ltype.IsArray)
                    Write((bool) obj);
                else
                    Write((bool[]) obj);
                break;
            case "SByte":
                if (!ltype.IsArray)
                    Write((sbyte) obj);
                else
                    Write((sbyte[]) obj);
                break;
            case "Char":
                if (!ltype.IsArray)
                    Write((char) obj);
                else
                    Write((char[]) obj);
                break;
            case "Int16":
                if (!ltype.IsArray)
                    Write((short) obj);
                else
                    Write((short[]) obj);
                break;
            case "UInt16":
                if (!ltype.IsArray)
                    Write((ushort) obj);
                else
                    Write((ushort[]) obj);
                break;
            case "Int32":
                if (!ltype.IsArray)
                    Write((int) obj);
                else
                    Write((int[]) obj);
                break;
            case "UInt32":
                if (!ltype.IsArray)
                    Write((uint) obj);
                else
                    Write((uint[]) obj);
                break;
            case "Int64":
                if (!ltype.IsArray)
                    Write((long) obj);
                else
                    Write((long[]) obj);
                break;
            case "UInt64":
                if (!ltype.IsArray)
                    Write((ulong) obj);
                else
                    Write((ulong[]) obj);
                break;
            case "Single":
                if (!ltype.IsArray)
                    Write((float) obj);
                else
                    Write((float[]) obj);
                break;
            case "Double":
                if (!ltype.IsArray)
                    Write((double) obj);
                else
                    Write((double[]) obj);
                break;
            case "String":
                if (!ltype.IsArray)
                    Write((string) obj);
                else
                    Write((string[]) obj);
                break;
            case "Decimal":
                if (!ltype.IsArray)
                    Write((decimal) obj);
                else
                    Write((decimal[]) obj);
                break;
            case "DateTime":
                if (!ltype.IsArray)
                    Write((DateTime) obj);
                else
                    Write((DateTime[]) obj);
                break;
        }
    }
    private void Write(bool value)
    {
        _buffer[0] = value ? (byte) 1 : (byte) 0;
        _outStream.Write(_buffer, 0, 1);
    }
    private void Write(bool[] value)
    {
        for (var i = 0; i < value.Length; ++i)
        {
            _buffer[0] = value[i] ? (byte) 1 : (byte) 0;
            _outStream.Write(_buffer, 0, 1);
        }
    }
    private void Write(byte value)
    {
        _outStream.WriteByte(value);
    }
    private void Write(sbyte value)
    {
        _outStream.WriteByte((byte) value);
    }
    private void Write(sbyte[] value)
    {
        for (var i = 0; i < value.Length; ++i)
            _outStream.WriteByte((byte) value[i]);
    }
    private void Write(byte[] buffer)
    {
        if (buffer == null)
            throw new ArgumentNullException(nameof(buffer));
        _outStream.Write(buffer, 0, buffer.Length);
    }
    private void Write(byte[] buffer, int index, int count)
    {
        _outStream.Write(buffer, index, count);
    }
    [SecuritySafeCritical]
    private unsafe void Write(char ch)
    {
        int blen;
        fixed (byte* bytes = _buffer)
        {
            blen = _encoder.GetBytes(&ch, 1, bytes, _buffer.Length, true);
        }
        _outStream.Write(_buffer, 0, blen);
    }
    private void Write(char[] chars)
    {
        if (chars == null)
            throw new ArgumentNullException(nameof(chars));
        var bytes = _encoding.GetBytes(chars, 0, chars.Length);
        _outStream.Write(bytes, 0, bytes.Length);
    }
    private void Write(char[] chars, int index, int count)
    {
        var bytes = _encoding.GetBytes(chars, index, count);
        _outStream.Write(bytes, 0, bytes.Length);
    }
    [SecuritySafeCritical]
    private unsafe void Write(double value)
    {
        var num = (ulong) *(long*) &value;
        _buffer[0] = (byte) num;
        _buffer[1] = (byte) (num >> 8);
        _buffer[2] = (byte) (num >> 16);
        _buffer[3] = (byte) (num >> 24);
        _buffer[4] = (byte) (num >> 32);
        _buffer[5] = (byte) (num >> 40);
        _buffer[6] = (byte) (num >> 48);
        _buffer[7] = (byte) (num >> 56);
        _outStream.Write(_buffer, 0, 8);
    }
    private void Write(double[] value)
    {
        for (var i = 0; i < value.Length; ++i)
            Write(value[i]);
    }
    private void Write(decimal value)
    {
        var bits  = decimal.GetBits(value);
        var lo    = bits[0];
        var mid   = bits[1];
        var hi    = bits[2];
        var flags = bits[3];
        _buffer[0]  = (byte) lo;
        _buffer[1]  = (byte) (lo >> 8);
        _buffer[2]  = (byte) (lo >> 16);
        _buffer[3]  = (byte) (lo >> 24);
        _buffer[4]  = (byte) mid;
        _buffer[5]  = (byte) (mid >> 8);
        _buffer[6]  = (byte) (mid >> 16);
        _buffer[7]  = (byte) (mid >> 24);
        _buffer[8]  = (byte) hi;
        _buffer[9]  = (byte) (hi >> 8);
        _buffer[10] = (byte) (hi >> 16);
        _buffer[11] = (byte) (hi >> 24);
        _buffer[12] = (byte) flags;
        _buffer[13] = (byte) (flags >> 8);
        _buffer[14] = (byte) (flags >> 16);
        _buffer[15] = (byte) (flags >> 24);
        _outStream.Write(_buffer, 0, 16);
    }
    private void Write(decimal[] value)
    {
        for (var i = 0; i < value.Length; ++i)
            Write(value[i]);
    }
    private void Write(short value)
    {
        _buffer[0] = (byte) value;
        _buffer[1] = (byte) ((uint) value >> 8);
        _outStream.Write(_buffer, 0, 2);
    }
    private void Write(short[] value)
    {
        for (var i = 0; i < value.Length; ++i)
            Write(value[i]);
    }
    private void Write(ushort value)
    {
        _buffer[0] = (byte) value;
        _buffer[1] = (byte) ((uint) value >> 8);
        _outStream.Write(_buffer, 0, 2);
    }
    private void Write(ushort[] value)
    {
        for (var i = 0; i < value.Length; ++i)
            Write(value[i]);
    }
    private void Write(int value)
    {
        _buffer[0] = (byte) value;
        _buffer[1] = (byte) (value >> 8);
        _buffer[2] = (byte) (value >> 16);
        _buffer[3] = (byte) (value >> 24);
        _outStream.Write(_buffer, 0, 4);
    }
    private void Write(int[] value)
    {
        for (var i = 0; i < value.Length; ++i)
            Write(value[i]);
    }
    private void Write(uint value)
    {
        _buffer[0] = (byte) value;
        _buffer[1] = (byte) (value >> 8);
        _buffer[2] = (byte) (value >> 16);
        _buffer[3] = (byte) (value >> 24);
        _outStream.Write(_buffer, 0, 4);
    }
    private void Write(uint[] value)
    {
        for (var i = 0; i < value.Length; ++i)
            Write(value[i]);
    }
    private void Write(long value)
    {
        _buffer[0] = (byte) value;
        _buffer[1] = (byte) (value >> 8);
        _buffer[2] = (byte) (value >> 16);
        _buffer[3] = (byte) (value >> 24);
        _buffer[4] = (byte) (value >> 32);
        _buffer[5] = (byte) (value >> 40);
        _buffer[6] = (byte) (value >> 48);
        _buffer[7] = (byte) (value >> 56);
        _outStream.Write(_buffer, 0, 8);
    }
    private void Write(long[] value)
    {
        for (var i = 0; i < value.Length; ++i)
            Write(value[i]);
    }
    private void Write(ulong value)
    {
        _buffer[0] = (byte) value;
        _buffer[1] = (byte) (value >> 8);
        _buffer[2] = (byte) (value >> 16);
        _buffer[3] = (byte) (value >> 24);
        _buffer[4] = (byte) (value >> 32);
        _buffer[5] = (byte) (value >> 40);
        _buffer[6] = (byte) (value >> 48);
        _buffer[7] = (byte) (value >> 56);
        _outStream.Write(_buffer, 0, 8);
    }
    private void Write(ulong[] value)
    {
        for (var i = 0; i < value.Length; ++i)
            Write(value[i]);
    }
    [SecuritySafeCritical]
    private unsafe void Write(float value)
    {
        var num = *(uint*) &value;
        _buffer[0] = (byte) num;
        _buffer[1] = (byte) (num >> 8);
        _buffer[2] = (byte) (num >> 16);
        _buffer[3] = (byte) (num >> 24);
        _outStream.Write(_buffer, 0, 4);
    }
    private void Write(float[] value)
    {
        for (var i = 0; i < value.Length; ++i)
            Write(value[i]);
    }
    [SecuritySafeCritical]
    private void Write(string value)
    {
        int count;
        if (value != null)
            count = _encoding.GetByteCount(value);
        else
            throw new Exception("Value cannot be null");
        var LBuffer = new byte[count];
        _encoding.GetBytes(value, 0, value.Length, LBuffer, 0);
        _outStream.Write(LBuffer, 0, count);
    }
    private void Write(string[] value)
    {
        for (var i = 0; i < value.Length; ++i)
            Write(value[i]);
    }
    private unsafe void Write(DateTime value)
    {
        var numArray = new byte[8];
        fixed (byte* ptr = numArray)
        {
            *(long*) ptr = value.Ticks;
        }
        _buffer[0] = numArray[0];
        _buffer[1] = numArray[1];
        _buffer[2] = numArray[2];
        _buffer[3] = numArray[3];
        _buffer[4] = numArray[4];
        _buffer[5] = numArray[5];
        _buffer[6] = numArray[6];
        _buffer[7] = numArray[7];
        _outStream.Write(_buffer, 0, 8);
    }
    private void Write(DateTime[] value)
    {
        for (var i = 0; i < value.Length; ++i)
            Write(value[i]);
    }
}

Leave a Reply

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