/// <summary> /// Writes the end of a BSON document to the writer. /// </summary> public override void WriteEndDocument() { if (Disposed) { throw new ObjectDisposedException("BsonBinaryWriter"); } if (State != BsonWriterState.Name) { ThrowInvalidState("WriteEndDocument", BsonWriterState.Name); } if (_context.ContextType != ContextType.Document && _context.ContextType != ContextType.ScopeDocument) { ThrowInvalidContextType("WriteEndDocument", _context.ContextType, ContextType.Document, ContextType.ScopeDocument); } base.WriteEndDocument(); _buffer.WriteByte(0); BackpatchSize(); // size of document _context = _context.ParentContext; 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(); } }
/// <summary> /// Writes a raw BSON document. /// </summary> /// <param name="slice">The byte buffer containing the raw BSON document.</param> 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(); } }
// constructors internal BsonBinaryWriterContext( BsonBinaryWriterContext parentContext, ContextType contextType, int startPosition) { _parentContext = parentContext; _contextType = contextType; _startPosition = startPosition; }
private int _index; // used when contextType is Array // constructors internal BsonBinaryWriterContext( BsonBinaryWriterContext parentContext, ContextType contextType, int startPosition) { _parentContext = parentContext; _contextType = contextType; _startPosition = startPosition; }
/// <summary> /// Initializes a new instance of the BsonBinaryWriter class. /// </summary> /// <param name="buffer">A BsonBuffer.</param> /// <param name="disposeBuffer">if set to <c>true</c> this BsonBinaryReader will own the buffer and when Dispose is called the buffer will be Disposed also.</param> /// <param name="settings">Optional BsonBinaryWriter settings.</param> /// <exception cref="System.ArgumentNullException"> /// encoder /// or /// settings /// </exception> public BsonBinaryWriter(BsonBuffer buffer, bool disposeBuffer, BsonBinaryWriterSettings settings) : base(settings) { if (buffer == null) { throw new ArgumentNullException("encoder"); } _buffer = buffer; _disposeBuffer = disposeBuffer; _binaryWriterSettings = settings; // already frozen by base class _maxDocumentSizeStack.Push(_binaryWriterSettings.MaxDocumentSize); _context = null; State = BsonWriterState.Initial; }
// public methods /// <summary> /// Closes the writer. /// </summary> public override void Close() { // Close can be called on Disposed objects if (State != BsonWriterState.Closed) { if (State == BsonWriterState.Done) { Flush(); } if (_stream != null && _binaryWriterSettings.CloseOutput) { _stream.Close(); } _context = null; State = BsonWriterState.Closed; } }
/// <summary> /// Writes the start of a BSON array to the writer. /// </summary> public override void WriteStartArray() { if (Disposed) { throw new ObjectDisposedException("BsonBinaryWriter"); } if (State != BsonWriterState.Value) { ThrowInvalidState("WriteStartArray", BsonWriterState.Value); } base.WriteStartArray(); _buffer.WriteByte((byte)BsonType.Array); WriteNameHelper(); _context = new BsonBinaryWriterContext(_context, ContextType.Array, _buffer.Position); _buffer.WriteInt32(0); // reserve space for size State = BsonWriterState.Value; }
// constructors /// <summary> /// Initializes a new instance of the BsonBinaryWriter class. /// </summary> /// <param name="stream">A stream.</param> /// <param name="buffer">A BsonBuffer.</param> /// <param name="settings">Optional BsonBinaryWriter settings.</param> public BsonBinaryWriter(Stream stream, BsonBuffer buffer, BsonBinaryWriterSettings settings) : base(settings) { _stream = stream; if (buffer == null) { _buffer = new BsonBuffer(); _disposeBuffer = true; // only call Dispose if we allocated the buffer } else { _buffer = buffer; _disposeBuffer = false; } _binaryWriterSettings = settings; // already frozen by base class _context = null; State = BsonWriterState.Initial; }
/// <summary> /// Writes a BSON JavaScript to the writer (call WriteStartDocument to start writing the scope). /// </summary> /// <param name="code">The JavaScript code.</param> public override void WriteJavaScriptWithScope(string code) { if (Disposed) { throw new ObjectDisposedException("BsonBinaryWriter"); } if (State != BsonWriterState.Value) { ThrowInvalidState("WriteJavaScriptWithScope", BsonWriterState.Value); } _buffer.WriteByte((byte)BsonType.JavaScriptWithScope); WriteNameHelper(); _context = new BsonBinaryWriterContext(_context, ContextType.JavaScriptWithScope, _buffer.Position); _buffer.WriteInt32(0); // reserve space for size of JavaScript with scope value _buffer.WriteString(code); State = BsonWriterState.ScopeDocument; }
/// <summary> /// Writes the end of a BSON array to the writer. /// </summary> public override void WriteEndArray() { if (Disposed) { throw new ObjectDisposedException("BsonBinaryWriter"); } if (State != BsonWriterState.Value) { ThrowInvalidState("WriteEndArray", BsonWriterState.Value); } if (_context.ContextType != ContextType.Array) { ThrowInvalidContextType("WriteEndArray", _context.ContextType, ContextType.Array); } base.WriteEndArray(); _buffer.WriteByte(0); BackpatchSize(); // size of document _context = _context.ParentContext; State = GetNextState(); }
/// <summary> /// Writes the start of a BSON document to the writer. /// </summary> public override void WriteStartDocument() { if (Disposed) { throw new ObjectDisposedException("BsonBinaryWriter"); } if (State != BsonWriterState.Initial && State != BsonWriterState.Value && State != BsonWriterState.ScopeDocument && State != BsonWriterState.Done) { ThrowInvalidState("WriteStartDocument", BsonWriterState.Initial, BsonWriterState.Value, BsonWriterState.ScopeDocument, BsonWriterState.Done); } base.WriteStartDocument(); if (State == BsonWriterState.Value) { _buffer.WriteByte((byte)BsonType.Document); WriteNameHelper(); } var contextType = (State == BsonWriterState.ScopeDocument) ? ContextType.ScopeDocument : ContextType.Document; _context = new BsonBinaryWriterContext(_context, ContextType.Document, _buffer.Position); _buffer.WriteInt32(0); // reserve space for size State = BsonWriterState.Name; }