private static object DeserializeObject(PooledSegment value)
 {
     using (var ms = new MemoryStream(value.Array, 0, value.Count))
     {
         return(new BinaryFormatter().Deserialize(ms));
     }
 }
		public static PooledSegment GetBytes(IBufferAllocator allocator, bool value)
		{
			var retval = new PooledSegment(allocator, 1);
			retval.Array[0] = value ? TRUE : FALSE;

			return retval;
		}
        public static PooledSegment GetBytes(IBufferAllocator allocator, bool value)
        {
            var retval = new PooledSegment(allocator, 1);

            retval.Array[0] = value ? TRUE : FALSE;

            return(retval);
        }
        public unsafe static short ToInt16(PooledSegment value)
        {
            if (value.Count != 2)
                throw new ArgumentOutOfRangeException("value.Count", value.Count, "count must be == 2");

            fixed(byte *ptr = value.Array)
            {
                return((short)(ptr[0] + (ptr[1] << 8)));
            }
        }
        public unsafe static PooledSegment GetBytes(IBufferAllocator allocator, short value)
        {
            var retval = new PooledSegment(allocator, 2);

            fixed(byte *ptr = retval.Array)
            {
                ptr[0] = (byte)value;
                ptr[1] = (byte)(value >> 8);
            }

            return(retval);
        }
		public unsafe static PooledSegment GetBytes(IBufferAllocator allocator, short value)
		{
			var retval = new PooledSegment(allocator, 2);

			fixed (byte* ptr = retval.Array)
			{
				ptr[0] = (byte)value;
				ptr[1] = (byte)(value >> 8);
			}

			return retval;
		}
        public unsafe static int ToInt32(PooledSegment value)
        {
            if (value.Count != 4)
                throw new ArgumentOutOfRangeException("value.Count", value.Count, "count must be == 4");

            fixed(byte *ptr = value.Array)
            {
                return(ptr[0]
                       + (ptr[1] << 8)
                       + (ptr[2] << 16)
                       + (ptr[3] << 24));
            }
        }
        private PooledSegment SerializeObject(object value)
        {
            using (var ms = new PooledMemoryStream(allocator))
            {
                new BinaryFormatter().Serialize(ms, value);

                var retval = new PooledSegment(allocator, (int)ms.Length);
                ms.Position = 0;
                ms.Read(retval.Array, 0, retval.Count);

                return(retval);
            }
        }
        public unsafe static long ToInt64(PooledSegment value)
        {
            if (value.Count < 8)
                throw new ArgumentOutOfRangeException("value.Count", value.Count, "count must be >= 8");

            fixed(byte *ptr = value.Array)
            {
                return(ptr[0]
                       + (ptr[1] << 8)
                       + (ptr[2] << 16)
                       + (ptr[3] << 24)
                       + (ptr[4] << 32)
                       + (ptr[5] << 40)
                       + (ptr[6] << 48)
                       + (ptr[7] << 56));
            }
        }
        public unsafe static PooledSegment GetBytes(IBufferAllocator allocator, long value)
        {
            var retval = new PooledSegment(allocator, 8);

            fixed(byte *ptr = retval.Array)
            {
                ptr[0] = (byte)value;
                ptr[1] = (byte)(value >> 8);
                ptr[2] = (byte)(value >> 16);
                ptr[3] = (byte)(value >> 24);
                ptr[4] = (byte)(value >> 32);
                ptr[5] = (byte)(value >> 40);
                ptr[6] = (byte)(value >> 48);
                ptr[7] = (byte)(value >> 56);
            }

            return(retval);
        }
        public unsafe static PooledSegment GetBytes(IBufferAllocator allocator, decimal value)
        {
            // should use 'internal static void GetBytes(decimal d, byte[] buffer)'
            int v;
            var tmp    = Decimal.GetBits(value);
            var retval = new PooledSegment(allocator, 16);

            const int I_0 = 0;
            const int I_1 = 4;
            const int I_2 = 8;
            const int I_3 = 12;

            fixed(byte *ptr = retval.Array)
            {
                v            = tmp[0];
                ptr[0 + I_0] = (byte)v;
                ptr[1 + I_0] = (byte)(v >> 8);
                ptr[2 + I_0] = (byte)(v >> 16);
                ptr[3 + I_0] = (byte)(v >> 24);

                v            = tmp[1];
                ptr[0 + I_1] = (byte)v;
                ptr[1 + I_1] = (byte)(v >> 8);
                ptr[2 + I_1] = (byte)(v >> 16);
                ptr[3 + I_1] = (byte)(v >> 24);

                v            = tmp[2];
                ptr[0 + I_2] = (byte)v;
                ptr[1 + I_2] = (byte)(v >> 8);
                ptr[2 + I_2] = (byte)(v >> 16);
                ptr[3 + I_2] = (byte)(v >> 24);

                v            = tmp[3];
                ptr[0 + I_3] = (byte)v;
                ptr[1 + I_3] = (byte)(v >> 8);
                ptr[2 + I_3] = (byte)(v >> 16);
                ptr[3 + I_3] = (byte)(v >> 24);
            }

            return(retval);
        }
		public unsafe static PooledSegment GetBytes(IBufferAllocator allocator, decimal value)
		{
			// should use 'internal static void GetBytes(decimal d, byte[] buffer)'
			int v;
			var tmp = Decimal.GetBits(value);
			var retval = new PooledSegment(allocator, 16);

			const int I_0 = 0;
			const int I_1 = 4;
			const int I_2 = 8;
			const int I_3 = 12;

			fixed (byte* ptr = retval.Array)
			{
				v = tmp[0];
				ptr[0 + I_0] = (byte)v;
				ptr[1 + I_0] = (byte)(v >> 8);
				ptr[2 + I_0] = (byte)(v >> 16);
				ptr[3 + I_0] = (byte)(v >> 24);

				v = tmp[1];
				ptr[0 + I_1] = (byte)v;
				ptr[1 + I_1] = (byte)(v >> 8);
				ptr[2 + I_1] = (byte)(v >> 16);
				ptr[3 + I_1] = (byte)(v >> 24);

				v = tmp[2];
				ptr[0 + I_2] = (byte)v;
				ptr[1 + I_2] = (byte)(v >> 8);
				ptr[2 + I_2] = (byte)(v >> 16);
				ptr[3 + I_2] = (byte)(v >> 24);

				v = tmp[3];
				ptr[0 + I_3] = (byte)v;
				ptr[1 + I_3] = (byte)(v >> 8);
				ptr[2 + I_3] = (byte)(v >> 16);
				ptr[3 + I_3] = (byte)(v >> 24);
			}

			return retval;
		}
        public unsafe static decimal ToDecimal(PooledSegment value)
        {
            if (value.Count != 16)
            {
                throw new ArgumentOutOfRangeException("value.Count", value.Count, "count must be == 16");
            }

            const int I_0 = 0;
            const int I_1 = 4;
            const int I_2 = 8;
            const int I_3 = 12;

            fixed(byte *ptr = value.Array)
            {
                var v0 = ptr[0 + I_0]
                         + (ptr[1 + I_0] << 8)
                         + (ptr[2 + I_0] << 16)
                         + (ptr[3 + I_0] << 24);

                var v1 = ptr[0 + I_1]
                         + (ptr[1 + I_1] << 8)
                         + (ptr[2 + I_1] << 16)
                         + (ptr[3 + I_1] << 24);

                var v2 = ptr[0 + I_2]
                         + (ptr[1 + I_2] << 8)
                         + (ptr[2 + I_2] << 16)
                         + (ptr[3 + I_2] << 24);

                var v3 = ptr[0 + I_3]
                         + (ptr[1 + I_3] << 8)
                         + (ptr[2 + I_3] << 16)
                         + (ptr[3 + I_3] << 24);

                return(new decimal(new int[] { v0, v1, v2, v3 }));
            }
        }
		public unsafe static PooledSegment GetBytes(IBufferAllocator allocator, long value)
		{
			var retval = new PooledSegment(allocator, 8);

			fixed (byte* ptr = retval.Array)
			{
				ptr[0] = (byte)value;
				ptr[1] = (byte)(value >> 8);
				ptr[2] = (byte)(value >> 16);
				ptr[3] = (byte)(value >> 24);
				ptr[4] = (byte)(value >> 32);
				ptr[5] = (byte)(value >> 40);
				ptr[6] = (byte)(value >> 48);
				ptr[7] = (byte)(value >> 56);
			}

			return retval;
		}
 public static char ToChar(PooledSegment value)
 {
     return((char)ToInt16(value));
 }
		public unsafe static double ToDouble(PooledSegment value)
		{
			var tmp = ToInt64(value);

			return *(&tmp);
		}
		public static ulong ToUInt64(PooledSegment value)
		{
			return (ulong)ToInt64(value);
		}
		public bool Equals(PooledSegment obj)
		{
			return obj.array == array && obj.count == count;
		}
		public unsafe static long ToInt64(PooledSegment value)
		{
			if (value.Count < 8)
				throw new ArgumentOutOfRangeException("value.Count", value.Count, "count must be >= 8");

			fixed (byte* ptr = value.Array)
			{
				return (ptr[0]
							+ (ptr[1] << 8)
							+ (ptr[2] << 16)
							+ (ptr[3] << 24)
							+ (ptr[4] << 32)
							+ (ptr[5] << 40)
							+ (ptr[6] << 48)
							+ (ptr[7] << 56));
			}
		}
		public unsafe static short ToInt16(PooledSegment value)
		{
			if (value.Count != 2)
				throw new ArgumentOutOfRangeException("value.Count", value.Count, "count must be == 2");

			fixed (byte* ptr = value.Array)
			{
				return (short)(ptr[0] + (ptr[1] << 8));
			}
		}
        public unsafe static double ToDouble(PooledSegment value)
        {
            var tmp = ToInt64(value);

            return(*(&tmp));
        }
        public unsafe static float ToSingle(PooledSegment value)
        {
            var tmp = ToInt32(value);

            return(*(&tmp));
        }
 public static ulong ToUInt64(PooledSegment value)
 {
     return((ulong)ToInt64(value));
 }
		private PooledSegment SerializeObject(object value)
		{
			using (var ms = new PooledMemoryStream(allocator))
			{
				new BinaryFormatter().Serialize(ms, value);

				var retval = new PooledSegment(allocator, (int)ms.Length);
				ms.Position = 0;
				ms.Read(retval.Array, 0, retval.Count);

				return retval;
			}
		}
 public static uint ToUInt32(PooledSegment value)
 {
     return((uint)ToInt32(value));
 }
 public static ushort ToUInt16(PooledSegment value)
 {
     return((ushort)ToInt16(value));
 }
		public static bool ToBoolean(PooledSegment value)
		{
			return value.Array[0] == TRUE;
		}
		public static char ToChar(PooledSegment value)
		{
			return (char)ToInt16(value);
		}
