// constructors
 internal BsonBinaryReaderContext(BsonBinaryReaderContext parentContext, ContextType contextType, int startPosition, int size)
 {
     this.parentContext = parentContext;
     this.contextType = contextType;
     this.startPosition = startPosition;
     this.size = size;
 }
Esempio n. 2
0
        /// <summary>
        /// Reads a raw BSON document.
        /// </summary>
        /// <returns>
        /// The raw BSON document.
        /// </returns>
        public override IByteBuffer ReadRawBsonDocument()
        {
            if (Disposed)
            {
                ThrowObjectDisposedException();
            }
            VerifyBsonType("ReadRawBsonDocument", BsonType.Document);

            var slice = _bsonStream.ReadSlice();

            if (_context.ContextType == ContextType.JavaScriptWithScope)
            {
                _context = _context.PopContext(_bsonStream.Position); // JavaScriptWithScope
            }
            switch (_context.ContextType)
            {
            case ContextType.Array: State = BsonReaderState.Type; break;

            case ContextType.Document: State = BsonReaderState.Type; break;

            case ContextType.TopLevel: State = BsonReaderState.Initial; break;

            default: throw new BsonInternalException("Unexpected ContextType.");
            }

            return(slice);
        }
Esempio n. 3
0
 // constructors
 internal BsonBinaryReaderContext(BsonBinaryReaderContext parentContext, ContextType contextType, int startPosition, int size)
 {
     this.parentContext = parentContext;
     this.contextType   = contextType;
     this.startPosition = startPosition;
     this.size          = size;
 }
        /// <summary>
        /// Reads the end of a BSON array from the reader.
        /// </summary>
        public override void ReadEndArray()
        {
            if (disposed)
            {
                ThrowObjectDisposedException();
            }
            if (context.ContextType != ContextType.Array)
            {
                var message = string.Format("ReadEndArray cannot be called when ContextType is: {0}", context.ContextType);
                throw new InvalidOperationException(message);
            }
            if (state == BsonReaderState.Type)
            {
                ReadBsonType(); // will set state to EndOfArray if at end of array
            }
            if (state != BsonReaderState.EndOfArray)
            {
                var message = string.Format("ReadEndArray cannot be called when State is: {0}", state);
                throw new InvalidOperationException(message);
            }

            context = context.PopContext(buffer.Position);
            switch (context.ContextType)
            {
            case ContextType.Array: state = BsonReaderState.Type; break;

            case ContextType.Document: state = BsonReaderState.Type; break;

            case ContextType.TopLevel: state = BsonReaderState.Done; break;

            default: throw new BsonInternalException("Unexpected ContextType");
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Reads the end of a BSON array from the reader.
        /// </summary>
        public override void ReadEndArray()
        {
            if (Disposed)
            {
                ThrowObjectDisposedException();
            }
            if (_context.ContextType != ContextType.Array)
            {
                ThrowInvalidContextType("ReadEndArray", _context.ContextType, ContextType.Array);
            }
            if (State == BsonReaderState.Type)
            {
                ReadBsonType(); // will set state to EndOfArray if at end of array
            }
            if (State != BsonReaderState.EndOfArray)
            {
                ThrowInvalidState("ReadEndArray", BsonReaderState.EndOfArray);
            }

            _context = _context.PopContext(_bsonStream.Position);
            switch (_context.ContextType)
            {
            case ContextType.Array: State = BsonReaderState.Type; break;

            case ContextType.Document: State = BsonReaderState.Type; break;

            case ContextType.TopLevel: State = BsonReaderState.Initial; break;

            default: throw new BsonInternalException("Unexpected ContextType.");
            }
        }
        /// <summary>
        /// Reads the end of a BSON document from the reader.
        /// </summary>
        public override void ReadEndDocument()
        {
            if (disposed)
            {
                ThrowObjectDisposedException();
            }
            if (context.ContextType != ContextType.Document && context.ContextType != ContextType.ScopeDocument)
            {
                ThrowInvalidContextType("ReadEndDocument", context.ContextType, ContextType.Document, ContextType.ScopeDocument);
            }
            if (state == BsonReaderState.Type)
            {
                ReadBsonType(); // will set state to EndOfDocument if at end of document
            }
            if (state != BsonReaderState.EndOfDocument)
            {
                ThrowInvalidState("ReadEndDocument", BsonReaderState.EndOfDocument);
            }

            context = context.PopContext(buffer.Position);
            if (context != null && context.ContextType == ContextType.JavaScriptWithScope)
            {
                context = context.PopContext(buffer.Position); // JavaScriptWithScope
            }
            switch (context.ContextType)
            {
            case ContextType.Array: state = BsonReaderState.Type; break;

            case ContextType.Document: state = BsonReaderState.Type; break;

            case ContextType.TopLevel: state = BsonReaderState.Done; break;

            default: throw new BsonInternalException("Unexpected ContextType.");
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Reads the end of a BSON document from the reader.
        /// </summary>
        public override void ReadEndDocument()
        {
            if (Disposed)
            {
                ThrowObjectDisposedException();
            }
            if (_context.ContextType != ContextType.Document && _context.ContextType != ContextType.ScopeDocument)
            {
                ThrowInvalidContextType("ReadEndDocument", _context.ContextType, ContextType.Document, ContextType.ScopeDocument);
            }
            if (State == BsonReaderState.Type)
            {
                ReadBsonType(); // will set state to EndOfDocument if at end of document
            }
            if (State != BsonReaderState.EndOfDocument)
            {
                ThrowInvalidState("ReadEndDocument", BsonReaderState.EndOfDocument);
            }

            _context = _context.PopContext(_bsonStream.Position);
            if (_context.ContextType == ContextType.JavaScriptWithScope)
            {
                _context = _context.PopContext(_bsonStream.Position); // JavaScriptWithScope
            }
            switch (_context.ContextType)
            {
            case ContextType.Array: State = BsonReaderState.Type; break;

            case ContextType.Document: State = BsonReaderState.Type; break;

            case ContextType.TopLevel: State = BsonReaderState.Initial; break;

            default: throw new BsonInternalException("Unexpected ContextType.");
            }
        }
Esempio n. 8
0
 /// <summary>
 /// Returns the reader to previously bookmarked position and state.
 /// </summary>
 /// <param name="bookmark">The bookmark.</param>
 public override void ReturnToBookmark(BsonReaderBookmark bookmark)
 {
     var binaryReaderBookmark = (BsonBinaryReaderBookmark)bookmark;
     State = binaryReaderBookmark.State;
     CurrentBsonType = binaryReaderBookmark.CurrentBsonType;
     CurrentName = binaryReaderBookmark.CurrentName;
     _context = binaryReaderBookmark.CloneContext();
     _bsonStream.Position = binaryReaderBookmark.Position;
 }
 public BsonBinaryReader(
     BsonBuffer buffer,
     BsonBinaryReaderSettings settings
 ) {
     this.buffer = buffer ?? new BsonBuffer();
     this.disposeBuffer = buffer == null; // only call Dispose if we allocated the buffer
     this.settings = settings;
     context = null;
 }
        /// <summary>
        /// Returns the reader to previously bookmarked position and state.
        /// </summary>
        /// <param name="bookmark">The bookmark.</param>
        public override void ReturnToBookmark(BsonReaderBookmark bookmark)
        {
            var binaryReaderBookmark = (BsonBinaryReaderBookmark)bookmark;

            state           = binaryReaderBookmark.State;
            currentBsonType = binaryReaderBookmark.CurrentBsonType;
            currentName     = binaryReaderBookmark.CurrentName;
            context         = binaryReaderBookmark.CloneContext();
            buffer.Position = binaryReaderBookmark.Position;
        }
Esempio n. 11
0
        /// <summary>
        /// Reads the start of a BSON array.
        /// </summary>
        public override void ReadStartArray()
        {
            if (Disposed) { ThrowObjectDisposedException(); }
            VerifyBsonType("ReadStartArray", BsonType.Array);

            var startPosition = _bsonStream.Position; // position of size field
            var size = ReadSize();
            _context = new BsonBinaryReaderContext(_context, ContextType.Array, startPosition, size);
            State = BsonReaderState.Type;
        }
Esempio n. 12
0
 // constructors
 internal BsonBinaryReaderBookmark(
     BsonReaderState state,
     BsonType currentBsonType,
     string currentName,
     BsonBinaryReaderContext context,
     int position)
     : base(state, currentBsonType, currentName)
 {
     _context  = context.Clone();
     _position = position;
 }
Esempio n. 13
0
 // constructors
 internal BsonBinaryReaderBookmark(
     BsonReaderState state,
     BsonType currentBsonType,
     string currentName,
     BsonBinaryReaderContext context,
     int position)
     : base(state, currentBsonType, currentName)
 {
     _context = context.Clone();
     _position = position;
 }
 public BsonBinaryReader(
     BsonBuffer buffer,
     BsonBinaryReaderSettings settings
 ) {
     this.buffer = buffer ?? new BsonBuffer();
     this.disposeBuffer = buffer == null; // only call Dispose if we allocated the buffer
     this.settings = settings;
     context = null;
     state = BsonReadState.Initial;
     currentBsonType = BsonType.Document;
 }
Esempio n. 15
0
        /// <summary>
        /// Reads the start of a BSON document.
        /// </summary>
        public override void ReadStartDocument()
        {
            if (Disposed) { ThrowObjectDisposedException(); }
            VerifyBsonType("ReadStartDocument", BsonType.Document);

            var contextType = (State == BsonReaderState.ScopeDocument) ? ContextType.ScopeDocument : ContextType.Document;
            var startPosition = _bsonStream.Position; // position of size field
            var size = ReadSize();
            _context = new BsonBinaryReaderContext(_context, contextType, startPosition, size);
            State = BsonReaderState.Type;
        }
 // constructors
 internal BsonBinaryReaderContext(
     BsonBinaryReaderContext parentContext,
     ContextType contextType,
     int startPosition, 
     int size)
 {
     _parentContext = parentContext;
     _contextType = contextType;
     _startPosition = startPosition;
     _size = size;
 }
 // constructors
 internal BsonBinaryReaderContext(
     BsonBinaryReaderContext parentContext,
     ContextType contextType,
     int startPosition,
     int size)
 {
     _parentContext = parentContext;
     _contextType   = contextType;
     _startPosition = startPosition;
     _size          = size;
 }
 internal BsonBinaryReaderBookmark(
     BsonBinaryReaderContext context,
     BsonReadState state,
     BsonType currentBsonType,
     int position
 )
 {
     this.context = context;
     this.state = state;
     this.currentBsonType = currentBsonType;
     this.position = position;
 }
Esempio n. 19
0
        /// <summary>
        /// Reads a BSON JavaScript with scope from the reader (call ReadStartDocument next to read the scope).
        /// </summary>
        /// <returns>A string.</returns>
        public override string ReadJavaScriptWithScope()
        {
            if (Disposed) { ThrowObjectDisposedException(); }
            VerifyBsonType("ReadJavaScriptWithScope", BsonType.JavaScriptWithScope);

            var startPosition = _bsonStream.Position; // position of size field
            var size = ReadSize();
            _context = new BsonBinaryReaderContext(_context, ContextType.JavaScriptWithScope, startPosition, size);
            var code = _bsonStream.ReadString(_settings.Encoding);

            State = BsonReaderState.ScopeDocument;
            return code;
        }
Esempio n. 20
0
 /// <summary>
 /// Initializes a new instance of the BsonBinaryReader class.
 /// <param name="buffer">A BsonBuffer.</param>
 /// <param name="settings">A BsonBinaryReaderSettings.</param>
 /// </summary>
 public BsonBinaryReader(
     BsonBuffer buffer,
     BsonBinaryReaderSettings settings
 ) {
     if (buffer == null) {
         this.buffer = new BsonBuffer();
         this.disposeBuffer = true; // only call Dispose if we allocated the buffer
     } else {
         this.buffer = buffer;
         this.disposeBuffer = false;
     }
     this.settings = settings.Freeze();
     context = new BsonBinaryReaderContext(null, ContextType.TopLevel, 0, 0);
 }
Esempio n. 21
0
        /// <summary>
        /// Initializes a new instance of the BsonBinaryReader 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">A BsonBinaryReaderSettings.</param>
        /// <exception cref="System.ArgumentNullException">
        /// buffer
        /// or
        /// settings
        /// </exception>
        public BsonBinaryReader(BsonBuffer buffer, bool disposeBuffer, BsonBinaryReaderSettings settings)
            : base(settings)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }

            _buffer               = buffer;
            _disposeBuffer        = disposeBuffer;
            _binaryReaderSettings = settings; // already frozen by base class

            _context = new BsonBinaryReaderContext(null, ContextType.TopLevel, 0, 0);
        }
 // constructors
 /// <summary>
 /// Initializes a new instance of the BsonBinaryReader class.
 /// </summary>
 /// <param name="buffer">A BsonBuffer.</param>
 /// <param name="settings">A BsonBinaryReaderSettings.</param>
 public BsonBinaryReader(BsonBuffer buffer, BsonBinaryReaderSettings settings)
     : base(settings)
 {
     if (buffer == null)
     {
         this.buffer        = new BsonBuffer();
         this.disposeBuffer = true; // only call Dispose if we allocated the buffer
     }
     else
     {
         this.buffer        = buffer;
         this.disposeBuffer = false;
     }
     this.settings = settings; // already frozen by base class
     context       = new BsonBinaryReaderContext(null, ContextType.TopLevel, 0, 0);
 }
 // constructors
 /// <summary>
 /// Initializes a new instance of the BsonBinaryReader class.
 /// </summary>
 /// <param name="buffer">A BsonBuffer.</param>
 /// <param name="settings">A BsonBinaryReaderSettings.</param>
 public BsonBinaryReader(BsonBuffer buffer, BsonBinaryReaderSettings settings)
     : base(settings)
 {
     if (buffer == null)
     {
         _buffer = new BsonBuffer();
         _disposeBuffer = true; // only call Dispose if we allocated the buffer
     }
     else
     {
         _buffer = buffer;
         _disposeBuffer = false;
     }
     _binaryReaderSettings = settings; // already frozen by base class
     _context = new BsonBinaryReaderContext(null, ContextType.TopLevel, 0, 0);
 }
        /// <summary>
        /// Initializes a new instance of the BsonBinaryReader class.
        /// </summary>
        /// <param name="stream">A stream (BsonBinary does not own the stream and will not Dispose it).</param>
        /// <param name="settings">A BsonBinaryReaderSettings.</param>
        public BsonBinaryReader(Stream stream, BsonBinaryReaderSettings settings)
            : base(settings)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (!stream.CanSeek)
            {
                throw new ArgumentException("The stream must be capable of seeking.", "stream");
            }

            _streamReader = new BsonStreamReader(stream, settings.Encoding);
            _settings     = settings; // already frozen by base class

            _context = new BsonBinaryReaderContext(null, ContextType.TopLevel, 0, 0);
        }
Esempio n. 25
0
        /// <summary>
        /// Initializes a new instance of the BsonBinaryReader class.
        /// </summary>
        /// <param name="stream">A stream (BsonBinary does not own the stream and will not Dispose it).</param>
        /// <param name="settings">A BsonBinaryReaderSettings.</param>
        public BsonBinaryReader(Stream stream, BsonBinaryReaderSettings settings)
            : base(settings)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (!stream.CanSeek)
            {
                throw new ArgumentException("The stream must be capable of seeking.", "stream");
            }

            _baseStream = stream;
            _bsonStream = (stream as BsonStream) ?? new BsonStreamAdapter(stream);

            _context = new BsonBinaryReaderContext(null, ContextType.TopLevel, 0, 0);
        }
        /// <summary>
        /// Initializes a new instance of the BsonBinaryReader class.
        /// </summary>
        /// <param name="stream">A stream (BsonBinary does not own the stream and will not Dispose it).</param>
        /// <param name="settings">A BsonBinaryReaderSettings.</param>
        public BsonBinaryReader(Stream stream, BsonBinaryReaderSettings settings)
            : base(settings)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (!stream.CanSeek)
            {
                throw new ArgumentException("The stream must be capable of seeking.", "stream");
            }

            _streamReader = new BsonStreamReader(stream, settings.Encoding);
            _settings = settings; // already frozen by base class

            _context = new BsonBinaryReaderContext(null, ContextType.TopLevel, 0, 0);
        }
        /// <summary>
        /// Reads a BSON JavaScript with scope from the reader (call ReadStartDocument next to read the scope).
        /// </summary>
        /// <returns>A string.</returns>
        public override string ReadJavaScriptWithScope()
        {
            if (disposed)
            {
                ThrowObjectDisposedException();
            }
            VerifyBsonType("ReadJavaScriptWithScope", BsonType.JavaScriptWithScope);

            var startPosition = buffer.Position; // position of size field
            var size          = ReadSize();

            context = new BsonBinaryReaderContext(context, ContextType.JavaScriptWithScope, startPosition, size);
            var code = buffer.ReadString();

            state = BsonReaderState.ScopeDocument;
            return(code);
        }
