コード例 #1
0
        /// <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));
        }