public virtual void TestMany()
 {
     //Write a big set of data, one of each primitive type array
     foreach (object x in bigSet)
     {
         //write each test object two ways
         //First, transparently via ObjectWritable
         ObjectWritable.WriteObject(@out, x, x.GetType(), null, true);
         //Second, explicitly via ArrayPrimitiveWritable
         (new ArrayPrimitiveWritable(x)).Write(@out);
     }
     //Now read the data back in
     @in.Reset(@out.GetData(), @out.GetLength());
     for (int x_1 = 0; x_1 < resultSet.Length;)
     {
         //First, transparently
         resultSet[x_1++] = ObjectWritable.ReadObject(@in, null);
         //Second, explicitly
         ArrayPrimitiveWritable apw = new ArrayPrimitiveWritable();
         apw.ReadFields(@in);
         resultSet[x_1++] = apw.Get();
     }
     //validate data structures and values
     Assert.Equal(expectedResultSet.Length, resultSet.Length);
     for (int x_2 = 0; x_2 < resultSet.Length; x_2++)
     {
         Assert.Equal("ComponentType of array " + x_2, expectedResultSet
                      [x_2].GetType().GetElementType(), resultSet[x_2].GetType().GetElementType());
     }
     Assert.True("In and Out arrays didn't match values", Arrays.DeepEquals
                     (expectedResultSet, resultSet));
 }
Beispiel #2
0
        /// <exception cref="System.IO.IOException"/>
        public virtual void TestSerializeAndDeserializeNull()
        {
            bool gotException = false;

            try
            {
                new EnumSetWritable <TestEnumSetWritable.TestEnumSet>(null);
            }
            catch (RuntimeException)
            {
                gotException = true;
            }
            Assert.True("Instantiation of empty EnumSetWritable with no element type class "
                        + "provided should throw exception", gotException);
            EnumSetWritable <TestEnumSetWritable.TestEnumSet> nullFlagWritable = new EnumSetWritable
                                                                                 <TestEnumSetWritable.TestEnumSet>(null, typeof(TestEnumSetWritable.TestEnumSet));
            DataOutputBuffer @out = new DataOutputBuffer();

            ObjectWritable.WriteObject(@out, nullFlagWritable, nullFlagWritable.GetType(), null
                                       );
            DataInputBuffer @in = new DataInputBuffer();

            @in.Reset(@out.GetData(), @out.GetLength());
            EnumSet <TestEnumSetWritable.TestEnumSet> read = ((EnumSetWritable <TestEnumSetWritable.TestEnumSet
                                                                                >)ObjectWritable.ReadObject(@in, null)).Get();

            Assert.Equal(read, null);
        }
        public virtual void TestOldFormat()
        {
            //Make sure we still correctly write the old format if desired.
            //Write the data array with old ObjectWritable API
            //which will set allowCompactArrays false.
            ObjectWritable.WriteObject(@out, i, i.GetType(), null);
            //Get ready to read it back
            @in.Reset(@out.GetData(), @out.GetLength());
            //Read the int[] object as written by ObjectWritable, but
            //"going around" ObjectWritable
            string className = UTF8.ReadString(@in);

            Assert.Equal("The int[] written by ObjectWritable as a non-compact array "
                         + "was not labelled as an array of int", i.GetType().FullName, className);
            int length = @in.ReadInt();

            Assert.Equal("The int[] written by ObjectWritable as a non-compact array "
                         + "was not expected length", i.Length, length);
            int[] readValue = new int[length];
            try
            {
                for (int i = 0; i < length; i++)
                {
                    readValue[i] = (int)((int)ObjectWritable.ReadObject(@in, null));
                }
            }
            catch (Exception e)
            {
                Fail("The int[] written by ObjectWritable as a non-compact array " + "was corrupted.  Failed to correctly read int[] of length "
                     + length + ". Got exception:\n" + StringUtils.StringifyException(e));
            }
            Assert.True("The int[] written by ObjectWritable as a non-compact array "
                        + "was corrupted.", Arrays.Equals(i, readValue));
        }
