public void BsonBinaryReader_should_support_reading_more_than_2GB()
        {
            RequireEnvironmentVariable.IsDefined("EXPLICIT");

            var binaryData = new BsonBinaryData(new byte[1024 * 1024]);

            var tempFileName = Path.GetTempFileName();
            try
            {
                using (var stream = new FileStream(tempFileName, FileMode.Open))
                {
                    using (var binaryWriter = new BsonBinaryWriter(stream))
                    {
                        while (stream.Position < (long)int.MaxValue * 4)
                        {
                            binaryWriter.WriteStartDocument();
                            binaryWriter.WriteName("x");
                            binaryWriter.WriteBinaryData(binaryData);
                            binaryWriter.WriteEndDocument();
                        }
                    }

                    var endOfFilePosition = stream.Position;
                    stream.Position = 0;

                    using (var binaryReader = new BsonBinaryReader(stream))
                    {
                        while (!binaryReader.IsAtEndOfFile())
                        {
                            binaryReader.ReadStartDocument();
                            var bookmark = binaryReader.GetBookmark();

                            binaryReader.ReadName("x");
                            binaryReader.ReturnToBookmark(bookmark);

                            binaryReader.ReadName("x");
                            var readBinaryData = binaryReader.ReadBinaryData();
                            Assert.Equal(binaryData.Bytes.Length, readBinaryData.Bytes.Length);

                            binaryReader.ReadEndDocument();
                        }
                    }

                    Assert.Equal(endOfFilePosition, stream.Position);
                }
            }
            finally
            {
                try
                {
                    File.Delete(tempFileName);
                }
                catch
                {
                    // ignore exceptions
                }
            }
        }
        public void BsonBinaryReader_should_support_reading_multiple_documents(
            [Range(0, 3)]
            int numberOfDocuments)
        {
            var document = new BsonDocument("x", 1);
            var bson = document.ToBson();
            var input = Enumerable.Repeat(bson, numberOfDocuments).Aggregate(Enumerable.Empty<byte>(), (a, b) => a.Concat(b)).ToArray();
            var expectedResult = Enumerable.Repeat(document, numberOfDocuments);

            using (var stream = new MemoryStream(input))
            using (var binaryReader = new BsonBinaryReader(stream))
            {
                var result = new List<BsonDocument>();

                while (!binaryReader.IsAtEndOfFile())
                {
                    binaryReader.ReadStartDocument();
                    var name = binaryReader.ReadName();
                    var value = binaryReader.ReadInt32();
                    binaryReader.ReadEndDocument();

                    var resultDocument = new BsonDocument(name, value);
                    result.Add(resultDocument);
                }

                result.Should().Equal(expectedResult);
            }
        }
Example #3
0
        /// <summary>
        /// Writes a raw BSON array.
        /// </summary>
        /// <param name="slice">The byte buffer containing the raw BSON array.</param>
        public virtual void WriteRawBsonArray(IByteBuffer slice)
        {
            // overridden in BsonBinaryWriter to write the raw bytes to the stream
            // for all other streams, deserialize the raw bytes and serialize the resulting array instead

            var documentLength = slice.Length + 8;

            using (var memoryStream = new MemoryStream(documentLength))
            {
                // wrap the array in a fake document so we can deserialize it
                var streamWriter = new BsonStreamWriter(memoryStream, Utf8Helper.StrictUtf8Encoding);
                streamWriter.WriteInt32(documentLength);
                streamWriter.WriteBsonType(BsonType.Array);
                streamWriter.WriteByte((byte)'x');
                streamWriter.WriteByte(0);
                slice.WriteTo(streamWriter.BaseStream);
                streamWriter.WriteByte(0);

                memoryStream.Position = 0;
                using (var bsonReader = new BsonBinaryReader(memoryStream, BsonBinaryReaderSettings.Defaults))
                {
                    var deserializationContext = BsonDeserializationContext.CreateRoot <BsonDocument>(bsonReader);
                    bsonReader.ReadStartDocument();
                    bsonReader.ReadName("x");
                    var array = deserializationContext.DeserializeWithChildContext(BsonArraySerializer.Instance);
                    bsonReader.ReadEndDocument();

                    var serializationContext = BsonSerializationContext.CreateRoot <BsonArray>(this);
                    BsonArraySerializer.Instance.Serialize(serializationContext, array);
                }
            }
        }
        /// <summary>
        /// Writes a raw BSON array.
        /// </summary>
        /// <param name="slice">The byte buffer containing the raw BSON array.</param>
        public virtual void WriteRawBsonArray(IByteBuffer slice)
        {
            // overridden in BsonBinaryWriter to write the raw bytes to the stream
            // for all other streams, deserialize the raw bytes and serialize the resulting array instead

            using (var chunkSource = new InputBufferChunkSource(BsonChunkPool.Default))
                using (var buffer = new MultiChunkBuffer(chunkSource))
                    using (var stream = new ByteBufferStream(buffer))
                    {
                        // wrap the array in a fake document so we can deserialize it
                        var documentLength = slice.Length + 8;
                        buffer.EnsureCapacity(documentLength);
                        stream.WriteInt32(documentLength);
                        stream.WriteBsonType(BsonType.Array);
                        stream.WriteByte((byte)'x');
                        stream.WriteByte(0);
                        stream.WriteSlice(slice);
                        stream.WriteByte(0);
                        buffer.MakeReadOnly();

                        stream.Position = 0;
                        using (var reader = new BsonBinaryReader(stream, BsonBinaryReaderSettings.Defaults))
                        {
                            var deserializationContext = BsonDeserializationContext.CreateRoot(reader);
                            reader.ReadStartDocument();
                            reader.ReadName("x");
                            var array = BsonArraySerializer.Instance.Deserialize(deserializationContext);
                            reader.ReadEndDocument();

                            var serializationContext = BsonSerializationContext.CreateRoot(this);
                            BsonArraySerializer.Instance.Serialize(serializationContext, array);
                        }
                    }
        }