Esempio n. 28
0
 private string GenerateDottedElementName(BsonBinaryReaderContext context, string elementName)
 {
     if (context.ContextType == ContextType.Document)
     {
         return(GenerateDottedElementName(context.ParentContext, (context.CurrentElementName ?? "?") + "." + elementName));
     }
     else if (context.ContextType == ContextType.Array)
     {
         var indexElementName = context.CurrentArrayIndex.ToString(NumberFormatInfo.InvariantInfo);
         return(GenerateDottedElementName(context.ParentContext, indexElementName + "." + elementName));
     }
     else if (context.ParentContext != null)
     {
         return(GenerateDottedElementName(context.ParentContext, "?." + elementName));
     }
     else
     {
         return(elementName);
     }
 }
 public override void ReturnToBookmark(
     BsonBinaryReaderBookmark bookmark
 ) {
     context = bookmark.Context;
     state = bookmark.State;
     currentBsonType = bookmark.CurrentBsonType;
     buffer.Position = bookmark.Position;
 }
 private string GenerateDottedElementName(BsonBinaryReaderContext context, string elementName)
 {
     if (context.ContextType == ContextType.Document)
     {
         return GenerateDottedElementName(context.ParentContext, (context.CurrentElementName ?? "?") + "." + elementName);
     }
     else if (context.ContextType == ContextType.Array)
     {
         var indexElementName = context.CurrentArrayIndex.ToString(NumberFormatInfo.InvariantInfo);
         return GenerateDottedElementName(context.ParentContext, indexElementName + "." + elementName);
     }
     else if (context.ParentContext != null)
     {
         return GenerateDottedElementName(context.ParentContext, "?." + elementName);
     }
     else
     {
         return elementName;
     }
 }
