Пример #1
0
            internal readonly FileTools.BinaryWriter2 binaryWriter;         // the one of the Formatter when multiplexed.

            internal ChannelInfos(
                SerializationFormatter Formatter,
                Stream stream,
#if DEBUG
                string NameForDebugging,
#endif
                ChannelNumber ChannelNumber
                )
            {
                this.stream = stream;

#if DEBUG
                this.NameForDebugging = NameForDebugging;
#endif
                this.ChannelNumber = ChannelNumber;


                this.streamWriterForText =
                    Formatter.IsStringFormatter ?
                    (Formatter.parameters.TheStreamingMode == StreamingModes.SetOfStreams ?
                     new StreamWriter(stream)
                                        : Formatter.streamWriter)
                                        : null;

                this.binaryWriter =
                    !Formatter.IsStringFormatter ?
                    (Formatter.parameters.TheStreamingMode == StreamingModes.SetOfStreams ?
                     new FileTools.BinaryWriter2(stream, Encoding.UTF8)
                                        : Formatter.binaryWriter)
                                        : null;
            }
        internal override bool ChannelEnterElementAndWriteValue(
            ref ChannelInfos channelInfos, Element element
            , out bool TheElementHaveAttributes
            , TypeCode TypeIndex
            , object Value,
            bool IsStructure)
        {
            var et = element.ElementType;

            if (et == ElementTypes.PrimitiveValue && !element.typeIndexIsKnown && TypeIndex != TypeCode.String)                   // No tag for the value primitive types (if not included in an object field or property).
            {
                SerializationFormatter.WriteValueToBinaryWriter(base.binaryWriter, Value, parameters.CompressIntsAs7Bits, TypeIndex);
                TheElementHaveAttributes = false;                         // useless for this formatter.
                return(false);
            }

            if (et == ElementTypes.SubBranch && IsStructure && !element.typeIndexIsKnown && element.NumberOfElements == null) // No tag for structure types (if not included in an object field or property).
            {
                TheElementHaveAttributes = false;                                                                             // useless for this formatter.
                return(true);
            }



            FileTools.BinaryWriter2 bw = base.binaryWriter;

            // 1) Element code : byte
            {
                byte elementCode = unchecked ((byte)et);
                if (element.typeIndexIsKnown)
                {
                    elementCode |= (byte)BinaryAttributeCode.HasATypeNumber;
                }
                if (element.NumberOfElements != null)
                {
                    elementCode |= (byte)BinaryAttributeCode.HasANumberOfElements;
                }
                bw.Write(elementCode);
            }

            // 2) typeNumber : 7 bit-compressed int
            // Type is written immediately after the tag, if a type attribute is required exists.
            if (element.typeIndexIsKnown)
            {
                bw.Write7BitEncodedInt(element.typeIndex);
            }

            // 3) Reference InstanceIndex : 7 bit-compressed int
            if (et == ElementTypes.Reference)
            {
                bw.Write7BitEncodedInt(element.InstanceIndex.Value);
            }

            // 4) NumberOfElements : 7 bit-compressed long
            if (et == ElementTypes.SubBranch)
            {
                if (element.NumberOfElements != null)
                {
                    bw.Write7BitEncodedLong(element.NumberOfElements.Value);
                }
            }
#if DEBUG
            else
            if (element.NumberOfElements != null)
            {
                Debugger.Break();
            }
#endif

            bool alreadyClosed = element.ElementType != ElementTypes.PrimitiveValue;

            if (!alreadyClosed)
            {
#if DEBUG
                if (Value == null)
                {
                    throw new Exception();
                }
#endif
                SerializationFormatter.WriteValueToBinaryWriter(bw, Value, parameters.CompressIntsAs7Bits, TypeIndex);
            }

            TheElementHaveAttributes = false;                     // useless for this formatter.
            return(alreadyClosed);
        }