Example #5
0
        /// <summary>
        /// Writes a raw BSON array.
        /// </summary>
        /// <param name="slice">The byte buffer containing the raw BSON array.</param>
        public virtual void WriteRawBsonArray(IByteBuffer slice)
        {
            // overridden in BsonBinaryWriter

            using (var bsonBuffer = new BsonBuffer())
            {
                BsonArray array;

                // wrap the array in a fake document so we can deserialize it
                var arrayLength    = slice.Length;
                var documentLength = arrayLength + 8;
                bsonBuffer.WriteInt32(documentLength);
                bsonBuffer.WriteByte((byte)BsonType.Array);
                bsonBuffer.WriteByte((byte)'x');
                bsonBuffer.WriteByte((byte)0);
                bsonBuffer.ByteBuffer.WriteBytes(slice);
                bsonBuffer.WriteByte((byte)0);

                bsonBuffer.Position = 0;
                using (var bsonReader = new BsonBinaryReader(bsonBuffer, true, BsonBinaryReaderSettings.Defaults))
                {
                    bsonReader.ReadStartDocument();
                    bsonReader.ReadName("x");
                    array = (BsonArray)BsonArraySerializer.Instance.Deserialize(bsonReader, typeof(BsonArray), null);
                    bsonReader.ReadEndDocument();
                }

                BsonArraySerializer.Instance.Serialize(this, typeof(BsonArray), array, null);
            }
        }
 public void TestHelloWorld()
 {
     string byteString = @"\x16\x00\x00\x00\x02hello\x00\x06\x00\x00\x00world\x00\x00";
     byte[] bytes = DecodeByteString(byteString);
     var stream = new MemoryStream(bytes);
     using (var bsonReader = new BsonBinaryReader(stream))
     {
         bsonReader.ReadStartDocument();
         Assert.AreEqual(BsonType.String, bsonReader.ReadBsonType());
         Assert.AreEqual("hello", bsonReader.ReadName());
         Assert.AreEqual("world", bsonReader.ReadString());
         bsonReader.ReadEndDocument();
     }
 }
 public void TestBsonAwesome()
 {
     string byteString = @"1\x00\x00\x00\x04BSON\x00&\x00\x00\x00\x020\x00\x08\x00\x00\x00awesome\x00\x011\x00333333\x14@\x102\x00\xc2\x07\x00\x00\x00\x00";
     byte[] bytes = DecodeByteString(byteString);
     MemoryStream stream = new MemoryStream(bytes);
     using (BsonReader bsonReader = new BsonBinaryReader(stream))
     {
         bsonReader.ReadStartDocument();
         Assert.AreEqual(BsonType.Array, bsonReader.ReadBsonType());
         Assert.AreEqual("BSON", bsonReader.ReadName());
         bsonReader.ReadStartArray();
         Assert.AreEqual(BsonType.String, bsonReader.ReadBsonType());
         Assert.AreEqual("awesome", bsonReader.ReadString());
         Assert.AreEqual(BsonType.Double, bsonReader.ReadBsonType());
         Assert.AreEqual(5.05, bsonReader.ReadDouble());
         Assert.AreEqual(BsonType.Int32, bsonReader.ReadBsonType());
         Assert.AreEqual(1986, bsonReader.ReadInt32());
         bsonReader.ReadEndArray();
         bsonReader.ReadEndDocument();
     }
 }
        /// <summary>
        /// Tests whether the document contains an element with the specified name.
        /// </summary>
        /// <param name="name">The name of the element to look for.</param>
        /// <returns>
        /// True if the document contains an element with the specified name.
        /// </returns>
        public override bool Contains(string name)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            ThrowIfDisposed();

            using (var stream = new ByteBufferStream(_slice, ownsBuffer: false))
            using (var bsonReader = new BsonBinaryReader(stream, _readerSettings))
            {
                bsonReader.ReadStartDocument();
                while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
                {
                    if (bsonReader.ReadName() == name)
                    {
                        return true;
                    }
                    bsonReader.SkipValue();
                }
                bsonReader.ReadEndDocument();

                return false;
            }
        }
        private void DeserializeThisLevel()
        {
            var elements = new List<BsonElement>();

            using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), true), true, _readerSettings))
            {
                bsonReader.ReadStartDocument();
                BsonType bsonType;
                while ((bsonType = bsonReader.ReadBsonType()) != BsonType.EndOfDocument)
                {
                    var name = bsonReader.ReadName();
                    BsonValue value;
                    switch (bsonType)
                    {
                        case BsonType.Array: value = DeserializeLazyBsonArray(bsonReader); break;
                        case BsonType.Document: value = DeserializeLazyBsonDocument(bsonReader); break;
                        default: value = (BsonValue)BsonValueSerializer.Instance.Deserialize(bsonReader, typeof(BsonValue), null); break;
                    }
                    elements.Add(new BsonElement(name, value));
                }
                bsonReader.ReadEndDocument();
            }

            var slice = _slice;
            try
            {
                _slice = null;
                AddRange(elements);
                slice.Dispose();
            }
            catch
            {
                try { Clear(); } catch { }
                _slice = slice;
                throw;
            }
        }
        /// <summary>
        /// Writes a raw BSON array.
        /// </summary>
        /// <param name="slice">The byte buffer containing the raw BSON array.</param>
        public virtual void WriteRawBsonArray(IByteBuffer slice)
        {
            // overridden in BsonBinaryWriter to write the raw bytes to the stream
            // for all other streams, deserialize the raw bytes and serialize the resulting array instead

            using (var chunkSource = new InputBufferChunkSource(BsonChunkPool.Default))
            using (var buffer = new MultiChunkBuffer(chunkSource))
            using (var stream = new ByteBufferStream(buffer))
            {
                // wrap the array in a fake document so we can deserialize it
                var documentLength = slice.Length + 8;
                buffer.EnsureCapacity(documentLength);
                stream.WriteInt32(documentLength);
                stream.WriteBsonType(BsonType.Array);
                stream.WriteByte((byte)'x');
                stream.WriteByte(0);
                stream.WriteSlice(slice);
                stream.WriteByte(0);
                buffer.MakeReadOnly();

                stream.Position = 0;
                using (var reader = new BsonBinaryReader(stream, BsonBinaryReaderSettings.Defaults))
                {
                    var deserializationContext = BsonDeserializationContext.CreateRoot(reader);
                    reader.ReadStartDocument();
                    reader.ReadName("x");
                    var array = BsonArraySerializer.Instance.Deserialize(deserializationContext);
                    reader.ReadEndDocument();

                    var serializationContext = BsonSerializationContext.CreateRoot(this);
                    BsonArraySerializer.Instance.Serialize(serializationContext, array);
                }
            }
        }
        /// <summary>
        /// Tries to get the value of an element of this document.
        /// </summary>
        /// <param name="name">The name of the element.</param>
        /// <param name="value">The value of the element.</param>
        /// <returns>
        /// True if an element with that name was found.
        /// </returns>
        public override bool TryGetValue(string name, out BsonValue value)
        {
            ThrowIfDisposed();
            using (var stream = new ByteBufferStream(_slice, ownsBuffer: false))
            using (var bsonReader = new BsonBinaryReader(stream, _readerSettings))
            {
                var context = BsonDeserializationContext.CreateRoot(bsonReader);
                
                bsonReader.ReadStartDocument();
                while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
                {
                    if (bsonReader.ReadName() == name)
                    {
                        value = DeserializeBsonValue(context);
                        return true;
                    }

                    bsonReader.SkipValue();
                }
                bsonReader.ReadEndDocument();

                value = null;
                return false;
            }
        }
        /// <summary>
        /// Gets an element of this document.
        /// </summary>
        /// <param name="index">The zero based index of the element.</param>
        /// <returns>
        /// The element.
        /// </returns>
        public override BsonElement GetElement(int index)
        {
            if (index < 0)
            {
                throw new ArgumentOutOfRangeException("index");
            }
            ThrowIfDisposed();

            using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), true), true, _readerSettings))
            {
                bsonReader.ReadStartDocument();
                var i = 0;
                while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
                {
                    if (i == index)
                    {
                        var name = bsonReader.ReadName();
                        var value = DeserializeBsonValue(bsonReader);
                        return new BsonElement(name, value);
                    }

                    bsonReader.SkipName();
                    bsonReader.SkipValue();
                    i++;
                }
                bsonReader.ReadEndDocument();

                throw new ArgumentOutOfRangeException("index");
            }
        }
        private IEnumerable<BsonElement> MaterializeThisLevel()
        {
            var elements = new List<BsonElement>();

            using (var stream = new ByteBufferStream(_slice, ownsBuffer: false))
            using (var bsonReader = new BsonBinaryReader(stream, _readerSettings))
            {
                var context = BsonDeserializationContext.CreateRoot(bsonReader);

                bsonReader.ReadStartDocument();
                BsonType bsonType;
                while ((bsonType = bsonReader.ReadBsonType()) != BsonType.EndOfDocument)
                {
                    var name = bsonReader.ReadName();
                    BsonValue value;
                    switch (bsonType)
                    {
                        case BsonType.Array: value = DeserializeLazyBsonArray(bsonReader); break;
                        case BsonType.Document: value = DeserializeLazyBsonDocument(bsonReader); break;
                        default: value = BsonValueSerializer.Instance.Deserialize(context); break;
                    }
                    elements.Add(new BsonElement(name, value));
                }
                bsonReader.ReadEndDocument();
            }

            return elements;
        }
