MongoDB.Bson.IO.BsonBinaryWriter.WriteRawBsonDocument C# (CSharp) Method

WriteRawBsonDocument() public method

Writes a raw BSON document.
public WriteRawBsonDocument ( IByteBuffer slice ) : void
slice IByteBuffer The byte buffer containing the raw BSON document.
return void
        public override void WriteRawBsonDocument(IByteBuffer slice)
        {
            if (Disposed) { throw new ObjectDisposedException("BsonBinaryWriter"); }
            if (State != BsonWriterState.Initial && State != BsonWriterState.Value && State != BsonWriterState.ScopeDocument && State != BsonWriterState.Done)
            {
                ThrowInvalidState("WriteRawBsonDocument", BsonWriterState.Initial, BsonWriterState.Value, BsonWriterState.ScopeDocument, BsonWriterState.Done);
            }

            if (State == BsonWriterState.Value)
            {
                _buffer.WriteByte((byte)BsonType.Document);
                WriteNameHelper();
            }
            _buffer.ByteBuffer.WriteBytes(slice); // assumes byteBuffer is a valid raw BSON document

            if (_context == null)
            {
                State = BsonWriterState.Done;
            }
            else
            {
                if (_context.ContextType == ContextType.JavaScriptWithScope)
                {
                    BackpatchSize(); // size of the JavaScript with scope value
                    _context = _context.ParentContext;
                }
                State = GetNextState();
            }
        }

Usage Example

 // private methods
 private void AddOverfow(BsonBinaryWriter bsonBinaryWriter, IByteBuffer overflow)
 {
     _lastRequestPosition = bsonBinaryWriter.Buffer.Position;
     bsonBinaryWriter.WriteRawBsonDocument(overflow);
     _batchCount++;
     _batchLength = bsonBinaryWriter.Buffer.Position - _batchStartPosition;
 }