예제 #1
0
        public override object Read(CompactBinaryReader reader)
        {
            int         length = reader.ReadInt32();
            IDictionary dict   = (IDictionary)CreateInstance();

            for (int i = 0; i < length; i++)
            {
                object key   = reader.Read();
                object value = reader.Read();
                dict.Add(key, value);
            }
            return(dict);
        }
예제 #2
0
        /// <summary>
        /// Converts the given <see cref="Stream"/> into a list of <see cref="RelayMessage"/>.
        /// </summary>
        /// <param name="stream">The given <see cref="Stream"/></param>
        /// <param name="evaluateMethod">A method to evaluate each <see cref="RelayMessage"/> as it's deserialized.</param>
        /// <returns>A list of <see cref="RelayMessage"/>.</returns>
        public static List <RelayMessage> ReadRelayMessageList(Stream stream, Action <RelayMessage> evaluateMethod)
        {
            BinaryReader        bread    = new BinaryReader(stream);
            CompactBinaryReader br       = new CompactBinaryReader(bread);
            int objectCount              = br.ReadInt32();
            List <RelayMessage> messages = new List <RelayMessage>(objectCount);

            for (int i = 0; i < objectCount; i++)
            {
                RelayMessage nextMessage = new RelayMessage();
                try
                {
                    br.Read <RelayMessage>(nextMessage, false);
                }
                catch (SerializationException exc)
                {
                    //try and add some context to this object
                    //Id and TypeId most likely got correctly deserialized so we're providing that much
                    string message = string.Format("Deserialization failed for RelayMessage of Id='{0}', ExtendedId='{1}', TypeId='{2}' and StreamLength='{3}'",
                                                   nextMessage.Id, Algorithm.ToHex(nextMessage.ExtendedId), nextMessage.TypeId, stream.Length);
                    SerializationException newException = new SerializationException(message, exc);
                    throw newException;
                }
                messages.Add(nextMessage);
                if (evaluateMethod != null)
                {
                    evaluateMethod(nextMessage);
                }
            }
            return(messages);
        }
예제 #3
0
        public override object Read(CompactBinaryReader reader)
        {
            int   length = reader.ReadInt32();
            IList list   = (IList)CreateInstance();

            for (int i = 0; i < length; i++)
            {
                list.Add(reader.Read());
            }
            return(list);
        }
예제 #4
0
        public override object Read(CompactBinaryReader reader)
        {
            int   length = reader.ReadInt32();
            Array array  = (Array)CreateInstance(length);

            for (int i = 0; i < length; i++)
            {
                array.SetValue(reader.Read(), i);
            }
            return(array);
        }
예제 #5
0
        public override object Read(CompactBinaryReader reader)
        {
            int length = reader.ReadInt32();

            object[] array = SafeMemoryAllocator.CreateArray <object>(length);
            for (int i = 0; i < length; i++)
            {
                array[i] = reader.Read();
            }
            return(array);
        }
예제 #6
0
        public void CanReadAndWriteCompactBinary()
        {
            using (var stream = new MemoryStream())
                using (var writerStream = new WriterStream(stream))
                {
                    var writer = writerStream.CreateCompactBinaryWriter();
                    writer.Write(this.testObject);

                    stream.Position = 0;
                    using (var reader = ReaderStream.FromMemoryStreamBuffer(stream, this.mgr))
                    {
                        var cbReader      = new CompactBinaryReader <ReaderStream>(reader);
                        var anotherObject = cbReader.Read <AnotherDerivedThing>();

                        Assert.IsTrue(Comparer.Equal(anotherObject, this.testObject));
                    }
                }
        }
		/// <summary>
		/// Converts the given <see cref="Stream"/> into a list of <see cref="RelayMessage"/>.
		/// </summary>
		/// <param name="stream">The given <see cref="Stream"/></param>
		/// <param name="evaluateMethod">A method to evaluate each <see cref="RelayMessage"/> as it's deserialized.</param>
		/// <returns>A list of <see cref="RelayMessage"/>.</returns>
		public static List<RelayMessage> ReadRelayMessageList(Stream stream, Action<RelayMessage> evaluateMethod)
		{
			BinaryReader bread = new BinaryReader(stream);
			CompactBinaryReader br = new CompactBinaryReader(bread);
			int objectCount = br.ReadInt32();
			List<RelayMessage> messages = new List<RelayMessage>(objectCount);
			for (int i = 0; i < objectCount; i++)
			{
				RelayMessage nextMessage = new RelayMessage();
				try
				{
					br.Read<RelayMessage>(nextMessage, false);
				}
				catch (SerializationException exc)
				{
					//try and add some context to this object
					//Id and TypeId most likely got correctly deserialized so we're providing that much
					string message = string.Format("Deserialization failed for RelayMessage of Id='{0}', ExtendedId='{1}', TypeId='{2}' and StreamLength='{3}'",
						nextMessage.Id, Algorithm.ToHex(nextMessage.ExtendedId), nextMessage.TypeId, stream.Length);
					SerializationException newException = new SerializationException(message, exc);
					throw newException;
				}
				messages.Add(nextMessage);
				if (evaluateMethod != null) evaluateMethod(nextMessage);
			}
			return messages;
		}
		public override object Read(CompactBinaryReader reader)
		{
			int length = reader.ReadInt32();
			object[] array = SafeMemoryAllocator.CreateArray<object>(length);
			for (int i = 0; i < length; i++) array[i] = reader.Read();
			return array;
		}
		public override object Read(CompactBinaryReader reader)
		{
			int length = reader.ReadInt32();
			IDictionary dict = (IDictionary)CreateInstance();
			for (int i = 0; i < length; i++)
			{
				object key = reader.Read();
				object value = reader.Read();
				dict.Add(key, value);
			}
			return dict;
		}
		public override object Read(CompactBinaryReader reader)
		{
			int length = reader.ReadInt32();
			IList list = (IList)CreateInstance();
			for (int i = 0; i < length; i++)
				list.Add(reader.Read());
			return list;
		}
		public override object Read(CompactBinaryReader reader)
		{
			int length = reader.ReadInt32();
			Array array = (Array)CreateInstance(length);
			for (int i = 0; i < length; i++)
				array.SetValue(reader.Read(), i);
			return array;
		}