Exemple #1
0
#pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary
        /// <summary>
        /// Writes BSON binary data to the writer.
        /// </summary>
        /// <param name="bytes">The binary data.</param>
        /// <param name="subType">The binary data subtype.</param>
        /// <param name="guidRepresentation">The representation for Guids.</param>
        public override void WriteBinaryData(
            byte[] bytes,
            BsonBinarySubType subType,
            GuidRepresentation guidRepresentation)
        {
            if (Disposed)
            {
                throw new ObjectDisposedException("BsonBinaryWriter");
            }
            if (State != BsonWriterState.Value)
            {
                ThrowInvalidState("WriteBinaryData", BsonWriterState.Value);
            }

            switch (subType)
            {
            case BsonBinarySubType.OldBinary:
                if (_binaryWriterSettings.FixOldBinarySubTypeOnOutput)
                {
                    subType = BsonBinarySubType.Binary;     // replace obsolete OldBinary with new Binary sub type
                }
                break;

            case BsonBinarySubType.UuidLegacy:
            case BsonBinarySubType.UuidStandard:
                if (_binaryWriterSettings.GuidRepresentation != GuidRepresentation.Unspecified)
                {
                    var expectedSubType = (_binaryWriterSettings.GuidRepresentation == GuidRepresentation.Standard) ? BsonBinarySubType.UuidStandard : BsonBinarySubType.UuidLegacy;
                    if (subType != expectedSubType)
                    {
                        var message = string.Format(
                            "The GuidRepresentation for the writer is {0}, which requires the subType argument to be {1}, not {2}.",
                            _binaryWriterSettings.GuidRepresentation, expectedSubType, subType);
                        throw new BsonSerializationException(message);
                    }
                    if (guidRepresentation != _binaryWriterSettings.GuidRepresentation)
                    {
                        var message = string.Format(
                            "The GuidRepresentation for the writer is {0}, which requires the the guidRepresentation argument to also be {0}, not {1}.",
                            _binaryWriterSettings.GuidRepresentation, guidRepresentation);
                        throw new BsonSerializationException(message);
                    }
                }
                break;
            }

            _buffer.WriteByte((byte)BsonType.Binary);
            WriteNameHelper();
            if (subType == BsonBinarySubType.OldBinary)
            {
                // sub type OldBinary has two sizes (for historical reasons)
                _buffer.WriteInt32(bytes.Length + 4);
                _buffer.WriteByte((byte)subType);
                _buffer.WriteInt32(bytes.Length);
            }
            else
            {
                _buffer.WriteInt32(bytes.Length);
                _buffer.WriteByte((byte)subType);
            }
            _buffer.WriteBytes(bytes);

            State = GetNextState();
        }