コード例 #1
0
 internal virtual void WriteHeaderTo(BsonStreamWriter streamWriter)
 {
     streamWriter.WriteInt32(0); // messageLength will be backpatched later
     streamWriter.WriteInt32(_requestId);
     streamWriter.WriteInt32(0); // responseTo not used in requests sent by client
     streamWriter.WriteInt32((int)_opcode);
 }
コード例 #2
0
 internal override void WriteHeaderTo(BsonStreamWriter streamWriter)
 {
     base.WriteHeaderTo(streamWriter);
     streamWriter.WriteInt32(0); // reserved
     streamWriter.WriteCString(_collectionFullName);
     streamWriter.WriteInt32((int)_flags);
 }
コード例 #3
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 to write the raw bytes to the stream
            // for all other streams, deserialize the raw bytes and serialize the resulting array instead

            var documentLength = slice.Length + 8;

            using (var memoryStream = new MemoryStream(documentLength))
            {
                // wrap the array in a fake document so we can deserialize it
                var streamWriter = new BsonStreamWriter(memoryStream, Utf8Helper.StrictUtf8Encoding);
                streamWriter.WriteInt32(documentLength);
                streamWriter.WriteBsonType(BsonType.Array);
                streamWriter.WriteByte((byte)'x');
                streamWriter.WriteByte(0);
                slice.WriteTo(streamWriter.BaseStream);
                streamWriter.WriteByte(0);

                memoryStream.Position = 0;
                using (var bsonReader = new BsonBinaryReader(memoryStream, BsonBinaryReaderSettings.Defaults))
                {
                    var deserializationContext = BsonDeserializationContext.CreateRoot <BsonDocument>(bsonReader);
                    bsonReader.ReadStartDocument();
                    bsonReader.ReadName("x");
                    var array = deserializationContext.DeserializeWithChildContext(BsonArraySerializer.Instance);
                    bsonReader.ReadEndDocument();

                    var serializationContext = BsonSerializationContext.CreateRoot <BsonArray>(this);
                    BsonArraySerializer.Instance.Serialize(serializationContext, array);
                }
            }
        }
コード例 #4
0
 // internal methods
 internal override void WriteBodyTo(BsonStreamWriter streamWriter)
 {
     streamWriter.WriteInt32(_cursorIds.Length);
     foreach (long cursorId in _cursorIds)
     {
         streamWriter.WriteInt64(cursorId);
     }
 }
コード例 #5
0
 // private methods
 private void Backpatch(Stream stream, int position, int value)
 {
     var streamWriter = new BsonStreamWriter(stream, Utf8Helper.StrictUtf8Encoding);
     var currentPosition = stream.Position;
     stream.Position = position;
     streamWriter.WriteInt32(value);
     stream.Position = currentPosition;
 }
コード例 #6
0
        internal override void WriteHeaderTo(BsonStreamWriter buffer)
        {
            if ((_flags & QueryFlags.Exhaust) != 0)
            {
                throw new NotSupportedException("The Exhaust QueryFlag is not yet supported.");
            }

            base.WriteHeaderTo(buffer);
            buffer.WriteInt32((int)_flags);
            buffer.WriteCString(_collectionFullName);
            buffer.WriteInt32(_numberToSkip);
            buffer.WriteInt32(_numberToReturn);
        }
コード例 #7
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 to write the raw bytes to the stream
            // for all other streams, deserialize the raw bytes and serialize the resulting array instead

            var documentLength = slice.Length + 8;
            using (var memoryStream = new MemoryStream(documentLength))
            {
                // wrap the array in a fake document so we can deserialize it
                var streamWriter = new BsonStreamWriter(memoryStream, Utf8Helper.StrictUtf8Encoding);
                streamWriter.WriteInt32(documentLength);
                streamWriter.WriteBsonType(BsonType.Array);
                streamWriter.WriteByte((byte)'x');
                streamWriter.WriteByte(0);
                slice.WriteTo(streamWriter.BaseStream);
                streamWriter.WriteByte(0);

                memoryStream.Position = 0;
                using (var bsonReader = new BsonBinaryReader(memoryStream, BsonBinaryReaderSettings.Defaults))
                {
                    var deserializationContext = BsonDeserializationContext.CreateRoot<BsonDocument>(bsonReader);
                    bsonReader.ReadStartDocument();
                    bsonReader.ReadName("x");
                    var array = deserializationContext.DeserializeWithChildContext(BsonArraySerializer.Instance);
                    bsonReader.ReadEndDocument();

                    var serializationContext = BsonSerializationContext.CreateRoot<BsonArray>(this);
                    BsonArraySerializer.Instance.Serialize(serializationContext, array);
                }
            }
        }
コード例 #8
0
#pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary
        /// <summary>
        /// Writes BSON binary data to the writer.
        /// </summary>
        /// <param name="binaryData">The binary data.</param>
        public override void WriteBinaryData(BsonBinaryData binaryData)
        {
            if (Disposed)
            {
                throw new ObjectDisposedException("BsonBinaryWriter");
            }
            if (State != BsonWriterState.Value)
            {
                ThrowInvalidState("WriteBinaryData", BsonWriterState.Value);
            }

            var bytes              = binaryData.Bytes;
            var subType            = binaryData.SubType;
            var guidRepresentation = binaryData.GuidRepresentation;

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

            case BsonBinarySubType.UuidLegacy:
            case BsonBinarySubType.UuidStandard:
                if (_settings.GuidRepresentation != GuidRepresentation.Unspecified)
                {
                    var expectedSubType = (_settings.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}.",
                            _settings.GuidRepresentation, expectedSubType, subType);
                        throw new BsonSerializationException(message);
                    }
                    if (guidRepresentation != _settings.GuidRepresentation)
                    {
                        var message = string.Format(
                            "The GuidRepresentation for the writer is {0}, which requires the the guidRepresentation argument to also be {0}, not {1}.",
                            _settings.GuidRepresentation, guidRepresentation);
                        throw new BsonSerializationException(message);
                    }
                }
                break;
            }

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

            State = GetNextState();
        }
コード例 #9
0
 internal override void WriteHeaderTo(BsonStreamWriter streamWriter)
 {
     base.WriteHeaderTo(streamWriter);
     streamWriter.WriteInt32(0); // reserved
 }
コード例 #10
0
 internal override void WriteHeaderTo(BsonStreamWriter streamWriter)
 {
     _batchStartPosition = (int)streamWriter.Position;
     base.WriteHeaderTo(streamWriter);
     streamWriter.WriteInt32((int)_flags);
     streamWriter.WriteCString(_collectionFullName);
 }