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

WriteEndDocument() public method

Writes the end of a BSON document to the writer.
public WriteEndDocument ( ) : void
return void
        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();
            }
        }

Usage Example

Exemplo n.º 1
0
        /// <summary>
        /// Reads a raw BSON array.
        /// </summary>
        /// <returns>The raw BSON array.</returns>
        public virtual IByteBuffer ReadRawBsonArray()
        {
            // overridden in BsonBinaryReader to read the raw bytes from the stream
            // for all other streams, deserialize the array and reserialize it using a BsonBinaryWriter to get the raw bytes

            var deserializationContext = BsonDeserializationContext.CreateRoot(this);
            var array = BsonArraySerializer.Instance.Deserialize(deserializationContext);

            using (var memoryStream = new MemoryStream())
                using (var bsonWriter = new BsonBinaryWriter(memoryStream, BsonBinaryWriterSettings.Defaults))
                {
                    var serializationContext = BsonSerializationContext.CreateRoot(bsonWriter);
                    bsonWriter.WriteStartDocument();
                    var startPosition = memoryStream.Position + 3; // just past BsonType, "x" and null byte
                    bsonWriter.WriteName("x");
                    BsonArraySerializer.Instance.Serialize(serializationContext, array);
                    var endPosition = memoryStream.Position;
                    bsonWriter.WriteEndDocument();

                    byte[] memoryStreamBuffer;
#if NETSTANDARD1_5 || NETSTANDARD1_6
                    memoryStreamBuffer = memoryStream.ToArray();
#else
                    memoryStreamBuffer = memoryStream.GetBuffer();
#endif
                    var buffer = new ByteArrayBuffer(memoryStreamBuffer, (int)memoryStream.Length, isReadOnly: true);
                    return(new ByteBufferSlice(buffer, (int)startPosition, (int)(endPosition - startPosition)));
                }
        }
All Usage Examples Of MongoDB.Bson.IO.BsonBinaryWriter::WriteEndDocument