Exemple #1
0
        private int GetByteSize(Element element)
        {
            switch (element.TypeOfElement)
            {
            case ElementType.Collection:
                CollectionElement collection = element as CollectionElement
                                               ?? throw new InvalidDataException("Element with type Collection is not a CollectionElement.");

                return(GetByteSize(collection));

            case ElementType.Vector:
                VectorElement vector = element as VectorElement
                                       ?? throw new InvalidDataException("Element with type Vector is not a VectorElement.");

                return(GetByteSize(vector));

            case ElementType.Scalar:
                ScalarElement scalar = element as ScalarElement
                                       ?? throw new InvalidDataException("Element with type Scalar is not a ScalarElement.");

                return(GetByteSize(scalar));

            default:
                return(0);
            }
        }
Exemple #2
0
        private int GetByteSize(CollectionElement collection)
        {
            int sum = collection.Elements
                      .Where(element => !IsEmbedded(element))
                      .Sum(GetPaddedByteSize);

            return(4 + 28 * collection.Size + sum);
        }
Exemple #3
0
        private void WriteCollection(BinaryWriter writer, CollectionElement collection)
        {
            int linkPosition = (int)writer.BaseStream.Position + 4 + 28 * collection.Size;

            writer.Write(collection.Size);

            foreach (Element element in collection.Elements)
            {
                bool isEmbedded = IsEmbedded(element);

                writer.Write(element.TagOfElement.ToByteArray());
                writer.Write((byte)element.TypeOfElement);
                writer.Write((byte)element.TypeOfValue);
                writer.Write(isEmbedded ? (byte)1 : (byte)0);
                writer.Write((byte)0);

                if (!isEmbedded)
                {
                    int padSize = GetPaddedByteSize(element);
                    writer.Write(linkPosition);
                    writer.Write(padSize);
                    linkPosition += padSize;
                }
                else
                {
                    ScalarElement scalar = element as ScalarElement
                                           ?? throw new InvalidDataException("Embedded element is not a scalar element.");

                    WriteScalar(writer, scalar);

                    for (int i = element.TypeOfValue.GetByteSize(); i < 8; i++)
                    {
                        writer.Write((byte)0);
                    }
                }
            }

            foreach (Element element in collection.Elements)
            {
                if (IsEmbedded(element))
                {
                    continue;
                }

                switch (element.TypeOfElement)
                {
                case ElementType.Collection:
                    CollectionElement nestedCollection = element as CollectionElement
                                                         ?? throw new InvalidDataException("Element with type Collection is not a CollectionElement.");

                    WriteCollection(writer, nestedCollection);
                    break;

                case ElementType.Vector:
                    VectorElement vector = element as VectorElement
                                           ?? throw new InvalidDataException("Element with type Vector is not a VectorElement.");

                    WriteVector(writer, vector);
                    break;

                case ElementType.Scalar:
                    ScalarElement scalar = element as ScalarElement
                                           ?? throw new InvalidDataException("Element with type Scalar is not a ScalarElement.");

                    WriteScalar(writer, scalar);
                    break;
                }

                int byteSize = GetByteSize(element);
                int padSize  = GetPaddedByteSize(element);

                for (int i = 0; i < padSize - byteSize; i++)
                {
                    writer.Write((byte)0);
                }
            }
        }
Exemple #4
0
 /// <summary>
 /// Creates a new instance of the <see cref="RecordBody"/> class.
 /// </summary>
 /// <param name="root">The root collection element of the record.</param>
 public RecordBody(CollectionElement root) => Collection = root;