Exemple #1
0
 /// <summary>
 /// Decode the field into an object reference.
 /// </summary>
 /// <param name="typ">the type of data in the value array</param>
 /// <param name="val">the binary representation of the data</param>
 /// <returns>the object value of the data encoded in the value attribute.</returns>
 private object GetObjectValue(byte typ, byte[] val)
 {
     if (val == null)
     {
         return(null);
     }
     else if (val.Length == 0)
     {
         if (typ == FRAME)
         {
             // empty value in a frame indicates an empty frame
             return(new DataFrame());
         }
         else
         {
             // an empty value for any other type indicates a null object
             return(null);
         }
     }
     else
     {
         FieldType datatype = GetDataType(typ);
         return(datatype.Decode(val));
     }
 }
Exemple #2
0
        public virtual object Decode(byte[] value)
        {
            object[]      retval   = EMPTY_ARRAY;
            List <object> elements = new List <object>();
            byte          type     = 0;

            byte[]    data     = null;
            FieldType datatype = null;

            if (value != null)
            {
                try {
                    using (MemoryStream stream = new MemoryStream(value)) {
                        using (BinaryReader reader = new BinaryReader(stream)) {
                            while (reader.BaseStream.Position != reader.BaseStream.Length)
                            {
                                // the next field we read is the data type
                                type = reader.ReadByte();

                                // get the proper field type
                                try {
                                    datatype = DataField.GetDataType(type);
                                } catch (System.Exception) {
                                    throw new IOException("non supported type: '" + type + "'");
                                }

                                // if the file type is a variable length (i.e. size < 0), read in the length
                                if (datatype.Size < 0)
                                {
                                    // Read the next 4 bytes
                                    byte[] u32 = reader.ReadBytes(4);

                                    // ...and convert it to a length using big-endian encoding
                                    uint length = ByteUtil.RetrieveUnsignedInt(u32, 0);

                                    if (length < 0)
                                    {
                                        throw new IOException("read length bad value: length = " + length + " type = " + type);
                                    }

                                    data = new byte[length];

                                    if (length > 0)
                                    {
                                        reader.Read(data, 0, data.Length);
                                    }
                                }
                                else
                                {
                                    data = new byte[datatype.Size];
                                    reader.Read(data, 0, data.Length);
                                }

                                // now get the object value of the data
                                elements.Add(datatype.Decode(data));
                            } // while there is data available to read

                            retval = elements.ToArray();
                        }
                    }
                } catch (Exception e) {
                    throw new ArgumentException("Could not decode value", e);
                }
            }

            return(retval);
        }