Esempio n. 29
0
 public bool Equals(PooledSegment obj)
 {
     return(obj.array == array && obj.count == count);
 }
		public unsafe static int ToInt32(PooledSegment value)
		{
			if (value.Count != 4)
				throw new ArgumentOutOfRangeException("value.Count", value.Count, "count must be == 4");

			fixed (byte* ptr = value.Array)
			{
				return (ptr[0]
							+ (ptr[1] << 8)
							+ (ptr[2] << 16)
							+ (ptr[3] << 24));
			}
		}
 private static string DeserializeString(PooledSegment value)
 {
     return(Encoding.UTF8.GetString(value.Array, 0, value.Count));
 }
		public static ushort ToUInt16(PooledSegment value)
		{
			return (ushort)ToInt16(value);
		}
 public static bool ToBoolean(PooledSegment value)
 {
     return(value.Array[0] == TRUE);
 }
		public static uint ToUInt32(PooledSegment value)
		{
			return (uint)ToInt32(value);
		}
		private static string DeserializeString(PooledSegment value)
		{
			return Encoding.UTF8.GetString(value.Array, 0, value.Count);
		}
		public unsafe static float ToSingle(PooledSegment value)
		{
			var tmp = ToInt32(value);

			return *(&tmp);
		}
        public CacheItem Serialize(object value)
        {
            // - or we just received a byte[]. No further processing is needed.
            var tmpByteArray = value as byte[];

            if (tmpByteArray != null)
            {
                return(new CacheItem(RawDataFlag, new PooledSegment(tmpByteArray, tmpByteArray.Length)));
            }

            // got some real data, serialize it
            var code = value == null
                                                ? TypeCode.DBNull
                                                : Type.GetTypeCode(value.GetType());

            PooledSegment data;

            switch (code)
            {
            case TypeCode.Empty:
            case TypeCode.DBNull: data = PooledSegment.Empty; break;

            case TypeCode.String: data = SerializeString((String)value); break;

            case TypeCode.SByte: data = PooledBitConverter.GetBytes(allocator, (SByte)value); break;

            case TypeCode.Byte: data = PooledBitConverter.GetBytes(allocator, (Byte)value); break;

            case TypeCode.Boolean: data = PooledBitConverter.GetBytes(allocator, (Boolean)value); break;

            case TypeCode.Char: data = PooledBitConverter.GetBytes(allocator, (Char)value); break;

            case TypeCode.Int16: data = PooledBitConverter.GetBytes(allocator, (Int16)value); break;

            case TypeCode.Int32: data = PooledBitConverter.GetBytes(allocator, (Int32)value); break;

            case TypeCode.Int64: data = PooledBitConverter.GetBytes(allocator, (Int64)value); break;

            case TypeCode.UInt16: data = PooledBitConverter.GetBytes(allocator, (UInt16)value); break;

            case TypeCode.UInt32: data = PooledBitConverter.GetBytes(allocator, (UInt32)value); break;

            case TypeCode.UInt64: data = PooledBitConverter.GetBytes(allocator, (UInt64)value); break;

            case TypeCode.DateTime: data = PooledBitConverter.GetBytes(allocator, ((DateTime)value).ToBinary()); break;

            case TypeCode.Single: data = PooledBitConverter.GetBytes(allocator, (Single)value); break;

            case TypeCode.Double: data = PooledBitConverter.GetBytes(allocator, (Double)value); break;

            case TypeCode.Decimal: data = PooledBitConverter.GetBytes(allocator, (Decimal)value); break;

            case TypeCode.Object:
                // raw data is a special case when someone passes in a buffer (byte[] or ArraySegment<byte>)
                // ArraySegment<byte> is only passed in when a part of buffer is being
                // serialized, usually from a MemoryStream (To avoid duplicating arrays
                // the byte[] returned by MemoryStream.GetBuffer is placed into an ArraySegment.)
                if (value is ArraySegment <byte> )
                {
                    return(new CacheItem(RawDataFlag, PooledSegment.From((ArraySegment <byte>)value)));
                }

                data = SerializeObject(value); break;

            default: throw new InvalidOperationException("Unknown TypeCode was returned: " + code);
            }

            return(new CacheItem((uint)((int)code | 0x100), data));
        }
		public unsafe static decimal ToDecimal(PooledSegment value)
		{
			if (value.Count != 16)
				throw new ArgumentOutOfRangeException("value.Count", value.Count, "count must be == 16");

			const int I_0 = 0;
			const int I_1 = 4;
			const int I_2 = 8;
			const int I_3 = 12;

			fixed (byte* ptr = value.Array)
			{
				var v0 = ptr[0 + I_0]
							+ (ptr[1 + I_0] << 8)
							+ (ptr[2 + I_0] << 16)
							+ (ptr[3 + I_0] << 24);

				var v1 = ptr[0 + I_1]
							+ (ptr[1 + I_1] << 8)
							+ (ptr[2 + I_1] << 16)
							+ (ptr[3 + I_1] << 24);

				var v2 = ptr[0 + I_2]
							+ (ptr[1 + I_2] << 8)
							+ (ptr[2 + I_2] << 16)
							+ (ptr[3 + I_2] << 24);

				var v3 = ptr[0 + I_3]
						+ (ptr[1 + I_3] << 8)
						+ (ptr[2 + I_3] << 16)
						+ (ptr[3 + I_3] << 24);

				return new decimal(new int[] { v0, v1, v2, v3 });
			}
		}
		private static object DeserializeObject(PooledSegment value)
		{
			using (var ms = new MemoryStream(value.Array, 0, value.Count))
			{
				return new BinaryFormatter().Deserialize(ms);
			}
		}
Esempio n. 40
0
 public CacheItem(uint flags, PooledSegment segment)
 {
     this.flags   = flags;
     this.segment = segment;
 }
Esempio n. 41
0
		public CacheItem(uint flags, PooledSegment segment)
		{
			this.flags = flags;
			this.segment = segment;
		}