Exemplo n.º 1
0
        public static void Write(this Stream stream, ulong obj)
        {
            BSDebug.TraceStart("WriteUInt64", stream.Position);

            var objL = (long)obj;
            var b1   = (byte)(objL & 0xFFL);
            var b2   = (byte)((objL & (0xFFL << 8)) >> 8);
            var b3   = (byte)((objL & (0xFFL << 16)) >> 16);
            var b4   = (byte)((objL & (0xFFL << 24)) >> 24);
            var b5   = (byte)((objL & (0xFFL << 32)) >> 32);
            var b6   = (byte)((objL & (0xFFL << 40)) >> 40);
            var b7   = (byte)((objL & (0xFFL << 48)) >> 48);
            var b8   = (byte)((objL & (0xFFL << 56)) >> 56);

            stream.WriteByte(b1);
            stream.WriteByte(b2);
            stream.WriteByte(b3);
            stream.WriteByte(b4);
            stream.WriteByte(b5);
            stream.WriteByte(b6);
            stream.WriteByte(b7);
            stream.WriteByte(b8);

            BSDebug.TraceEnd("WriteUInt64", stream.Position);
        }
        public unsafe static void Write(this Stream stream, decimal obj)
        {
            BSDebug.TraceStart("WriteDecimal", stream.Position);

            var objPtr = (byte *)&obj;

            stream.WriteByte(objPtr[0]);
            stream.WriteByte(objPtr[1]);
            stream.WriteByte(objPtr[2]);
            stream.WriteByte(objPtr[3]);
            stream.WriteByte(objPtr[4]);
            stream.WriteByte(objPtr[5]);
            stream.WriteByte(objPtr[6]);
            stream.WriteByte(objPtr[7]);
            stream.WriteByte(objPtr[8]);
            stream.WriteByte(objPtr[9]);
            stream.WriteByte(objPtr[10]);
            stream.WriteByte(objPtr[11]);
            stream.WriteByte(objPtr[12]);
            stream.WriteByte(objPtr[13]);
            stream.WriteByte(objPtr[14]);
            stream.WriteByte(objPtr[15]);

            BSDebug.TraceEnd("WriteDecimal", stream.Position);
        }
Exemplo n.º 3
0
        public static void SkipDateTime(this Stream stream)
        {
            BSDebug.TraceStart("SkipDateTime", stream.Position);

            stream.Position += sizeof(long);

            BSDebug.TraceEnd("SkipDateTime", stream.Position);
        }
Exemplo n.º 4
0
        public static void Write(this Stream stream, DateTime obj)
        {
            BSDebug.TraceStart("WriteDateTime", stream.Position);

            stream.Write(obj.Ticks);

            BSDebug.TraceEnd("WriteDateTime", stream.Position);
        }
Exemplo n.º 5
0
        public static void SkipSByte(this Stream stream)
        {
            BSDebug.TraceStart("SkipSByte", stream.Position);

            stream.Position += sizeof(byte);

            BSDebug.TraceEnd("SkipSByte", stream.Position);
        }
Exemplo n.º 6
0
        public static void SkipChar(this Stream stream)
        {
            BSDebug.TraceStart("SkipChar", stream.Position);

            stream.Position += sizeof(short);

            BSDebug.TraceEnd("SkipChar", stream.Position);
        }
Exemplo n.º 7
0
        public static void Write(this Stream stream, sbyte obj)
        {
            BSDebug.TraceStart("WriteSByte", stream.Position);

            stream.WriteByte((byte)obj);

            BSDebug.TraceEnd("WriteSByte", stream.Position);
        }
Exemplo n.º 8
0
        public static void SkipDecimal(this Stream stream)
        {
            BSDebug.TraceStart("SkipDecimal", stream.Position);

            stream.Position += sizeof(long) * 2;

            BSDebug.TraceEnd("SkipDecimal", stream.Position);
        }
Exemplo n.º 9
0
        public static void SkipSingle(this Stream stream)
        {
            BSDebug.TraceStart("SkipSingle", stream.Position);

            stream.Position += sizeof(int);

            BSDebug.TraceEnd("SkipSingle", stream.Position);
        }
Exemplo n.º 10
0
        public static void Write(this Stream stream, bool obj)
        {
            BSDebug.TraceStart("WriteBoolean", stream.Position);

            stream.WriteByte(obj ? (byte)1 : (byte)0);

            BSDebug.TraceEnd("WriteBoolean", stream.Position);
        }
