Example #1
0
        internal static float ReadVarSingle(BinaryReader reader)
        {
            var input = reader.ReadByte();

            var buff = SharedArray.Get();

            var embedded = (input & FlagEmbedded) == FlagEmbedded;

            if (embedded)
            {
                SharedArray.ClearArray4();

                // last byte
                buff[3] = (byte)(input & MaskEmbedded);
                return(BitConverter.ToSingle(buff, 0));
            }

            int length = input;

            if (length < 4)
            {
                SharedArray.ClearArray4();
            }

            reader.Read(buff, 4 - length, length);

            return(BitConverter.ToSingle(buff, 0));
        }
Example #2
0
        internal static double ReadVarDouble(BinaryReader reader)
        {
            var input = reader.ReadByte();

            var buff = SharedArray.Get();

            var embedded = (input & FlagEmbedded) == FlagEmbedded;

            if (embedded)
            {
                SharedArray.ClearArray8();

                // last byte
                buff[7] = (byte)(input & MaskEmbedded);
                return(BitConverter.ToDouble(buff, 0));
            }

            int length = input;

            if (length < 8)
            {
                SharedArray.ClearArray8();
            }

            reader.Read(buff, 8 - length, length);

            return(BitConverter.ToDouble(buff, 0));
        }
Example #3
0
        //private static byte[] ConvertToVarBinary(long value, out byte length)
        //{
        //	var buff = SharedArray.Get();
        //	buff[0] = (byte)value;
        //	buff[1] = unchecked((byte)(value >> 8));
        //	buff[2] = unchecked((byte)(value >> 16));
        //	buff[3] = unchecked((byte)(value >> 24));
        //	buff[4] = unchecked((byte)(value >> 32));
        //	buff[5] = unchecked((byte)(value >> 40));
        //	buff[6] = unchecked((byte)(value >> 48));
        //	buff[7] = unchecked((byte)(value >> 56));

        //	for (int i = 8 - 1; i >= 0; i--)
        //	{
        //		if (buff[i] > 0)
        //		{
        //			length = (byte)(i + 1);
        //			//if (length != 8)
        //			//	Array.Resize(ref buff, length);
        //			return buff;
        //		}
        //	}

        //	length = 1;
        //	return new byte[] { 0 };
        //}

        private static byte[] ConvertToVarBinary(ulong value, out byte length)
        {
            var buff = SharedArray.Get();

            buff[0] = (byte)value;
            buff[1] = unchecked ((byte)(value >> 8));
            buff[2] = unchecked ((byte)(value >> 16));
            buff[3] = unchecked ((byte)(value >> 24));
            buff[4] = unchecked ((byte)(value >> 32));
            buff[5] = unchecked ((byte)(value >> 40));
            buff[6] = unchecked ((byte)(value >> 48));
            buff[7] = unchecked ((byte)(value >> 56));

            for (int i = 8 - 1; i >= 0; i--)
            {
                if (buff[i] > 0)
                {
                    length = (byte)(i + 1);
                    return(buff);
                }
            }

            length = 1;
            return(new byte[] { 0 });
        }
Example #4
0
        private static byte[] ConvertToVarBinary(int value, out byte length)
        {
            if (value < 0)
            {
                length = 4;
                var buff = SharedArray.Get();
                buff[0] = (byte)value;
                buff[1] = unchecked ((byte)(value >> 8));
                buff[2] = unchecked ((byte)(value >> 16));
                buff[3] = unchecked ((byte)(value >> 24));
                return(buff);
            }
            else
            {
                var buff = SharedArray.Get();
                SharedArray.ClearArray4();

                var num1 = (byte)value;
                var num2 = unchecked ((byte)(value >> 8));
                var num3 = unchecked ((byte)(value >> 16));
                var num4 = unchecked ((byte)(value >> 24));


                buff[0] = num1;

                if (num2 > 0)
                {
                    buff[1] = num2;
                }
                else if (num3 == 0 && num4 == 0)
                {
                    length = 1;
                    return(buff);
                }

                if (num3 > 0)
                {
                    buff[2] = num3;
                }
                else if (num4 == 0)
                {
                    length = 2;
                    return(buff);
                }

                if (num4 > 0)
                {
                    buff[3] = num4;
                }
                else
                {
                    length = 3;
                    return(buff);
                }
                length = 4;
                return(buff);
            }
        }
Example #5
0
        private static byte[] ConvertToVarBinaryZigzag(int value, out byte length)
        {
            var buff = SharedArray.Get();

            length = 0;

            var zigZagEncoded = unchecked ((uint)((value << 1) ^ (value >> 31)));

            while ((zigZagEncoded & ~0x7F) != 0)
            {
                buff[length++]  = (byte)((zigZagEncoded | 0x80) & 0xFF);
                zigZagEncoded >>= 7;
            }
            buff[length++] = (byte)zigZagEncoded;

            return(buff);
        }
Example #6
0
        private static byte[] ConvertToVarBinary(decimal value, out byte length)
        {
            var bits      = decimal.GetBits(value);
            var bitsArray = SharedArray.Get();

            for (byte i = 0; i < bits.Length; i++)
            {
                var bytes = BitConverter.GetBytes(bits[i]);
                Array.Copy(bytes, 0, bitsArray, i * 4, 4);
            }

            // finding the empty characters
            for (int i = bitsArray.Length - 1; i >= 0; i--)
            {
                if (bitsArray[i] > 0)
                {
                    length = (byte)(i + 1);

                    return(bitsArray);
                }
            }
            length = 1;
            return(new byte[] { 0 });
        }