示例#1
0
        public byte[] Encode(IFieldValue value, Group template, Context context)
        {
            if (value == null)
            {
                return(ByteUtil.EmptyByteArray);
            }

            var groupValue = (GroupValue)value;

            if (context.TraceEnabled)
            {
                context.EncodeTrace.GroupStart(this);
            }
            var presenceMapBuilder = new BitVectorBuilder(template.MaxPresenceMapSize);

            try
            {
                var fieldEncodings = new byte[_fields.Length][];

                for (int fieldIndex = 0; fieldIndex < _fields.Length; fieldIndex++)
                {
                    IFieldValue fieldValue = groupValue.GetValue(fieldIndex);
                    Field       field      = _fields[fieldIndex];
                    if (!field.IsOptional && fieldValue == null)
                    {
                        Global.ErrorHandler.OnError(null, DynError.GeneralError, "Mandatory field {0} is null", field);
                        // BUG? error is ignored?
                    }
                    byte[] encoding = field.Encode(fieldValue, field.MessageTemplate ?? template, context,
                                                   presenceMapBuilder);
                    fieldEncodings[fieldIndex] = encoding;
                }
                var buffer = new MemoryStream();

                if (UsesPresenceMap)
                {
                    byte[] pmap = presenceMapBuilder.BitVector.TruncatedBytes;
                    if (context.TraceEnabled)
                    {
                        context.EncodeTrace.Pmap(pmap);
                    }
                    buffer.Write(pmap, 0, pmap.Length);
                }
                foreach (var t in fieldEncodings)
                {
                    if (t != null)
                    {
                        buffer.Write(t, 0, t.Length);
                    }
                }
                if (context.TraceEnabled)
                {
                    context.EncodeTrace.GroupEnd();
                }
                return(buffer.ToArray());
            }
            catch (IOException e)
            {
                throw new RuntimeException(e);
            }
        }
示例#2
0
        public virtual byte[] Encode(FieldValue value_Renamed, Group template, Context context)
        {
            if (value_Renamed == null)
            {
                return(new byte[] {});
            }

            var groupValue = (GroupValue)value_Renamed;

            if (context.TraceEnabled)
            {
                context.GetEncodeTrace().GroupStart(this);
            }
            var presenceMapBuilder = new BitVectorBuilder(template.MaxPresenceMapSize);

            try
            {
                var fieldEncodings = new byte[fields.Length][];

                for (int fieldIndex = 0; fieldIndex < fields.Length; fieldIndex++)
                {
                    FieldValue fieldValue = groupValue.GetValue(fieldIndex);
                    Field      field      = GetField(fieldIndex);
                    if (!field.Optional && fieldValue == null)
                    {
                        Global.HandleError(Error.FastConstants.GENERAL_ERROR, "Mandatory field " + field + " is null");
                    }
                    byte[] encoding = field.Encode(fieldValue, template, context, presenceMapBuilder);
                    fieldEncodings[fieldIndex] = encoding;
                }
                var buffer = new System.IO.MemoryStream();

                if (UsesPresenceMap())
                {
                    byte[] pmap = presenceMapBuilder.BitVector.TruncatedBytes;
                    if (context.TraceEnabled)
                    {
                        context.GetEncodeTrace().Pmap(pmap);
                    }
                    byte[] temp_byteArray = pmap;
                    buffer.Write(temp_byteArray, 0, temp_byteArray.Length);
                }
                for (int i = 0; i < fieldEncodings.Length; i++)
                {
                    if (fieldEncodings[i] != null)
                    {
                        byte[] temp_byteArray2 = fieldEncodings[i];
                        buffer.Write(temp_byteArray2, 0, temp_byteArray2.Length);
                    }
                }
                if (context.TraceEnabled)
                {
                    context.GetEncodeTrace().GroupEnd();
                }
                return(buffer.ToArray());
            }
            catch (System.IO.IOException e)
            {
                throw new RuntimeException(e);
            }
        }