Exemplo n.º 11
0
        public static void Write(this Stream stream, double obj)
        {
            BSDebug.TraceStart("WriteDouble", stream.Position);

            stream.Write(ToLong(obj));

            BSDebug.TraceEnd("WriteDouble", stream.Position);
        }
Exemplo n.º 12
0
        public static void Write(this Stream stream, float obj)
        {
            BSDebug.TraceStart("WriteSingle", stream.Position);

            stream.Write(ToInt(obj));

            BSDebug.TraceEnd("WriteSingle", stream.Position);
        }
Exemplo n.º 13
0
        public static double ReadDouble(this Stream stream)
        {
            BSDebug.TraceStart("ReadDouble", stream.Position);

            var result = ToDouble(stream.ReadInt64());

            BSDebug.TraceEnd("ReadDouble", stream.Position);
            return(result);
        }
Exemplo n.º 14
0
        public static float ReadSingle(this Stream stream)
        {
            BSDebug.TraceStart("ReadSingle", stream.Position);

            var result = ToSingle(stream.ReadInt32());

            BSDebug.TraceEnd("ReadSingle", stream.Position);
            return(result);
        }
Exemplo n.º 15
0
        public static sbyte ReadSByte(this Stream stream)
        {
            BSDebug.TraceStart("ReadSByte", stream.Position);

            var result = (sbyte)stream.ReadByte();

            BSDebug.TraceEnd("ReadSByte", stream.Position);
            return(result);
        }
Exemplo n.º 16
0
        public static bool ReadBoolean(this Stream stream)
        {
            BSDebug.TraceStart("ReadBoolean", stream.Position);

            var result = stream.ReadByte() == 1;

            BSDebug.TraceEnd("ReadBoolean", stream.Position);
            return(result);
        }
Exemplo n.º 17
0
        public static DateTime ReadDateTime(this Stream stream)
        {
            BSDebug.TraceStart("ReadDateTime", stream.Position);

            var ticks = stream.ReadInt64();

            BSDebug.TraceEnd("ReadDateTime", stream.Position);
            return(new DateTime(ticks));
        }
Exemplo n.º 18
0
        public static void SkipString(this Stream stream)
        {
            BSDebug.TraceStart("SkipString", stream.Position);

            var strLength = stream.ReadInt32();

            stream.Position += strLength * sizeof(char);

            BSDebug.TraceEnd("SkipString", stream.Position);
        }
        public unsafe static void Write(this Stream stream, ushort obj)
        {
            BSDebug.TraceStart("WriteUInt16", stream.Position);

            var objPtr = (byte *)&obj;

            stream.WriteByte(objPtr[0]);
            stream.WriteByte(objPtr[1]);

            BSDebug.TraceEnd("WriteUInt16", stream.Position);
        }
Exemplo n.º 20
0
        public static char ReadChar(this Stream stream)
        {
            BSDebug.TraceStart("ReadChar", stream.Position);

            var b1 = stream.ReadByte();
            var b2 = stream.ReadByte();

            var value = b1 | (b2 << 8);

            BSDebug.TraceEnd("ReadChar", stream.Position);
            return((char)value);
        }
Exemplo n.º 21
0
        public static ushort ReadUInt16(this Stream stream)
        {
            BSDebug.TraceStart("ReadUInt16", stream.Position);

            var b1 = stream.ReadByte();
            var b2 = stream.ReadByte();

            var value = b1 | (b2 << 8);

            BSDebug.TraceEnd("ReadUInt16", stream.Position);
            return((ushort)value);
        }
Exemplo n.º 22
0
        public static void Write(this Stream stream, ushort obj)
        {
            BSDebug.TraceStart("WriteUInt16", stream.Position);

            var b1 = (byte)(obj & 0xFF);
            var b2 = (byte)((obj & (0xFF << 8)) >> 8);

            stream.WriteByte(b1);
            stream.WriteByte(b2);

            BSDebug.TraceEnd("WriteUInt16", stream.Position);
        }
        public unsafe static void Write(this Stream stream, float obj)
        {
            BSDebug.TraceStart("WriteSingle", stream.Position);

            var objPtr = (byte *)&obj;

            stream.WriteByte(objPtr[0]);
            stream.WriteByte(objPtr[1]);
            stream.WriteByte(objPtr[2]);
            stream.WriteByte(objPtr[3]);

            BSDebug.TraceEnd("WriteSingle", stream.Position);
        }
Exemplo n.º 24
0
        public static void Write(this Stream stream, decimal obj)
        {
            BSDebug.TraceStart("WriteDecimal", stream.Position);

            unsafe
            {
                var p = (long *)&obj;
                stream.Write(p[0]);
                stream.Write(p[1]);
            }

            BSDebug.TraceEnd("WriteDecimal", stream.Position);
        }
