Beispiel #1
0
        public void ReadObject_Objects_Externalizable_AMF3()
        {
            IExternalizable externalizableValue = Mocks.CreateMock <IExternalizable>();

            externalizableValue.ReadExternal(input);
            LastCall.Do((ReadExternalDelegate) delegate(IDataInput inputToUse)
            {
                // Note: inputToUse will be the same instance of AMFDataInput which we've already
                // tested so we don't need to try all combinations here.  Just a few as a sanity check.
                Assert.AreEqual("abc", inputToUse.ReadUTF());
                Assert.AreEqual(10, inputToUse.ReadInt());
                Assert.AreEqual(new ASString("def"), inputToUse.ReadObject());
            });

            Expect.Call(serializer.CreateExternalizableInstance("class")).Return(externalizableValue);

            Mocks.ReplayAll();

            SetStreamContents(new byte[] { (byte)AMF0ObjectTypeCode.AMF3Data, (byte)AMF3ObjectTypeCode.Object, 0x07,
                                           0x0b, 0x63, 0x6c, 0x61, 0x73, 0x73,                     // class def
                                           0x00, 0x03, 0x61, 0x62, 0x63,                           // write utf "abc"
                                           0x00, 0x00, 0x00, 0x0a,                                 // write int 10
                                           (byte)AMF3ObjectTypeCode.String, 0x07, 0x64, 0x65, 0x66 // write object "def"
                              });

            input.BeginObjectStream();
            ASExternalizableObject obj = (ASExternalizableObject)input.ReadObject();

            Assert.AreEqual(AMFObjectEncoding.AMF3, input.ObjectEncoding);
            input.EndObjectStream();

            Assert.AreEqual("class", obj.Class.ClassAlias);
            Assert.AreEqual(ASClassLayout.Externalizable, obj.Class.Layout);
            Assert.AreSame(externalizableValue, obj.ExternalizableValue);
        }
Beispiel #2
0
        internal object ReadAMF3Object(ClassDefinition classDefinition)
        {
            object instance = null;

            if (!string.IsNullOrEmpty(classDefinition.ClassName))
            {
                instance = ObjectFactory.CreateInstance(classDefinition.ClassName);
            }
            else
            {
                instance = new ASObject();
            }
            if (instance == null)
            {
#if !SILVERLIGHT
                if (log.IsWarnEnabled)
                {
                    log.Warn(__Res.GetString(__Res.TypeLoad_ASO, classDefinition.ClassName));
                }
#endif
                instance = new ASObject(classDefinition.ClassName);
            }
            AddAMF3ObjectReference(instance);
            if (classDefinition.IsExternalizable)
            {
                if (instance is IExternalizable)
                {
                    IExternalizable externalizable = instance as IExternalizable;
                    DataInput       dataInput      = new DataInput(this);
                    externalizable.ReadExternal(dataInput);
                }
                else
                {
                    string msg = __Res.GetString(__Res.Externalizable_CastFail, instance.GetType().FullName);
                    throw new FluorineException(msg);
                }
            }
            else
            {
                for (int i = 0; i < classDefinition.MemberCount; i++)
                {
                    string key   = classDefinition.Members[i].Name;
                    object value = ReadAMF3Data();
                    SetMember(instance, key, value);
                }
                if (classDefinition.IsDynamic)
                {
                    string key = ReadAMF3String();
                    while (key != null && key != string.Empty)
                    {
                        object value = ReadAMF3Data();
                        SetMember(instance, key, value);
                        key = ReadAMF3String();
                    }
                }
            }
            return(instance);
        }
Beispiel #3
0
        internal object ReadAMF3Object(ClassDefinition classDefinition)
        {
            //Console.WriteLine(classDefinition.ClassName + "=" + BaseStream.Position);

            object instance = null;

            if (!string.IsNullOrEmpty(classDefinition.ClassName))
            {
                instance = ObjectFactory.CreateInstance(classDefinition.ClassName);
            }
            else
            {
                instance = new ASObject();
            }
            if (instance == null)
            {
                instance = new ASObject(classDefinition.ClassName);
            }
            AddAMF3ObjectReference(instance);
            if (classDefinition.IsExternalizable)
            {
                if (instance is IExternalizable)
                {
                    IExternalizable externalizable = instance as IExternalizable;
                    DataInput       dataInput      = new DataInput(this);
                    externalizable.ReadExternal(dataInput);
                }
                else
                {
                    throw new Exception(classDefinition.ClassName + "是个IExternalizable类型");
                }
            }
            else
            {
                for (int i = 0; i < classDefinition.MemberCount; i++)
                {
                    string key = classDefinition.Members[i].Name;

                    object value = ReadAMF3Data();
                    SetMember(instance, key, value);
                }
                if (classDefinition.IsDynamic)
                {
                    string key = ReadAMF3String();
                    while (key != null && key != string.Empty)
                    {
                        object value = ReadAMF3Data();
                        SetMember(instance, key, value);
                        key = ReadAMF3String();
                    }
                }
            }
            return(instance);
        }