Beispiel #4
0
        /// <summary>
        /// Write a protobuf to a buffer 'numProtos' times, and then
        /// read them back, making sure all data comes through correctly.
        /// </summary>
        /// <exception cref="System.IO.IOException"/>
        private void DoTest(int numProtos)
        {
            Configuration    conf = new Configuration();
            DataOutputBuffer @out = new DataOutputBuffer();

            // Write numProtos protobufs to the buffer
            Message[] sent = new Message[numProtos];
            for (int i = 0; i < numProtos; i++)
            {
                // Construct a test protocol buffer using one of the
                // protos that ships with the protobuf library
                Message testProto = ((DescriptorProtos.EnumValueDescriptorProto)DescriptorProtos.EnumValueDescriptorProto
                                     .NewBuilder().SetName("test" + i).SetNumber(i).Build());
                ObjectWritable.WriteObject(@out, testProto, typeof(DescriptorProtos.EnumValueDescriptorProto
                                                                   ), conf);
                sent[i] = testProto;
            }
            // Read back the data
            DataInputBuffer @in = new DataInputBuffer();

            @in.Reset(@out.GetData(), @out.GetLength());
            for (int i_1 = 0; i_1 < numProtos; i_1++)
            {
                Message received = (Message)ObjectWritable.ReadObject(@in, conf);
                Assert.Equal(sent[i_1], received);
            }
        }
Beispiel #5
0
        /// <exception cref="System.IO.IOException"/>
        public virtual void TestSerializeAndDeserializeNonEmpty()
        {
            DataOutputBuffer @out = new DataOutputBuffer();

            ObjectWritable.WriteObject(@out, nonEmptyFlagWritable, nonEmptyFlagWritable.GetType
                                           (), null);
            DataInputBuffer @in = new DataInputBuffer();

            @in.Reset(@out.GetData(), @out.GetLength());
            EnumSet <TestEnumSetWritable.TestEnumSet> read = ((EnumSetWritable <TestEnumSetWritable.TestEnumSet
                                                                                >)ObjectWritable.ReadObject(@in, null)).Get();

            Assert.Equal(read, nonEmptyFlag);
        }
        public virtual void TestObjectLabeling()
        {
            //Do a few tricky experiments to make sure things are being written
            //the way we expect
            //Write the data array with ObjectWritable
            //which will indirectly write it using APW.Internal
            ObjectWritable.WriteObject(@out, i, i.GetType(), null, true);
            //Write the corresponding APW directly with ObjectWritable
            ArrayPrimitiveWritable apw = new ArrayPrimitiveWritable(i);

            ObjectWritable.WriteObject(@out, apw, apw.GetType(), null, true);
            //Get ready to read it back
            @in.Reset(@out.GetData(), @out.GetLength());
            //Read the int[] object as written by ObjectWritable, but
            //"going around" ObjectWritable
            string className = UTF8.ReadString(@in);

            Assert.Equal("The int[] written by ObjectWritable was not labelled as "
                         + "an ArrayPrimitiveWritable.Internal", typeof(ArrayPrimitiveWritable.Internal)
                         .FullName, className);
            ArrayPrimitiveWritable.Internal apwi = new ArrayPrimitiveWritable.Internal();
            apwi.ReadFields(@in);
            Assert.Equal("The ArrayPrimitiveWritable.Internal component type was corrupted"
                         , typeof(int), apw.GetComponentType());
            Assert.True("The int[] written by ObjectWritable as " + "ArrayPrimitiveWritable.Internal was corrupted"
                        , Arrays.Equals(i, (int[])(apwi.Get())));
            //Read the APW object as written by ObjectWritable, but
            //"going around" ObjectWritable
            string declaredClassName = UTF8.ReadString(@in);

            Assert.Equal("The APW written by ObjectWritable was not labelled as "
                         + "declaredClass ArrayPrimitiveWritable", typeof(ArrayPrimitiveWritable).FullName
                         , declaredClassName);
            className = UTF8.ReadString(@in);
            Assert.Equal("The APW written by ObjectWritable was not labelled as "
                         + "class ArrayPrimitiveWritable", typeof(ArrayPrimitiveWritable).FullName, className
                         );
            ArrayPrimitiveWritable apw2 = new ArrayPrimitiveWritable();

            apw2.ReadFields(@in);
            Assert.Equal("The ArrayPrimitiveWritable component type was corrupted"
                         , typeof(int), apw2.GetComponentType());
            Assert.True("The int[] written by ObjectWritable as " + "ArrayPrimitiveWritable was corrupted"
                        , Arrays.Equals(i, (int[])(apw2.Get())));
        }