Exemplo n.º 25
0
        public static uint ReadUInt32(this Stream stream)
        {
            BSDebug.TraceStart("ReadUInt32", stream.Position);

            var b1 = stream.ReadByte();
            var b2 = stream.ReadByte();
            var b3 = stream.ReadByte();
            var b4 = stream.ReadByte();

            var value = b1 | (b2 << 8) | (b3 << 16) | (b4 << 24);

            BSDebug.TraceEnd("ReadUInt32", stream.Position);
            return((uint)value);
        }
Exemplo n.º 26
0
        public static void Write(this Stream stream, uint obj)
        {
            BSDebug.TraceStart("WriteUInt32", stream.Position);

            var b1 = (byte)(obj & 0xFF);
            var b2 = (byte)((obj & (0xFF << 8)) >> 8);
            var b3 = (byte)((obj & (0xFF << 16)) >> 16);
            var b4 = (byte)((obj & (0xFF << 24)) >> 24);

            stream.WriteByte(b1);
            stream.WriteByte(b2);
            stream.WriteByte(b3);
            stream.WriteByte(b4);

            BSDebug.TraceEnd("WriteUInt32", stream.Position);
        }
Exemplo n.º 27
0
        public static decimal ReadDecimal(this Stream stream)
        {
            BSDebug.TraceStart("ReadDecimal", stream.Position);

            decimal result;

            unsafe
            {
                var p = (long *)&result;
                p[0] = stream.ReadInt64();
                p[1] = stream.ReadInt64();
            }

            BSDebug.TraceEnd("ReadDecimal", stream.Position);
            return(result);
        }
Exemplo n.º 28
0
        public static void Write(this Stream stream, string obj)
        {
            BSDebug.TraceStart("WriteString", stream.Position);

            const int useBufferFrom = 7;

            var length      = obj.Length;
            var bytesLength = length * sizeof(char);

            // Write length
            stream.Write(length);

            if (length < useBufferFrom)
            {
                unsafe
                {
                    fixed(char *objPtr = obj)
                    {
                        var valueBytePtr = (byte *)objPtr;

                        for (int i = 0; i < bytesLength; i++)
                        {
                            stream.WriteByte(valueBytePtr[i]);
                        }
                    }
                }
            }
            else
            {
                var buffer = Buffers.Get(bytesLength);
                unsafe
                {
                    // It work faster but unfortunately available only from .net framework 4.6
                    //fixed (char* valuePtr = value)
                    //  fixed (byte* bufferPtr = buffer)
                    //    Buffer.MemoryCopy(valuePtr, bufferPtr, buffer.Length, usedLength);

                    fixed(void *objPtr = obj)
                    Marshal.Copy(new IntPtr(objPtr), buffer, 0, bytesLength);
                }

                // Write data
                stream.Write(buffer, 0, bytesLength);
            }

            BSDebug.TraceEnd("WriteString", stream.Position);
        }
        public unsafe static void Write(this Stream stream, double obj)
        {
            BSDebug.TraceStart("WriteDouble", stream.Position);

            var objPtr = (byte *)&obj;

            stream.WriteByte(objPtr[0]);
            stream.WriteByte(objPtr[1]);
            stream.WriteByte(objPtr[2]);
            stream.WriteByte(objPtr[3]);
            stream.WriteByte(objPtr[4]);
            stream.WriteByte(objPtr[5]);
            stream.WriteByte(objPtr[6]);
            stream.WriteByte(objPtr[7]);

            BSDebug.TraceEnd("WriteDouble", stream.Position);
        }
Exemplo n.º 30
0
        public static ulong ReadUInt64(this Stream stream)
        {
            BSDebug.TraceStart("ReadUInt64", stream.Position);

            var b1 = (long)stream.ReadByte();
            var b2 = (long)stream.ReadByte();
            var b3 = (long)stream.ReadByte();
            var b4 = (long)stream.ReadByte();
            var b5 = (long)stream.ReadByte();
            var b6 = (long)stream.ReadByte();
            var b7 = (long)stream.ReadByte();
            var b8 = (long)stream.ReadByte();

            var value = b1 | (b2 << 8) | (b3 << 16) | (b4 << 24) | (b5 << 32) | (b6 << 40) | (b7 << 48) | (b8 << 56);

            BSDebug.TraceEnd("ReadUInt64", stream.Position);
            return((ulong)value);
        }