public void ReadExternal(IPofReader reader)
        {
            Assert.AreEqual(INTEGER, reader.ReadInt32(0));
            IPofReader nested1 = reader.CreateNestedPofReader(1);

            IPofReader nested2 = nested1.CreateNestedPofReader(0);

            Assert.AreEqual(STRING, nested2.ReadString(0));
            var person2 = (PortablePerson)nested2.ReadObject(1);

            double[] doubleArray = nested2.ReadDoubleArray(2);
            Assert.AreEqual(ArrayEqual(DOUBLE_ARRAY, doubleArray), true);

            IPofReader nested3     = nested2.CreateNestedPofReader(3);
            var        stringArray = (String[])nested3.ReadArray(0, new String[0]);

            Assert.IsTrue(ArrayEqual(stringArray, STRING_ARRAY));
            nested3.ReadRemainder();

            // close nested3 and continue to nested2
            bool boolVal = nested2.ReadBoolean(4);

            Assert.AreEqual(false, boolVal);

            // nested1
            ICollection          col     = nested1.ReadCollection(1, null);
            ICollection <String> results = new Collection <String>();

            foreach (object res in col)
            {
                results.Add((string)res);
            }
            foreach (String val in set)
            {
                Assert.IsTrue(results.Contains(val));
            }

            Assert.AreEqual(2.0, nested1.ReadDouble(2));
            Assert.AreEqual(5, nested1.ReadInt32(3));

            results = nested1.ReadCollection(4, results);
            foreach (String val in set)
            {
                Assert.IsTrue(results.Contains(val));
            }

            var person1 = (PortablePerson)nested1.ReadObject(5);

            Assert.AreEqual(2.222, nested1.ReadDouble(10));

            nested1.ReadRemainder();

            Assert.AreEqual(4.444, reader.ReadDouble(2));
            Assert.AreEqual(15, reader.ReadInt32(3));
            var person = (PortablePerson)reader.ReadObject(4);

            Assert.IsTrue(person == person1);
            Assert.IsTrue(person1 == person2);
        }
        /// <summary>
        /// Deserialize a user type instance from a POF stream by reading its
        /// state using the specified <see cref="IPofReader"/> object.
        /// </summary>
        /// <remarks>
        /// An implementation of <b>IPofSerializer</b> is required to follow
        /// the following steps in sequence for reading in an object of a
        /// user type:
        /// <list type="number">
        /// <item>
        /// <description>
        /// If the object is evolvable, the implementation must get the
        /// version by calling <see cref="IPofReader.VersionId"/>.
        /// </description>
        /// </item>
        /// <item>
        /// <description>
        /// The implementation may read any combination of the
        /// properties of the user type by using "read" methods of the
        /// <b>IPofReader</b>, but it must do so in the order of the property
        /// indexes. Additionally, the implementation must call
        /// {@link IPofReader#RegisterIdentity} with the new instance prior
        /// to reading any properties which are user type instances
        /// themselves.
        /// </description>
        /// </item>
        /// <item>
        /// <description>
        /// After all desired properties of the user type have been read,
        /// the implementation must terminate the reading of the user type by
        /// calling <see cref="IPofReader.ReadRemainder"/>.
        /// </description>
        /// </item>
        /// </list>
        /// </remarks>
        /// <param name="pofReader">
        /// The <b>IPofReader</b> with which to read the object's state.
        /// </param>
        /// <returns>
        /// The deserialized user type instance.
        /// </returns>
        /// <exception cref="IOException">
        /// If an I/O error occurs.
        /// </exception>
        public Object Deserialize(IPofReader pofReader)
        {
            try
            {
                IPortableObject po = (IPortableObject)
                                     Activator.CreateInstance(GetTypeForTypeId(pofReader.PofContext, m_nTypeId));

                CacheFactory.Log("Deserializing " + po.GetType(), CacheFactory.LogLevel.Max);

                bool             fEvolvable = po is IEvolvableObject;
                IEvolvableObject et         = fEvolvable ? (IEvolvableObject)po : null;

                int typeId = ((PofStreamReader.UserTypeReader)pofReader).NextPropertyIndex;
                while (typeId > 0)
                {
                    IEvolvable e      = null;
                    IPofReader reader = pofReader.CreateNestedPofReader(typeId);

                    if (fEvolvable)
                    {
                        e             = et.GetEvolvable(typeId);
                        e.DataVersion = reader.VersionId;
                    }

                    po.ReadExternal(reader);

                    Binary binRemainder = reader.ReadRemainder();
                    if (fEvolvable)
                    {
                        e.FutureData = binRemainder;
                    }
                    typeId = ((PofStreamReader.UserTypeReader)pofReader).NextPropertyIndex;
                }

                pofReader.ReadRemainder();
                return(po);
            }
            catch (Exception e)
            {
                String sClass = null;
                try
                {
                    sClass = pofReader.PofContext.GetTypeName(m_nTypeId);
                }
                catch (Exception)
                {
                }

                throw new IOException(
                          "An exception occurred instantiating a IPortableObject"
                          + " user type from a POF stream: type-id=" + m_nTypeId
                          + (sClass == null ? "" : ", class-name=" + sClass)
                          + ", exception=\n" + e, e);
            }
        }
Esempio n. 3
0
        public void ReadExternal(IPofReader reader)
        {
            Assert.AreEqual(0, reader.ReadInt32(0));
            Assert.AreEqual(1, reader.ReadInt32(1));
            Assert.AreEqual(2, reader.ReadInt32(2));
            IPofReader reader2 = reader.CreateNestedPofReader(3);

            Assert.AreEqual(0, reader2.ReadInt32(0));
            Assert.AreEqual(1, reader2.ReadInt32(1));
            Assert.AreEqual(2, reader2.ReadInt32(2));
            Assert.AreEqual(4, reader.ReadInt32(4));
            Assert.AreEqual(5, reader.ReadInt32(5));
        }