Beispiel #7
0
        /// <summary>
        /// Read a
        /// <see cref="IWritable"/>
        /// ,
        /// <see cref="string"/>
        /// , primitive type, or an array of
        /// the preceding.
        /// </summary>
        /// <exception cref="System.IO.IOException"/>
        public static object ReadObject(BinaryReader reader, ObjectWritable objectWritable, Configuration
                                        conf)
        {
            string className     = UTF8.ReadString(@in);
            Type   declaredClass = PrimitiveNames[className];

            if (declaredClass == null)
            {
                declaredClass = LoadClass(conf, className);
            }
            object instance;

            if (declaredClass.IsPrimitive)
            {
                // primitive types
                if (declaredClass == typeof(bool))
                {
                    // boolean
                    instance = Extensions.ValueOf(@in.ReadBoolean());
                }
                else
                {
                    if (declaredClass == typeof(char))
                    {
                        // char
                        instance = char.ValueOf(@in.ReadChar());
                    }
                    else
                    {
                        if (declaredClass == typeof(byte))
                        {
                            // byte
                            instance = byte.ValueOf(@in.ReadByte());
                        }
                        else
                        {
                            if (declaredClass == typeof(short))
                            {
                                // short
                                instance = short.ValueOf(@in.ReadShort());
                            }
                            else
                            {
                                if (declaredClass == typeof(int))
                                {
                                    // int
                                    instance = Extensions.ValueOf(@in.ReadInt());
                                }
                                else
                                {
                                    if (declaredClass == typeof(long))
                                    {
                                        // long
                                        instance = Extensions.ValueOf(@in.ReadLong());
                                    }
                                    else
                                    {
                                        if (declaredClass == typeof(float))
                                        {
                                            // float
                                            instance = float.ValueOf(@in.ReadFloat());
                                        }
                                        else
                                        {
                                            if (declaredClass == typeof(double))
                                            {
                                                // double
                                                instance = double.ValueOf(@in.ReadDouble());
                                            }
                                            else
                                            {
                                                if (declaredClass == typeof(void))
                                                {
                                                    // void
                                                    instance = null;
                                                }
                                                else
                                                {
                                                    throw new ArgumentException("Not a primitive: " + declaredClass);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                if (declaredClass.IsArray)
                {
                    // array
                    int length = @in.ReadInt();
                    instance = System.Array.CreateInstance(declaredClass.GetElementType(), length);
                    for (int i = 0; i < length; i++)
                    {
                        Runtime.SetArrayValue(instance, i, ReadObject(@in, conf));
                    }
                }
                else
                {
                    if (declaredClass == typeof(ArrayPrimitiveWritable.Internal))
                    {
                        // Read and unwrap ArrayPrimitiveWritable$Internal array.
                        // Always allow the read, even if write is disabled by allowCompactArrays.
                        ArrayPrimitiveWritable.Internal temp = new ArrayPrimitiveWritable.Internal();
                        temp.ReadFields(@in);
                        instance      = temp.Get();
                        declaredClass = instance.GetType();
                    }
                    else
                    {
                        if (declaredClass == typeof(string))
                        {
                            // String
                            instance = UTF8.ReadString(@in);
                        }
                        else
                        {
                            if (declaredClass.IsEnum())
                            {
                                // enum
                                instance = Enum.ValueOf((Type)declaredClass, UTF8.ReadString(@in));
                            }
                            else
                            {
                                if (typeof(Message).IsAssignableFrom(declaredClass))
                                {
                                    instance = TryInstantiateProtobuf(declaredClass, @in);
                                }
                                else
                                {
                                    // Writable
                                    Type   instanceClass = null;
                                    string str           = UTF8.ReadString(@in);
                                    instanceClass = LoadClass(conf, str);
                                    IWritable writable = WritableFactories.NewInstance(instanceClass, conf);
                                    writable.ReadFields(@in);
                                    instance = writable;
                                    if (instanceClass == typeof(ObjectWritable.NullInstance))
                                    {
                                        // null
                                        declaredClass = ((ObjectWritable.NullInstance)instance).declaredClass;
                                        instance      = null;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (objectWritable != null)
            {
                // store values
                objectWritable.declaredClass = declaredClass;
                objectWritable.instance      = instance;
            }
            return(instance);
        }