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

WriteStartArray() public method

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

Usage Example

        public void BackpatchSize_should_throw_when_size_is_larger_than_2GB()
        {
            using (var stream = new NullBsonStream())
            using (var binaryWriter = new BsonBinaryWriter(stream))
            {
                var bytes = new byte[int.MaxValue / 2]; // 1GB
                var binaryData = new BsonBinaryData(bytes);

                binaryWriter.WriteStartDocument();
                binaryWriter.WriteName("array");
                binaryWriter.WriteStartArray();
                binaryWriter.WriteBinaryData(binaryData);
                binaryWriter.WriteBinaryData(binaryData);

                Action action = () => binaryWriter.WriteEndArray(); // indirectly calls private BackpatchSize method

                action.ShouldThrow<FormatException>();
            }
        }
All Usage Examples Of MongoDB.Bson.IO.BsonBinaryWriter::WriteStartArray