internal override void WriteHeaderTo(BsonBuffer buffer)
 {
     base.WriteHeaderTo(buffer);
     buffer.WriteInt32(0); // reserved
     buffer.WriteCString(new UTF8Encoding(false, true), _collectionFullName);
     buffer.WriteInt32(_numberToReturn);
 }
 // protected methods
 protected override void WriteBody(BsonBuffer buffer)
 {
     buffer.WriteInt32((int)_flags);
     buffer.WriteCString(new UTF8Encoding(false, true), _collectionFullName);
     _firstDocumentStartPosition = buffer.Position;
     // documents to be added later by calling AddDocument
 }
 // protected methods
 protected override void WriteBody(BsonBuffer buffer)
 {
     buffer.WriteInt32(0); // reserved
     buffer.WriteCString(new UTF8Encoding(false, true), _collectionFullName);
     buffer.WriteInt32(_numberToReturn);
     buffer.WriteInt64(_cursorId);
 }
        /// <summary>
        /// Writes a BSON regular expression to the writer.
        /// </summary>
        /// <param name="regex">A BsonRegularExpression.</param>
        public override void WriteRegularExpression(BsonRegularExpression regex)
        {
            if (Disposed)
            {
                throw new ObjectDisposedException("BsonBinaryWriter");
            }
            if (State != BsonWriterState.Value)
            {
                ThrowInvalidState("WriteRegularExpression", BsonWriterState.Value);
            }

            _buffer.WriteByte((byte)BsonType.RegularExpression);
            WriteNameHelper();
            _buffer.WriteCString(regex.Pattern);
            _buffer.WriteCString(regex.Options);

            State = GetNextState();
        }
        /// <summary>
        /// Writes a BSON regular expression to the writer.
        /// </summary>
        /// <param name="pattern">A regular expression pattern.</param>
        /// <param name="options">A regular expression options.</param>
        public override void WriteRegularExpression(string pattern, string options)
        {
            if (disposed)
            {
                throw new ObjectDisposedException("BsonBinaryWriter");
            }
            if (state != BsonWriterState.Value)
            {
                ThrowInvalidState("WriteRegularExpression", BsonWriterState.Value);
            }

            buffer.WriteByte((byte)BsonType.RegularExpression);
            WriteNameHelper();
            buffer.WriteCString(pattern);
            buffer.WriteCString(options);

            state = GetNextState();
        }
        /// <summary>
        /// Writes a BSON regular expression to the writer.
        /// </summary>
        /// <param name="pattern">A regular expression pattern.</param>
        /// <param name="options">A regular expression options.</param>
        public override void WriteRegularExpression(
            string pattern,
            string options
            )
        {
            if (disposed)
            {
                throw new ObjectDisposedException("BsonBinaryWriter");
            }
            if (state != BsonWriterState.Value)
            {
                var message = string.Format("WriteRegularExpression cannot be called when State is: {0}", state);
                throw new InvalidOperationException(message);
            }

            buffer.WriteByte((byte)BsonType.RegularExpression);
            WriteNameHelper();
            buffer.WriteCString(pattern);
            buffer.WriteCString(options);

            state = GetNextState();
        }
        // protected methods
        protected override void WriteBody(BsonBuffer buffer)
        {
            buffer.WriteInt32(0); // reserved
            buffer.WriteCString(_collectionFullName);
            buffer.WriteInt32((int)_flags);

            using (var bsonWriter = new BsonBinaryWriter(buffer, false, WriterSettings))
            {
                if (_query == null)
                {
                    bsonWriter.WriteStartDocument();
                    bsonWriter.WriteEndDocument();
                }
                else
                {
                    BsonSerializer.Serialize(bsonWriter, _query.GetType(), _query, DocumentSerializationOptions.SerializeIdFirstInstance);
                }
            }
        }
        // protected methods
        protected override void WriteBody(BsonBuffer buffer)
        {
            buffer.WriteInt32((int)_flags);
            buffer.WriteCString(new UTF8Encoding(false, true), _collectionFullName);
            buffer.WriteInt32(_numberToSkip);
            buffer.WriteInt32(_numberToReturn);

            using (var bsonWriter = new BsonBinaryWriter(buffer, false, WriterSettings))
            {
                if (_query == null)
                {
                    bsonWriter.WriteStartDocument();
                    bsonWriter.WriteEndDocument();
                }
                else
                {
                    BsonSerializer.Serialize(bsonWriter, _query.GetType(), _query, DocumentSerializationOptions.SerializeIdFirstInstance);
                }
                if (_fields != null)
                {
                    BsonSerializer.Serialize(bsonWriter, _fields);
                }
            }
        }
 public void TestWriteCStringThrowsWhenValueIsNull()
 {
     using (var bsonBuffer = new BsonBuffer())
     {
         Assert.Throws<ArgumentNullException>(() => { bsonBuffer.WriteCString((UTF8Encoding)Encoding.UTF8, null); });
     }
 }
 public void TestWriteCStringThrowsWhenValueContainsNulls()
 {
     using (var bsonBuffer = new BsonBuffer())
     {
         Assert.Throws<ArgumentException>(() => { bsonBuffer.WriteCString((UTF8Encoding)Encoding.UTF8, "a\0b"); });
     }
 }
        internal override void WriteHeaderTo(BsonBuffer 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(new UTF8Encoding(false, true), _collectionFullName);
            buffer.WriteInt32(_numberToSkip);
            buffer.WriteInt32(_numberToReturn);
        }
 internal override void WriteHeaderTo(BsonBuffer buffer)
 {
     _batchStartPosition = buffer.Position;
     base.WriteHeaderTo(buffer);
     buffer.WriteInt32((int)_flags);
     buffer.WriteCString(new UTF8Encoding(false, true), _collectionFullName);
 }