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

Flush() public method

Flushes any pending data to the output destination.
public Flush ( ) : void
return void
        public override void Flush()
        {
            if (Disposed) { throw new ObjectDisposedException("BsonBinaryWriter"); }
            if (State == BsonWriterState.Closed)
            {
                throw new InvalidOperationException("Flush called on closed BsonWriter.");
            }
            if (State != BsonWriterState.Done)
            {
                throw new InvalidOperationException("Flush called before BsonBinaryWriter was finished writing to buffer.");
            }
            if (_stream != null)
            {
                _buffer.WriteTo(_stream);
                _stream.Flush();
                _buffer.Clear(); // only clear the buffer if we have written it to a stream
            }
        }

Usage Example

Exemplo n.º 1
0
 public void TestFlushAndClose()
 {
     var stream = new MemoryStream();
     using (var bsonWriter = new BsonBinaryWriter(stream))
     {
         bsonWriter.WriteStartDocument();
         bsonWriter.WriteEndDocument();
         bsonWriter.Flush();
         bsonWriter.Close();
     }
 }