Beispiel #4
0
        internal object ReadAMF3Object(ClassDefinition classDefinition)
        {
            object instance = null;
            string name;
            object obj3;

            if ((classDefinition.ClassName != null) && (classDefinition.ClassName != string.Empty))
            {
                instance = ObjectFactory.CreateInstance(classDefinition.ClassName);
            }
            else
            {
                instance = new ASObject();
            }
            if (instance == null)
            {
                if (log.get_IsWarnEnabled())
                {
                    log.Warn(__Res.GetString("TypeLoad_ASO", new object[] { classDefinition.ClassName }));
                }
                instance = new ASObject(classDefinition.ClassName);
            }
            this.AddAMF3ObjectReference(instance);
            if (classDefinition.IsExternalizable)
            {
                if (!(instance is IExternalizable))
                {
                    throw new FluorineException(__Res.GetString("Externalizable_CastFail", new object[] { instance.GetType().FullName }));
                }
                IExternalizable externalizable = instance as IExternalizable;
                DataInput       input          = new DataInput(this);
                externalizable.ReadExternal(input);
                return(instance);
            }
            for (int i = 0; i < classDefinition.MemberCount; i++)
            {
                name = classDefinition.Members[i].Name;
                obj3 = this.ReadAMF3Data();
                this.SetMember(instance, name, obj3);
            }
            if (classDefinition.IsDynamic)
            {
                for (name = this.ReadAMF3String(); (name != null) && (name != string.Empty); name = this.ReadAMF3String())
                {
                    obj3 = this.ReadAMF3Data();
                    this.SetMember(instance, name, obj3);
                }
            }
            return(instance);
        }
Beispiel #5
0
        public object ReadData(AMFReader reader, ClassDefinition classDefinition)
        {
            object instance = ObjectFactory.CreateInstance(classDefinition.ClassName);

            if (instance == null)
            {
                throw new FluorineException(__Res.GetString("Type_InitError", new object[] { classDefinition.ClassName }));
            }
            reader.AddAMF3ObjectReference(instance);
            if (!(instance is IExternalizable))
            {
                throw new FluorineException(__Res.GetString("Externalizable_CastFail", new object[] { instance.GetType().FullName }));
            }
            IExternalizable externalizable = instance as IExternalizable;
            DataInput       input          = new DataInput(reader);

            externalizable.ReadExternal(input);
            return(instance);
        }
        public object ReadData(AMFReader reader, ClassDefinition classDefinition)
        {
            object instance = ObjectFactory.CreateInstance(classDefinition.ClassName);

            if (instance == null)
            {
            }
            reader.AddAMF3ObjectReference(instance);
            if (instance is IExternalizable)
            {
                IExternalizable externalizable = instance as IExternalizable;
                DataInput       dataInput      = new DataInput(reader);
                externalizable.ReadExternal(dataInput);
                return(instance);
            }
            else
            {
            }
            return(instance);
        }
Beispiel #7
0
        public object ReadData(AMFReader reader, ClassDefinition classDefinition)
        {
            object instance = ObjectFactory.CreateInstance(classDefinition.ClassName);

            if (instance == null)
            {
                string msg = __Res.GetString(__Res.Type_InitError, classDefinition.ClassName);
                throw new FluorineException(msg);
            }
            reader.AddAMF3ObjectReference(instance);
            if (instance is IExternalizable)
            {
                IExternalizable externalizable = instance as IExternalizable;
                DataInput       dataInput      = new DataInput(reader);
                externalizable.ReadExternal(dataInput);
                return(instance);
            }
            else
            {
                string msg = __Res.GetString(__Res.Externalizable_CastFail, instance.GetType().FullName, classDefinition.ClassName);
                throw new FluorineException(msg);
            }
        }
