コード例 #1
0
        /// <summary>
        /// Writes a raw BSON array.
        /// </summary>
        /// <param name="slice">The byte buffer containing the raw BSON array.</param>
        public virtual void WriteRawBsonArray(IByteBuffer slice)
        {
            // overridden in BsonBinaryWriter

            using (var bsonBuffer = new BsonBuffer())
            {
                BsonArray array;

                // wrap the array in a fake document so we can deserialize it
                var arrayLength    = slice.Length;
                var documentLength = arrayLength + 8;
                bsonBuffer.WriteInt32(documentLength);
                bsonBuffer.WriteByte((byte)BsonType.Array);
                bsonBuffer.WriteByte((byte)'x');
                bsonBuffer.WriteByte((byte)0);
                bsonBuffer.ByteBuffer.WriteBytes(slice);
                bsonBuffer.WriteByte((byte)0);

                bsonBuffer.Position = 0;
                using (var bsonReader = new BsonBinaryReader(bsonBuffer, true, BsonBinaryReaderSettings.Defaults))
                {
                    bsonReader.ReadStartDocument();
                    bsonReader.ReadName("x");
                    array = (BsonArray)BsonArraySerializer.Instance.Deserialize(bsonReader, typeof(BsonArray), null);
                    bsonReader.ReadEndDocument();
                }

                BsonArraySerializer.Instance.Serialize(this, typeof(BsonArray), array, null);
            }
        }
コード例 #2
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();
        }