예제 #1
0
        /// <summary>
        /// Calculates the encoded size of this field in bytes. This is the encoded size of the
        /// underlying value as defined by the corresponding <c>FudgeFieldType</c>, the 2 byte
        /// field prefix, plus the ordinal index and field name if specified. If a taxonomy is
        /// specified and defines the field name, only the corresponding ordinal index would be
        /// written so the field name is not counted.
        /// </summary>
        /// <param name="taxonomy">taxonomy used to resolve field names, or null</param>
        /// <returns>the encoded size of this field in bytes</returns>
        public override int ComputeSize(IFudgeTaxonomy taxonomy)
        {
            int size = 0;

            // Field prefix
            size += 2;
            bool hasOrdinal = ordinal != null;
            bool hasName    = name != null;

            if ((name != null) && (taxonomy != null))
            {
                if (taxonomy.GetFieldOrdinal(name) != null)
                {
                    hasOrdinal = true;
                    hasName    = false;
                }
            }
            if (hasOrdinal)
            {
                size += 2;
            }
            if (hasName)
            {
                // One for the size prefix
                size++;
                // Then for the UTF Encoding
                size += StringFieldType.Encoding.GetByteCount(name);
            }
            if (type.IsVariableSize)
            {
                int valueSize = type.GetVariableSize(value, taxonomy);
                if (valueSize <= 255)
                {
                    size += valueSize + 1;
                }
                else if (valueSize <= short.MaxValue)
                {
                    size += valueSize + 2;
                }
                else
                {
                    size += valueSize + 4;
                }
            }
            else
            {
                size += type.FixedSize;
            }
            return(size);
        }