/// <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(); }
/// <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(); }