public override void WriteBinaryData(BsonBinaryData binaryData)
{
if (Disposed) { throw new ObjectDisposedException("BsonBinaryWriter"); }
if (State != BsonWriterState.Value)
{
ThrowInvalidState("WriteBinaryData", BsonWriterState.Value);
}
var bytes = binaryData.Bytes;
var subType = binaryData.SubType;
var guidRepresentation = binaryData.GuidRepresentation;
switch (subType)
{
case BsonBinarySubType.OldBinary:
if (_binaryWriterSettings.FixOldBinarySubTypeOnOutput)
{
subType = BsonBinarySubType.Binary; // replace obsolete OldBinary with new Binary sub type
}
break;
case BsonBinarySubType.UuidLegacy:
case BsonBinarySubType.UuidStandard:
if (_binaryWriterSettings.GuidRepresentation != GuidRepresentation.Unspecified)
{
var expectedSubType = (_binaryWriterSettings.GuidRepresentation == GuidRepresentation.Standard) ? BsonBinarySubType.UuidStandard : BsonBinarySubType.UuidLegacy;
if (subType != expectedSubType)
{
var message = string.Format(
"The GuidRepresentation for the writer is {0}, which requires the subType argument to be {1}, not {2}.",
_binaryWriterSettings.GuidRepresentation, expectedSubType, subType);
throw new BsonSerializationException(message);
}
if (guidRepresentation != _binaryWriterSettings.GuidRepresentation)
{
var message = string.Format(
"The GuidRepresentation for the writer is {0}, which requires the the guidRepresentation argument to also be {0}, not {1}.",
_binaryWriterSettings.GuidRepresentation, guidRepresentation);
throw new BsonSerializationException(message);
}
}
break;
}
_buffer.WriteByte((byte)BsonType.Binary);
WriteNameHelper();
if (subType == BsonBinarySubType.OldBinary)
{
// sub type OldBinary has two sizes (for historical reasons)
_buffer.WriteInt32(bytes.Length + 4);
_buffer.WriteByte((byte)subType);
_buffer.WriteInt32(bytes.Length);
}
else
{
_buffer.WriteInt32(bytes.Length);
_buffer.WriteByte((byte)subType);
}
_buffer.WriteBytes(bytes);
State = GetNextState();
}
#pragma warning restore 618