Beispiel #8
0
        private IASValue ReadObjectValue()
        {
            int bits = input.ReadVWInt29();

            // Handle cached objects.
            if ((bits & 1) == 0)
            {
                int referenceId = bits >> 1;
                return(GetObjectFromCache(AMF3ObjectTypeCode.Object, referenceId));
            }

            // Handle cached class definitions.
            ASClass classDefinition;

            if ((bits & 2) == 0)
            {
                int referenceId = bits >> 2;

                // Note: Object might be an ASExternalizableObject.
                IASValue obj = GetObjectFromCache(AMF3ObjectTypeCode.Object, referenceId);
                classDefinition = obj.Class;
            }
            else
            {
                // Read the class definition.
                ASClassLayout classLayout = (ASClassLayout)((bits & 0x0c) >> 2);
                if (classLayout > ASClassLayout.Dynamic)
                {
                    throw new AMFException(String.Format(CultureInfo.CurrentCulture,
                                                         ExceptionPrefix + "Encountered Object token with invalid class layout '{0}'.", classLayout));
                }

                int memberCount = bits >> 4;
                if (memberCount < 0)
                {
                    throw new AMFException(String.Format(CultureInfo.CurrentCulture,
                                                         ExceptionPrefix + "Encountered Object token with invalid member count '{0}'.", memberCount));
                }

                if (classLayout == ASClassLayout.Externalizable && memberCount != 0)
                {
                    throw new AMFException(String.Format(CultureInfo.CurrentCulture,
                                                         ExceptionPrefix + "Encountered Object token with Externalizable class layout and non-zero member count '{0}'.", memberCount));
                }

                string classAlias = ReadStringData();

                string[] memberNames;
                if (memberCount != 0)
                {
                    memberNames = new string[memberCount];

                    for (int i = 0; i < memberCount; i++)
                    {
                        memberNames[i] = ReadStringData();
                    }
                }
                else
                {
                    memberNames = EmptyArray <string> .Instance;
                }

                // Look up the class in the cache.
                classDefinition = ASClassCache.GetClass(classAlias, classLayout, memberNames);
            }

            // Read the instance data.
            if (classDefinition.Layout == ASClassLayout.Externalizable)
            {
                // Important: Add the object to the cache before deserializing its properties!
                ASExternalizableObject result = ASExternalizableObject.CreateUninitializedInstance(classDefinition);
                AddObjectToCache(AMF3ObjectTypeCode.Object, result);

                // Use custom serialization for the externalizable object.
                IExternalizable externalizableValue = input.Serializer.CreateExternalizableInstance(classDefinition.ClassAlias);
                externalizableValue.ReadExternal(input);

                result.SetProperties(externalizableValue);
                return(result);
            }
            else
            {
                // Important: Add the object to the cache before deserializing its properties!
                ASObject result = ASObject.CreateUninitializedInstance(classDefinition);
                AddObjectToCache(AMF3ObjectTypeCode.Object, result);

                // Read the member values.
                int        memberCount = classDefinition.MemberNames.Count;
                IASValue[] memberValues;

                if (memberCount != 0)
                {
                    memberValues = new IASValue[memberCount];

                    for (int i = 0; i < memberCount; i++)
                    {
                        memberValues[i] = ReadObject();
                    }
                }
                else
                {
                    memberValues = EmptyArray <IASValue> .Instance;
                }

                // Read the dynamic property values.
                IDictionary <string, IASValue> dynamicProperties;

                if (classDefinition.Layout == ASClassLayout.Dynamic)
                {
                    string key = ReadStringData();
                    if (key.Length != 0)
                    {
                        dynamicProperties = new Dictionary <string, IASValue>();

                        for (; ;)
                        {
                            IASValue value = ReadObject();
                            dynamicProperties.Add(key, value);

                            key = ReadStringData();
                            if (key.Length == 0)
                            {
                                break;
                            }
                        }
                    }
                    else
                    {
                        dynamicProperties = EmptyDictionary <string, IASValue> .Instance;
                    }
                }
                else
                {
                    dynamicProperties = EmptyDictionary <string, IASValue> .Instance;
                }

                result.SetProperties(memberValues, dynamicProperties);
                return(result);
            }
        }