Пример #1
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(_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 a raw BSON document.
        /// </summary>
        /// <returns>
        /// The raw BSON document.
        /// </returns>
        public override IByteBuffer ReadRawBsonDocument()
        {
            if (Disposed)
            {
                ThrowObjectDisposedException();
            }
            VerifyBsonType("ReadRawBsonDocument", BsonType.Document);

            var position = _buffer.Position;
            var length   = _buffer.ReadInt32();
            var slice    = _buffer.ByteBuffer.GetSlice(position, length);

            _buffer.Position = position + length;

            if (_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.");
            }

            return(slice);
        }
Пример #3
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(_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.");
            }
        }
Пример #4
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();
            _buffer.Position = binaryReaderBookmark.Position;
        }
 // 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;
 }
 // constructors
 internal BsonBinaryReaderBookmark(
     BsonReaderState state,
     BsonType currentBsonType,
     string currentName,
     BsonBinaryReaderContext context,
     int position)
     : base(state, currentBsonType, currentName)
 {
     _context = context.Clone();
     _position = position;
 }
Пример #8
0
 // constructors
 internal BsonBinaryReaderBookmark(
     BsonReaderState state,
     BsonType currentBsonType,
     string currentName,
     BsonBinaryReaderContext context,
     int position)
     : base(state, currentBsonType, currentName)
 {
     _context  = context.Clone();
     _position = position;
 }
Пример #9
0
        /// <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;
        }
        /// <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);
        }
Пример #11
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 = _buffer.Position; // position of size field
            var size          = ReadSize();

            _context = new BsonBinaryReaderContext(_context, contextType, startPosition, size);
            State    = BsonReaderState.Type;
        }
Пример #12
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);
 }
Пример #13
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);
        }