Esempio n. 31
0
        /// <summary>
        /// Reads the end of a BSON document from the reader.
        /// </summary>
        public override void ReadEndDocument() {
            if (disposed) { ThrowObjectDisposedException(); }
            if (
                context.ContextType != ContextType.Document &&
                context.ContextType != ContextType.ScopeDocument
            ) {
                var message = string.Format("ReadEndDocument cannot be called when ContextType is: {0}", context.ContextType);
                throw new InvalidOperationException(message);
            }
            if (state == BsonReaderState.Type) {
                ReadBsonType(); // will set state to EndOfDocument if at end of document
            }
            if (state != BsonReaderState.EndOfDocument) {
                var message = string.Format("ReadEndDocument cannot be called when State is: {0}", state);
                throw new InvalidOperationException(message);
            }

            context = context.PopContext(buffer.Position);
            if (context != null && context.ContextType == ContextType.JavaScriptWithScope) {
                context = context.PopContext(buffer.Position); // JavaScriptWithScope
            }
            switch (context.ContextType) {
                case ContextType.Array: state = BsonReaderState.Type; break;
                case ContextType.Document: state = BsonReaderState.Type; break;
                case ContextType.TopLevel: state = BsonReaderState.Done; break;
                default: throw new BsonInternalException("Unexpected ContextType");
            }
        }
        /// <summary>
        /// Reads the end of a BSON document from the reader.
        /// </summary>
        public override void ReadEndDocument()
        {
            if (Disposed) { ThrowObjectDisposedException(); }
            if (_context.ContextType != ContextType.Document && _context.ContextType != ContextType.ScopeDocument)
            {
                ThrowInvalidContextType("ReadEndDocument", _context.ContextType, ContextType.Document, ContextType.ScopeDocument);
            }
            if (State == BsonReaderState.Type)
            {
                ReadBsonType(); // will set state to EndOfDocument if at end of document
            }
            if (State != BsonReaderState.EndOfDocument)
            {
                ThrowInvalidState("ReadEndDocument", BsonReaderState.EndOfDocument);
            }

            _context = _context.PopContext(_bsonStream.Position);
            if (_context.ContextType == ContextType.JavaScriptWithScope)
            {
                _context = _context.PopContext(_bsonStream.Position); // JavaScriptWithScope
            }
            switch (_context.ContextType)
            {
                case ContextType.Array: State = BsonReaderState.Type; break;
                case ContextType.Document: State = BsonReaderState.Type; break;
                case ContextType.TopLevel: State = BsonReaderState.Initial; break;
                default: throw new BsonInternalException("Unexpected ContextType.");
            }
        }
        /// <summary>
        /// Reads a BSON JavaScript with scope from the reader (call ReadStartDocument next to read the scope).
        /// </summary>
        /// <returns>A string.</returns>
        public override string ReadJavaScriptWithScope()
        {
            if (Disposed) { ThrowObjectDisposedException(); }
            VerifyBsonType("ReadJavaScriptWithScope", BsonType.JavaScriptWithScope);

            var startPosition = _bsonStream.Position; // position of size field
            var size = ReadSize();
            _context = new BsonBinaryReaderContext(_context, ContextType.JavaScriptWithScope, startPosition, size);
            var code = _bsonStream.ReadString(_settings.Encoding);

            State = BsonReaderState.ScopeDocument;
            return code;
        }
 /// <summary>
 /// Returns the reader to previously bookmarked position and state.
 /// </summary>
 /// <param name="bookmark">The bookmark.</param>
 public override void ReturnToBookmark(BsonReaderBookmark bookmark)
 {
     var binaryReaderBookmark = (BsonBinaryReaderBookmark)bookmark;
     State = binaryReaderBookmark.State;
     CurrentBsonType = binaryReaderBookmark.CurrentBsonType;
     CurrentName = binaryReaderBookmark.CurrentName;
     _context = binaryReaderBookmark.CloneContext();
     _bsonStream.Position = binaryReaderBookmark.Position;
 }
        /// <summary>
        /// Reads the end of a BSON array from the reader.
        /// </summary>
        public override void ReadEndArray() {
            if (disposed) { ThrowObjectDisposedException(); }
            if (context.ContextType != ContextType.Array) {
                ThrowInvalidContextType("ReadEndArray", context.ContextType, ContextType.Array);
            }
            if (state == BsonReaderState.Type) {
                ReadBsonType(); // will set state to EndOfArray if at end of array
            }
            if (state != BsonReaderState.EndOfArray) {
                ThrowInvalidState("ReadEndArray", BsonReaderState.EndOfArray);
            }

            context = context.PopContext(buffer.Position);
            switch (context.ContextType) {
                case ContextType.Array: state = BsonReaderState.Type; break;
                case ContextType.Document: state = BsonReaderState.Type; break;
                case ContextType.TopLevel: state = BsonReaderState.Done; break;
                default: throw new BsonInternalException("Unexpected ContextType.");
            }
        }
        /// <summary>
        /// Reads a BSON JavaScript with scope from the reader (call ReadStartDocument next to read the scope).
        /// </summary>
        /// <returns>A string.</returns>
        public override string ReadJavaScriptWithScope() {
            if (disposed) { ThrowObjectDisposedException(); }
            VerifyBsonType("ReadJavaScriptWithScope", BsonType.JavaScriptWithScope);

            var startPosition = buffer.Position; // position of size field
            var size = ReadSize();
            context = new BsonBinaryReaderContext(context, ContextType.JavaScriptWithScope, startPosition, size);
            var code = buffer.ReadString();

            state = BsonReaderState.ScopeDocument;
            return code;
        }
        /// <summary>
        /// Reads the start of a BSON document.
        /// </summary>
        public override void ReadStartDocument() {
            if (disposed) { ThrowObjectDisposedException(); }
            VerifyBsonType("ReadStartDocument", BsonType.Document);

            var contextType = (state == BsonReaderState.ScopeDocument) ? ContextType.ScopeDocument : ContextType.Document;
            var startPosition = buffer.Position; // position of size field
            var size = ReadSize();
            context = new BsonBinaryReaderContext(context, contextType, startPosition, size);
            state = BsonReaderState.Type;
        }
        public override void ReadStartDocument() {
            if (disposed) { ThrowObjectDisposedException(); }
            if (
                state != BsonReadState.Initial &&
                state != BsonReadState.Done &&
                state != BsonReadState.ScopeDocument &&
                (state != BsonReadState.Value || currentBsonType != BsonType.Document)
            ) {
                string message = string.Format("ReadStartDocument cannot be called when ReadState is: {0} and BsonType is: {1}", state, currentBsonType);
                throw new InvalidOperationException(message);
            }

            var contextType = (state == BsonReadState.ScopeDocument) ? ContextType.ScopeDocument : ContextType.Document;
            var startPosition = buffer.Position; // position of size field
            var size = ReadSize();
            context = new BsonBinaryReaderContext(context, contextType, startPosition, size);
            state = BsonReadState.Type;
        }
        /// <summary>
        /// Reads a raw BSON document.
        /// </summary>
        /// <returns>
        /// The raw BSON document.
        /// </returns>
        public override IByteBuffer ReadRawBsonDocument()
        {
            if (Disposed) { ThrowObjectDisposedException(); }
            VerifyBsonType("ReadRawBsonDocument", BsonType.Document);

            var slice = ReadSlice();

            if (_context.ContextType == ContextType.JavaScriptWithScope)
            {
                _context = _context.PopContext(_streamReader.Position); // JavaScriptWithScope
            }
            switch (_context.ContextType)
            {
                case ContextType.Array: State = BsonReaderState.Type; break;
                case ContextType.Document: State = BsonReaderState.Type; break;
                case ContextType.TopLevel: State = BsonReaderState.Done; break;
                default: throw new BsonInternalException("Unexpected ContextType.");
            }

            return slice;
        }
        public override void ReadEndArray() {
            if (disposed) { ThrowObjectDisposedException(); }
            if (context.ContextType != ContextType.Array) {
                var message = string.Format("ReadEndArray cannot be called when ContextType is: {0}", context.ContextType);
                throw new InvalidOperationException(message);
            }
            if (state == BsonReadState.Type && buffer.PeekByte() == 0) {
                buffer.Skip(1); // automatically advance to EndOfDocument state
                state = BsonReadState.EndOfDocument;
            }
            if (state != BsonReadState.EndOfDocument) {
                var message = string.Format("ReadEndArray cannot be called when ReadState is: {0}", state);
                throw new InvalidOperationException(message);
            }

            context = context.PopContext(buffer.Position);
            state = BsonReadState.Type;
        }
        /// <summary>
        /// Reads the end of a BSON document from the reader.
        /// </summary>
        public override void ReadEndDocument() {
            if (disposed) { ThrowObjectDisposedException(); }
            if (
                context.ContextType != ContextType.Document &&
                context.ContextType != ContextType.ScopeDocument
            ) {
                ThrowInvalidContextType("ReadEndDocument", context.ContextType, ContextType.Document, ContextType.ScopeDocument);
            }
            if (state == BsonReaderState.Type) {
                ReadBsonType(); // will set state to EndOfDocument if at end of document
            }
            if (state != BsonReaderState.EndOfDocument) {
                ThrowInvalidState("ReadEndDocument", BsonReaderState.EndOfDocument);
            }

            context = context.PopContext(buffer.Position);
            if (context != null && context.ContextType == ContextType.JavaScriptWithScope) {
                context = context.PopContext(buffer.Position); // JavaScriptWithScope
            }
            switch (context.ContextType) {
                case ContextType.Array: state = BsonReaderState.Type; break;
                case ContextType.Document: state = BsonReaderState.Type; break;
                case ContextType.TopLevel: state = BsonReaderState.Done; break;
                default: throw new BsonInternalException("Unexpected ContextType.");
            }
        }
        public override void ReadEndDocument() {
            if (disposed) { ThrowObjectDisposedException(); }
            if (
                context.ContextType != ContextType.Document &&
                context.ContextType != ContextType.ScopeDocument
            ) {
                var message = string.Format("ReadEndDocument cannot be called when ContextType is: {0}", context.ContextType);
                throw new InvalidOperationException(message);
            }
            if (state == BsonReadState.Type && buffer.PeekByte() == 0) {
                buffer.Skip(1); // automatically advance to EndOfDocument state
                state = BsonReadState.EndOfDocument;
            }
            if (state != BsonReadState.EndOfDocument) {
                var message = string.Format("ReadEndDocument cannot be called when ReadState is: {0}", state);
                throw new InvalidOperationException(message);
            }

            context = context.PopContext(buffer.Position);
            if (context != null && context.ContextType == ContextType.JavaScriptWithScope) {
                context = context.PopContext(buffer.Position); // JavaScriptWithScope
            }
            state = (context == null) ? BsonReadState.Done : BsonReadState.Type;
        }
        /// <summary>
        /// Reads the start of a BSON array.
        /// </summary>
        public override void ReadStartArray() {
            if (disposed) { ThrowObjectDisposedException(); }
            VerifyBsonType("ReadStartArray", BsonType.Array);

            var startPosition = buffer.Position; // position of size field
            var size = ReadSize();
            context = new BsonBinaryReaderContext(context, ContextType.Array, startPosition, size);
            state = BsonReaderState.Type;
        }
        public override void ReadStartArray() {
            if (disposed) { ThrowObjectDisposedException(); }
            if (state != BsonReadState.Value || currentBsonType != BsonType.Array) {
                string message = string.Format("ReadStartArray cannot be called when ReadState is: {0} and BsonType is: {1}", state, currentBsonType);
                throw new InvalidOperationException(message);
            }

            var startPosition = buffer.Position; // position of size field
            var size = ReadSize();
            context = new BsonBinaryReaderContext(context, ContextType.Array, startPosition, size);
            state = BsonReadState.Type;
        }
 /// <summary>
 /// Returns the reader to previously bookmarked position and state.
 /// </summary>
 /// <param name="bookmark">The bookmark.</param>
 public override void ReturnToBookmark(
     BsonReaderBookmark bookmark
 ) {
     var binaryReaderBookmark = (BsonBinaryReaderBookmark) bookmark;
     state = binaryReaderBookmark.State;
     currentBsonType = binaryReaderBookmark.CurrentBsonType;
     currentName = binaryReaderBookmark.CurrentName;
     context = binaryReaderBookmark.CloneContext();
     buffer.Position = binaryReaderBookmark.Position;
 }
 public BsonBinaryReaderContext Clone()
 {
     var clone = new BsonBinaryReaderContext();
     clone.parentContext = this.parentContext;
     clone.contextType = this.contextType;
     clone.startPosition = this.startPosition;
     clone.size = this.size;
     return clone;
 }