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

			return retval;
		}
コード例 #3
0
        public static PooledSegment GetBytes(IBufferAllocator allocator, bool value)
        {
            var retval = new PooledSegment(allocator, 1);

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

            return(retval);
        }
コード例 #4
0
        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)));
            }
        }
コード例 #5
0
        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);
        }
コード例 #6
0
		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;
		}
コード例 #7
0
        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));
            }
        }
コード例 #8
0
        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);
            }
        }
コード例 #9
0
        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));
            }
        }
コード例 #10
0
        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);
        }
コード例 #11
0
        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);
        }
コード例 #12
0
		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;
		}
コード例 #13
0
        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 }));
            }
        }
コード例 #14
0
		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;
		}
コード例 #15
0
 public static char ToChar(PooledSegment value)
 {
     return((char)ToInt16(value));
 }
コード例 #16
0
		public unsafe static double ToDouble(PooledSegment value)
		{
			var tmp = ToInt64(value);

			return *(&tmp);
		}
コード例 #17
0
		public static ulong ToUInt64(PooledSegment value)
		{
			return (ulong)ToInt64(value);
		}
コード例 #18
0
		public bool Equals(PooledSegment obj)
		{
			return obj.array == array && obj.count == count;
		}
コード例 #19
0
		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));
			}
		}
コード例 #20
0
		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));
			}
		}
コード例 #21
0
        public unsafe static double ToDouble(PooledSegment value)
        {
            var tmp = ToInt64(value);

            return(*(&tmp));
        }
コード例 #22
0
        public unsafe static float ToSingle(PooledSegment value)
        {
            var tmp = ToInt32(value);

            return(*(&tmp));
        }
コード例 #23
0
 public static ulong ToUInt64(PooledSegment value)
 {
     return((ulong)ToInt64(value));
 }
コード例 #24
0
		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;
			}
		}
コード例 #25
0
 public static uint ToUInt32(PooledSegment value)
 {
     return((uint)ToInt32(value));
 }
コード例 #26
0
 public static ushort ToUInt16(PooledSegment value)
 {
     return((ushort)ToInt16(value));
 }
コード例 #27
0
		public static bool ToBoolean(PooledSegment value)
		{
			return value.Array[0] == TRUE;
		}
コード例 #28
0
		public static char ToChar(PooledSegment value)
		{
			return (char)ToInt16(value);
		}
コード例 #29
0
 public bool Equals(PooledSegment obj)
 {
     return(obj.array == array && obj.count == count);
 }
コード例 #30
0
		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));
			}
		}
コード例 #31
0
 private static string DeserializeString(PooledSegment value)
 {
     return(Encoding.UTF8.GetString(value.Array, 0, value.Count));
 }
コード例 #32
0
		public static ushort ToUInt16(PooledSegment value)
		{
			return (ushort)ToInt16(value);
		}
コード例 #33
0
 public static bool ToBoolean(PooledSegment value)
 {
     return(value.Array[0] == TRUE);
 }
コード例 #34
0
		public static uint ToUInt32(PooledSegment value)
		{
			return (uint)ToInt32(value);
		}
コード例 #35
0
		private static string DeserializeString(PooledSegment value)
		{
			return Encoding.UTF8.GetString(value.Array, 0, value.Count);
		}
コード例 #36
0
		public unsafe static float ToSingle(PooledSegment value)
		{
			var tmp = ToInt32(value);

			return *(&tmp);
		}
コード例 #37
0
        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));
        }
コード例 #38
0
		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 });
			}
		}
コード例 #39
0
		private static object DeserializeObject(PooledSegment value)
		{
			using (var ms = new MemoryStream(value.Array, 0, value.Count))
			{
				return new BinaryFormatter().Deserialize(ms);
			}
		}
コード例 #40
0
 public CacheItem(uint flags, PooledSegment segment)
 {
     this.flags   = flags;
     this.segment = segment;
 }
コード例 #41
0
		public CacheItem(uint flags, PooledSegment segment)
		{
			this.flags = flags;
			this.segment = segment;
		}