/// <summary>
        /// Reads BSON binary data from the reader.
        /// </summary>
        /// <returns>A BsonBinaryData.</returns>
#pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary
        public override BsonBinaryData ReadBinaryData()
        {
            if (Disposed)
            {
                ThrowObjectDisposedException();
            }
            VerifyBsonType("ReadBinaryData", BsonType.Binary);

            int size = ReadSize();

            var subType = (BsonBinarySubType)_buffer.ReadByte();

            if (subType == BsonBinarySubType.OldBinary)
            {
                // sub type OldBinary has two sizes (for historical reasons)
                int size2 = ReadSize();
                if (size2 != size - 4)
                {
                    throw new FileFormatException("Binary sub type OldBinary has inconsistent sizes");
                }
                size = size2;

                if (_binaryReaderSettings.FixOldBinarySubTypeOnInput)
                {
                    subType = BsonBinarySubType.Binary; // replace obsolete OldBinary with new Binary sub type
                }
            }

            var bytes = _buffer.ReadBytes(size);

            var guidRepresentation = GuidRepresentation.Unspecified;

            if (subType == BsonBinarySubType.UuidLegacy || subType == BsonBinarySubType.UuidStandard)
            {
                if (_binaryReaderSettings.GuidRepresentation != GuidRepresentation.Unspecified)
                {
                    var expectedSubType = (_binaryReaderSettings.GuidRepresentation == GuidRepresentation.Standard) ? BsonBinarySubType.UuidStandard : BsonBinarySubType.UuidLegacy;
                    if (subType != expectedSubType)
                    {
                        var message = string.Format(
                            "The GuidRepresentation for the reader is {0}, which requires the binary sub type to be {1}, not {2}.",
                            _binaryReaderSettings.GuidRepresentation, expectedSubType, subType);
                        throw new FileFormatException(message);
                    }
                }
                guidRepresentation = (subType == BsonBinarySubType.UuidStandard) ? GuidRepresentation.Standard : _binaryReaderSettings.GuidRepresentation;
            }

            State = GetNextState();
            return(new BsonBinaryData(bytes, subType, guidRepresentation));
        }
        /// <summary>
        /// Reads BSON binary data from the reader.
        /// </summary>
        /// <param name="bytes">The binary data.</param>
        /// <param name="subType">The binary data subtype.</param>
        #pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary
        public override void ReadBinaryData(
            out byte[] bytes,
            out BsonBinarySubType subType
            )
        {
            if (disposed)
            {
                ThrowObjectDisposedException();
            }
            VerifyBsonType("ReadBinaryData", BsonType.Binary);

            int size = ReadSize();

            subType = (BsonBinarySubType)buffer.ReadByte();
            if (subType == BsonBinarySubType.OldBinary)
            {
                // sub type OldBinary has two sizes (for historical reasons)
                int size2 = ReadSize();
                if (size2 != size - 4)
                {
                    throw new FileFormatException("Binary sub type OldBinary has inconsistent sizes");
                }
                size = size2;

                if (settings.FixOldBinarySubTypeOnInput)
                {
                    subType = BsonBinarySubType.Binary; // replace obsolete OldBinary with new Binary sub type
                }
            }
            bytes = buffer.ReadBytes(size);

            state = GetNextState();
        }
 internal byte[] RemoveLastDocument(BsonBuffer buffer)
 {
     var lastDocumentLength = buffer.Position - _lastDocumentStartPosition;
     buffer.Position = _lastDocumentStartPosition;
     var lastDocument = buffer.ReadBytes(lastDocumentLength);
     buffer.Length = _lastDocumentStartPosition;
     BackpatchMessageLength(buffer);
     return lastDocument;
 }
Exemple #4
0
        /// <summary>
        /// Reads BSON binary data from the reader.
        /// </summary>
        /// <param name="bytes">The binary data.</param>
        /// <param name="subType">The binary data subtype.</param>
        /// <param name="guidRepresentation">The representation for Guids.</param>
        #pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary
        public override void ReadBinaryData(
            out byte[] bytes,
            out BsonBinarySubType subType,
            out GuidRepresentation guidRepresentation
            )
        {
            if (disposed)
            {
                ThrowObjectDisposedException();
            }
            VerifyBsonType("ReadBinaryData", BsonType.Binary);

            int size = ReadSize();

            subType = (BsonBinarySubType)buffer.ReadByte();
            if (subType == BsonBinarySubType.OldBinary)
            {
                // sub type OldBinary has two sizes (for historical reasons)
                int size2 = ReadSize();
                if (size2 != size - 4)
                {
                    throw new FileFormatException("Binary sub type OldBinary has inconsistent sizes");
                }
                size = size2;

                if (settings.FixOldBinarySubTypeOnInput)
                {
                    subType = BsonBinarySubType.Binary; // replace obsolete OldBinary with new Binary sub type
                }
            }
            switch (subType)
            {
            case BsonBinarySubType.UuidLegacy:
            case BsonBinarySubType.UuidStandard:
                if (settings.GuidRepresentation != GuidRepresentation.Unspecified)
                {
                    var expectedSubType = (settings.GuidRepresentation == GuidRepresentation.Standard) ? BsonBinarySubType.UuidStandard : BsonBinarySubType.UuidLegacy;
                    if (subType != expectedSubType)
                    {
                        var message = string.Format("The GuidRepresentation for the reader is {0}, which requires the binary sub type to be {1}, not {2}.", settings.GuidRepresentation, expectedSubType, subType);
                        throw new FileFormatException(message);
                    }
                }
                guidRepresentation = (subType == BsonBinarySubType.UuidStandard) ? GuidRepresentation.Standard : settings.GuidRepresentation;
                break;

            default:
                guidRepresentation = GuidRepresentation.Unspecified;
                break;
            }
            bytes = buffer.ReadBytes(size);

            state = GetNextState();
        }
            private IByteBuffer RemoveOverflow(BsonBuffer buffer)
            {
                var lastRequestLength = buffer.Position - _lastRequestPosition;
                buffer.Position = _lastRequestPosition;
                var lastArrayItem = buffer.ReadBytes(lastRequestLength);
                if ((BsonType)lastArrayItem[0] != BsonType.Document)
                {
                    throw new MongoInternalException("Expected overflow item to be a BsonDocument.");
                }
                var sliceOffset = Array.IndexOf<byte>(lastArrayItem, 0) + 1; // skip over type and array index
                var overflow = new ByteArrayBuffer(lastArrayItem, sliceOffset, lastArrayItem.Length - sliceOffset, true);
                buffer.Position = _lastRequestPosition;
                buffer.Length = _lastRequestPosition;

                _batchCount--;
                _batchLength = buffer.Position - _batchStartPosition;

                return overflow;
            }
        private byte[] RemoveLastDocument(BsonBuffer buffer)
        {
            var lastDocumentLength = buffer.Position - _lastDocumentStartPosition;
            buffer.Position = _lastDocumentStartPosition;
            var lastDocument = buffer.ReadBytes(lastDocumentLength);
            buffer.Position = _lastDocumentStartPosition;
            buffer.Length = _lastDocumentStartPosition;

            _batchCount -= 1;
            _batchLength = buffer.Position - _batchStartPosition;

            return lastDocument;
        }