Example #14
0
        /// <summary>
        /// Writes a raw BSON array.
        /// </summary>
        /// <param name="slice">The byte buffer containing the raw BSON array.</param>
        public virtual void WriteRawBsonArray(IByteBuffer slice)
        {
            // overridden in BsonBinaryWriter to write the raw bytes to the stream
            // for all other streams, deserialize the raw bytes and serialize the resulting array instead

            var documentLength = slice.Length + 8;
            using (var memoryStream = new MemoryStream(documentLength))
            {
                // wrap the array in a fake document so we can deserialize it
                var streamWriter = new BsonStreamWriter(memoryStream, Utf8Helper.StrictUtf8Encoding);
                streamWriter.WriteInt32(documentLength);
                streamWriter.WriteBsonType(BsonType.Array);
                streamWriter.WriteByte((byte)'x');
                streamWriter.WriteByte(0);
                slice.WriteTo(streamWriter.BaseStream);
                streamWriter.WriteByte(0);

                memoryStream.Position = 0;
                using (var bsonReader = new BsonBinaryReader(memoryStream, BsonBinaryReaderSettings.Defaults))
                {
                    var deserializationContext = BsonDeserializationContext.CreateRoot<BsonDocument>(bsonReader);
                    bsonReader.ReadStartDocument();
                    bsonReader.ReadName("x");
                    var array = deserializationContext.DeserializeWithChildContext(BsonArraySerializer.Instance);
                    bsonReader.ReadEndDocument();

                    var serializationContext = BsonSerializationContext.CreateRoot<BsonArray>(this);
                    BsonArraySerializer.Instance.Serialize(serializationContext, array);
                }
            }
        }
        private IEnumerable<BsonElement> MaterializeThisLevel()
        {
            var elements = new List<BsonElement>();

            using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), true), true, _readerSettings))
            {
                bsonReader.ReadStartDocument();
                BsonType bsonType;
                while ((bsonType = bsonReader.ReadBsonType()) != BsonType.EndOfDocument)
                {
                    var name = bsonReader.ReadName();
                    BsonValue value;
                    switch (bsonType)
                    {
                        case BsonType.Array: value = DeserializeLazyBsonArray(bsonReader); break;
                        case BsonType.Document: value = DeserializeLazyBsonDocument(bsonReader); break;
                        default: value = (BsonValue)BsonValueSerializer.Instance.Deserialize(bsonReader, typeof(BsonValue), null); break;
                    }
                    elements.Add(new BsonElement(name, value));
                }
                bsonReader.ReadEndDocument();
            }

            return elements;
        }
        /// <summary>
        /// Gets an element of this document.
        /// </summary>
        /// <param name="index">The zero based index of the element.</param>
        /// <returns>
        /// The element.
        /// </returns>
        public override BsonElement GetElement(int index)
        {
            if (index < 0)
            {
                throw new ArgumentOutOfRangeException("index");
            }
            ThrowIfDisposed();

            using (var stream = new ByteBufferStream(_slice, ownsBuffer: false))
            using (var bsonReader = new BsonBinaryReader(stream, _readerSettings))
            {
                var context = BsonDeserializationContext.CreateRoot(bsonReader);
                
                bsonReader.ReadStartDocument();
                var i = 0;
                while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
                {
                    if (i == index)
                    {
                        var name = bsonReader.ReadName();
                        var value = DeserializeBsonValue(context);
                        return new BsonElement(name, value);
                    }

                    bsonReader.SkipName();
                    bsonReader.SkipValue();
                    i++;
                }
                bsonReader.ReadEndDocument();

                throw new ArgumentOutOfRangeException("index");
            }
        }
        /// <summary>
        /// Gets an enumerator that can be used to enumerate the elements of this document.
        /// </summary>
        /// <returns>
        /// An enumerator.
        /// </returns>
        public override IEnumerator<BsonElement> GetEnumerator()
        {
            ThrowIfDisposed();
            using (var stream = new ByteBufferStream(_slice, ownsBuffer: false))
            using (var bsonReader = new BsonBinaryReader(stream, _readerSettings))
            {
                var context = BsonDeserializationContext.CreateRoot(bsonReader);

                bsonReader.ReadStartDocument();
                while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
                {
                    var name = bsonReader.ReadName();
                    var value = DeserializeBsonValue(context);
                    yield return new BsonElement(name, value);
                }
                bsonReader.ReadEndDocument();
            }
        }
 /// <summary>
 /// Gets an enumerator that can be used to enumerate the elements of this document.
 /// </summary>
 /// <returns>
 /// An enumerator.
 /// </returns>
 public override IEnumerator<BsonElement> GetEnumerator()
 {
     ThrowIfDisposed();
     using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), true), true, _readerSettings))
     {
         bsonReader.ReadStartDocument();
         while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
         {
             var name = bsonReader.ReadName();
             var value = DeserializeBsonValue(bsonReader);
             yield return new BsonElement(name, value);
         }
         bsonReader.ReadEndDocument();
     }
 }
        /// <summary>
        /// Writes a raw BSON array.
        /// </summary>
        /// <param name="slice">The byte buffer containing the raw BSON array.</param>
        public virtual void WriteRawBsonArray(IByteBuffer slice)
        {
            // overridden in BsonBinaryWriter

            using (var bsonBuffer = new BsonBuffer())
            {
                BsonArray array;

                // wrap the array in a fake document so we can deserialize it
                var arrayLength = slice.Length;
                var documentLength = arrayLength + 8;
                bsonBuffer.WriteInt32(documentLength);
                bsonBuffer.WriteByte((byte)BsonType.Array);
                bsonBuffer.WriteByte((byte)'x');
                bsonBuffer.WriteByte((byte)0);
                bsonBuffer.ByteBuffer.WriteBytes(slice);
                bsonBuffer.WriteByte((byte)0);

                bsonBuffer.Position = 0;
                using (var bsonReader = new BsonBinaryReader(bsonBuffer, true, BsonBinaryReaderSettings.Defaults))
                {
                    bsonReader.ReadStartDocument();
                    bsonReader.ReadName("x");
                    array = (BsonArray)BsonArraySerializer.Instance.Deserialize(bsonReader, typeof(BsonArray), null);
                    bsonReader.ReadEndDocument();
                }

                BsonArraySerializer.Instance.Serialize(this, typeof(BsonArray), array, null);
            }
        }
        /// <summary>
        /// Tries to get the value of an element of this document.
        /// </summary>
        /// <param name="name">The name of the element.</param>
        /// <param name="value">The value of the element.</param>
        /// <returns>
        /// True if an element with that name was found.
        /// </returns>
        public override bool TryGetValue(string name, out BsonValue value)
        {
            ThrowIfDisposed();
            using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), true), true, _readerSettings))
            {
                bsonReader.ReadStartDocument();
                while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
                {
                    if (bsonReader.ReadName() == name)
                    {
                        value = DeserializeBsonValue(bsonReader);
                        return true;
                    }

                    bsonReader.SkipValue();
                }
                bsonReader.ReadEndDocument();

                value = null;
                return false;
            }
        }