Beispiel #1
0
            /// <exception cref="System.IO.IOException"/>
            public virtual void Write(BinaryWriter writer)
            {
                OutputStream os = DataOutputOutputStream.ConstructOutputStream(@out);

                ((Message)requestHeader).WriteDelimitedTo(os);
                theRequest.WriteDelimitedTo(os);
            }
Beispiel #2
0
            /// <exception cref="System.IO.IOException"/>
            public virtual void Write(BinaryWriter writer)
            {
                OutputStream os = DataOutputOutputStream.ConstructOutputStream(@out);

                theResponse.WriteDelimitedTo(os);
            }
Beispiel #3
0
 /// <summary>
 /// Write a
 /// <see cref="IWritable"/>
 /// ,
 /// <see cref="string"/>
 /// , primitive type, or an array of
 /// the preceding.
 /// </summary>
 /// <param name="allowCompactArrays">
 /// - set true for RPC and internal or intra-cluster
 /// usages.  Set false for inter-cluster, File, and other persisted output
 /// usages, to preserve the ability to interchange files with other clusters
 /// that may not be running the same version of software.  Sometime in ~2013
 /// we can consider removing this parameter and always using the compact format.
 /// </param>
 /// <exception cref="System.IO.IOException"/>
 public static void WriteObject(BinaryWriter writer, object instance, Type declaredClass
                                , Configuration conf, bool allowCompactArrays)
 {
     if (instance == null)
     {
         // null
         instance      = new ObjectWritable.NullInstance(declaredClass, conf);
         declaredClass = typeof(IWritable);
     }
     // Special case: must come before writing out the declaredClass.
     // If this is an eligible array of primitives,
     // wrap it in an ArrayPrimitiveWritable$Internal wrapper class.
     if (allowCompactArrays && declaredClass.IsArray && instance.GetType().FullName.Equals
             (declaredClass.FullName) && instance.GetType().GetElementType().IsPrimitive)
     {
         instance      = new ArrayPrimitiveWritable.Internal(instance);
         declaredClass = typeof(ArrayPrimitiveWritable.Internal);
     }
     UTF8.WriteString(@out, declaredClass.FullName);
     // always write declared
     if (declaredClass.IsArray)
     {
         // non-primitive or non-compact array
         int length = Runtime.GetArrayLength(instance);
         @out.WriteInt(length);
         for (int i = 0; i < length; i++)
         {
             WriteObject(@out, Runtime.GetArrayValue(instance, i), declaredClass.GetElementType
                             (), conf, allowCompactArrays);
         }
     }
     else
     {
         if (declaredClass == typeof(ArrayPrimitiveWritable.Internal))
         {
             ((ArrayPrimitiveWritable.Internal)instance).Write(@out);
         }
         else
         {
             if (declaredClass == typeof(string))
             {
                 // String
                 UTF8.WriteString(@out, (string)instance);
             }
             else
             {
                 if (declaredClass.IsPrimitive)
                 {
                     // primitive type
                     if (declaredClass == typeof(bool))
                     {
                         // boolean
                         @out.WriteBoolean(((bool)instance));
                     }
                     else
                     {
                         if (declaredClass == typeof(char))
                         {
                             // char
                             @out.WriteChar(((char)instance));
                         }
                         else
                         {
                             if (declaredClass == typeof(byte))
                             {
                                 // byte
                                 @out.WriteByte(((byte)instance));
                             }
                             else
                             {
                                 if (declaredClass == typeof(short))
                                 {
                                     // short
                                     @out.WriteShort(((short)instance));
                                 }
                                 else
                                 {
                                     if (declaredClass == typeof(int))
                                     {
                                         // int
                                         @out.WriteInt(((int)instance));
                                     }
                                     else
                                     {
                                         if (declaredClass == typeof(long))
                                         {
                                             // long
                                             @out.WriteLong(((long)instance));
                                         }
                                         else
                                         {
                                             if (declaredClass == typeof(float))
                                             {
                                                 // float
                                                 @out.WriteFloat(((float)instance));
                                             }
                                             else
                                             {
                                                 if (declaredClass == typeof(double))
                                                 {
                                                     // double
                                                     @out.WriteDouble(((double)instance));
                                                 }
                                                 else
                                                 {
                                                     if (declaredClass == typeof(void))
                                                     {
                                                     }
                                                     else
                                                     {
                                                         // void
                                                         throw new ArgumentException("Not a primitive: " + declaredClass);
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
                 else
                 {
                     if (declaredClass.IsEnum())
                     {
                         // enum
                         UTF8.WriteString(@out, ((Enum)instance).Name());
                     }
                     else
                     {
                         if (typeof(IWritable).IsAssignableFrom(declaredClass))
                         {
                             // Writable
                             UTF8.WriteString(@out, instance.GetType().FullName);
                             ((IWritable)instance).Write(@out);
                         }
                         else
                         {
                             if (typeof(Message).IsAssignableFrom(declaredClass))
                             {
                                 ((Message)instance).WriteDelimitedTo(DataOutputOutputStream.ConstructOutputStream
                                                                          (@out));
                             }
                             else
                             {
                                 throw new IOException("Can't write: " + instance + " as " + declaredClass);
                             }
                         }
                     }
                 }
             }
         }
     }
 }