Ejemplo n.º 1
0
        public virtual byte[] Encode(object obj)
        {
            object[] ary = (object[])obj;

            MemoryStream ms = new MemoryStream();
            BinaryWriter bw = new BinaryWriter(ms);


            for (int x = 0; x < ary.Length; x++)
            {
                try {
                    // This will throw an exception for any unsupported data types.
                    byte   tipe = DataField.GetType(ary[x]);
                    byte[] data = DataField.Encode(ary[x], tipe);
                    int    size = DataField.GetDataType(tipe).Size;

                    // Write the type field
                    bw.Write(tipe);

                    if (data != null)
                    {
                        // If the value is variable in length
                        if (size < 0)
                        {
                            // write the length as a u32 like ither variable field length types
                            bw.Write(ByteUtil.RenderUnsignedInt((uint)data.Length));
                        }

                        // write the value itself
                        bw.Write(data);
                    }
                    else
                    {
                        bw.Write(0);   // null value
                    }
                } catch (System.Exception) {
                    //System.err.println("Array object of type " + ary[x].getClass().getSimpleName() + " is not supported in DataFrames");
                    // just skip the offending object and add the rest.
                }
            } // for each

            return(ms.ToArray());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Write the field to the output stream.
        /// </summary>
        /// <param name="bw">The binary writer on which the field is to be written.</param>
        public virtual void Write(BinaryWriter bw)
        {
            // If we have a name...
            if (name != null)
            {
                // write the length and name fields
                byte[] nameField  = strEnc.GetBytes(name);
                int    nameLength = nameField.Length;
                bw.Write((byte)nameLength);
                bw.Write(nameField);
            }
            else
            {
                // indicate a name field length of 0
                bw.Write((byte)0);
            }

            // Write the type field
            bw.Write((byte)type);

            if (value != null)
            {
                FieldType datatype = GetDataType(type);

                // If the value is variable in length
                if (datatype.Size < 0)
                {
                    // write the length as a U32 bit value (even though it will
                    // never be larger than a S32)
                    bw.Write(ByteUtil.RenderUnsignedInt((uint)value.Length));
                }

                // write the value itself
                bw.Write(value);
            }
            else
            {
                bw.Write((ushort)0);
            }

            return;
        }
Ejemplo n.º 3
0
 public virtual byte[] Encode(object obj)
 {
     return(ByteUtil.RenderUnsignedInt((uint)obj));
 }