/// <summary> /// Writes BSON binary data to the writer. /// </summary> /// <param name="bytes">The binary data.</param> /// <param name="subType">The binary data subtype.</param> public override void WriteBinaryData( byte[] bytes, BsonBinarySubType subType ) { if (disposed) { throw new ObjectDisposedException("JsonWriter"); } if (state != BsonWriterState.Value && state != BsonWriterState.Initial) { var message = string.Format("WriteBinaryData cannot be called when State is: {0}", state); throw new InvalidOperationException(message); } if (settings.OutputMode == JsonOutputMode.Shell) { WriteNameHelper(name); textWriter.Write("BinData({0}, \"{1}\")", (int)subType, Convert.ToBase64String(bytes)); } else { WriteStartDocument(); WriteString("$binary", Convert.ToBase64String(bytes)); WriteString("$type", ((int)subType).ToString("x2")); WriteEndDocument(); } state = GetNextState(); }
/// <summary> /// Writes a BSON binary data element to the writer. /// </summary> /// <param name="bytes">The binary data.</param> /// <param name="subType">The binary data subtype.</param> public override void WriteBinaryData( byte[] bytes, BsonBinarySubType subType ) { var guidRepresentation = (subType == BsonBinarySubType.UuidStandard) ? GuidRepresentation.Standard : GuidRepresentation.Unspecified; WriteBinaryData(bytes, subType, guidRepresentation); }
/// <summary> /// Initializes a new instance of the BsonBinaryData class. /// </summary> /// <param name="bytes">The binary data.</param> /// <param name="subType">The binary data subtype.</param> /// <param name="guidRepresentation">The representation for Guids.</param> public BsonBinaryData( byte[] bytes, BsonBinarySubType subType, GuidRepresentation guidRepresentation ) : base(BsonType.Binary) { if (subType == BsonBinarySubType.UuidStandard || subType == BsonBinarySubType.UuidLegacy) { if (bytes.Length != 16) { var message = string.Format("Length must be 16, not {0}, when subType is {1}.", bytes.Length, subType); throw new ArgumentException(message); } var expectedSubType = (guidRepresentation == GuidRepresentation.Standard) ? BsonBinarySubType.UuidStandard : BsonBinarySubType.UuidLegacy; if (subType != expectedSubType) { var message = string.Format("SubType must be {0}, not {1}, when GuidRepresentation is {2}.", expectedSubType, subType, GuidRepresentation); throw new ArgumentException(message); } } else { if (guidRepresentation != GuidRepresentation.Unspecified) { var message = string.Format("GuidRepresentation must be Unspecified, not {0}, when SubType is not UuidStandard or UuidLegacy.", guidRepresentation); throw new ArgumentException(message); } } this.bytes = bytes; this.subType = subType; this.guidRepresentation = guidRepresentation; }
/// <summary> /// Initializes a new instance of the BsonBinaryData class. /// </summary> /// <param name="bytes">The binary data.</param> /// <param name="subType">The binary data subtype.</param> public BsonBinaryData( byte[] bytes, BsonBinarySubType subType ) : this(bytes, subType, GuidRepresentation.Unspecified) { }
#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 = BsonReadState.Type; }
public void Deserialize_should_throw_when_representation_is_binary_and_sub_type_does_not_match( GuidRepresentationMode defaultGuidRepresentationMode, GuidRepresentation defaultGuidRepresentation, GuidRepresentation serializerGuidRepresentation, GuidRepresentation?readerGuidRepresentation, BsonBinarySubType expectedSubType) { GuidMode.Set(defaultGuidRepresentationMode, defaultGuidRepresentation); var subject = new GuidSerializer(serializerGuidRepresentation); var documentBytes = new byte[] { 29, 0, 0, 0, 5, 120, 0, 16, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 0 }; var nonMatchingSubType = expectedSubType == BsonBinarySubType.UuidLegacy ? BsonBinarySubType.UuidStandard : BsonBinarySubType.UuidLegacy; documentBytes[11] = (byte)nonMatchingSubType; var readerSettings = new BsonBinaryReaderSettings(); if (defaultGuidRepresentationMode == GuidRepresentationMode.V2 && readerGuidRepresentation.HasValue) { #pragma warning disable 618 readerSettings.GuidRepresentation = readerGuidRepresentation.Value; #pragma warning restore 618 } var reader = new BsonBinaryReader(new MemoryStream(documentBytes), readerSettings); reader.ReadStartDocument(); reader.ReadName("x"); var context = BsonDeserializationContext.CreateRoot(reader); var args = new BsonDeserializationArgs(); var exception = Record.Exception(() => subject.Deserialize(context, args)); exception.Should().BeOfType <FormatException>(); }
/// <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(); }
/***************************************************/ public override object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) { IBsonReader reader = context.Reader; BsonType currentBsonType = reader.GetCurrentBsonType(); switch (currentBsonType) { case BsonType.Array: if (context.DynamicArraySerializer != null) { return(context.DynamicArraySerializer.Deserialize(context)); } break; case BsonType.Binary: { BsonBinaryData bsonBinaryData = reader.ReadBinaryData(); BsonBinarySubType subType = bsonBinaryData.SubType; if (subType == BsonBinarySubType.UuidStandard || subType == BsonBinarySubType.UuidLegacy) { return(bsonBinaryData.ToGuid()); } break; } case BsonType.Boolean: return(reader.ReadBoolean()); case BsonType.DateTime: return(new BsonDateTime(reader.ReadDateTime()).ToUniversalTime()); case BsonType.Decimal128: return(reader.ReadDecimal128()); case BsonType.Document: return(DeserializeDiscriminatedValue(context, args)); case BsonType.Double: return(reader.ReadDouble()); case BsonType.Int32: return(reader.ReadInt32()); case BsonType.Int64: return(reader.ReadInt64()); case BsonType.Null: reader.ReadNull(); return(null); case BsonType.ObjectId: return(reader.ReadObjectId()); case BsonType.String: return(reader.ReadString()); } Engine.Reflection.Compute.RecordError($"ObjectSerializer does not support BSON type '{currentBsonType}'."); return(null); }
/// <summary> /// Writes BSON binary data to the writer. /// </summary> /// <param name="bytes">The binary data.</param> /// <param name="subType">The binary data subtype.</param> /// <param name="guidRepresentation">The representation for Guids.</param> public override void WriteBinaryData( byte[] bytes, BsonBinarySubType subType, GuidRepresentation guidRepresentation // ignored for now (until we figure out how to represent this in the generated JSON) ) { if (disposed) { throw new ObjectDisposedException("JsonWriter"); } if (state != BsonWriterState.Value && state != BsonWriterState.Initial) { ThrowInvalidState("WriteBinaryData", BsonWriterState.Value, BsonWriterState.Initial); } if (settings.OutputMode == JsonOutputMode.Shell) { WriteNameHelper(name); textWriter.Write("new BinData({0}, \"{1}\")", (int)subType, Convert.ToBase64String(bytes)); } else { WriteStartDocument(); WriteString("$binary", Convert.ToBase64String(bytes)); WriteString("$type", ((int)subType).ToString("x2")); WriteEndDocument(); } state = GetNextState(); }
private string GuidToString(BsonBinarySubType subType, byte[] bytes, GuidRepresentation guidRepresentation) { if (bytes.Length != 16) { var message = string.Format("Length of binary subtype {0} must be 16, not {1}.", subType, bytes.Length); throw new ArgumentException(message); } if (subType == BsonBinarySubType.UuidLegacy) { if (guidRepresentation == GuidRepresentation.Standard) { throw new ArgumentException("GuidRepresentation for binary subtype UuidLegacy must not be Standard."); } } if (subType == BsonBinarySubType.UuidStandard) { if (guidRepresentation == GuidRepresentation.Unspecified) { guidRepresentation = GuidRepresentation.Standard; } if (guidRepresentation != GuidRepresentation.Standard) { var message = string.Format("GuidRepresentation for binary subtype UuidStandard must be Standard, not {0}.", guidRepresentation); throw new ArgumentException(message); } } if (guidRepresentation == GuidRepresentation.Unspecified) { var s = BsonUtils.ToHexString(bytes); var parts = new string[] { s.Substring(0, 8), s.Substring(8, 4), s.Substring(12, 4), s.Substring(16, 4), s.Substring(20, 12) }; return(string.Format("HexData({0}, \"{1}\")", (int)subType, string.Join("-", parts))); } else { string uuidConstructorName; switch (guidRepresentation) { case GuidRepresentation.CSharpLegacy: uuidConstructorName = "CSUUID"; break; case GuidRepresentation.JavaLegacy: uuidConstructorName = "JUUID"; break; case GuidRepresentation.PythonLegacy: uuidConstructorName = "PYUUID"; break; case GuidRepresentation.Standard: uuidConstructorName = "UUID"; break; default: throw new BsonInternalException("Unexpected GuidRepresentation"); } var guid = GuidConverter.FromBytes(bytes, guidRepresentation); return(string.Format("{0}(\"{1}\")", uuidConstructorName, guid.ToString())); } }
/// <summary> /// Initializes a new instance of the BsonBinaryData class. /// </summary> /// <param name="guid">A Guid.</param> public BsonBinaryData( Guid guid ) : base(BsonType.Binary) { this.bytes = guid.ToByteArray(); this.subType = BsonBinarySubType.Uuid; }
/// <summary> /// Initializes a new instance of the BsonBinaryData class. /// </summary> /// <param name="bytes">The binary data.</param> public BsonBinaryData( byte[] bytes ) : base(BsonType.Binary) { this.bytes = bytes; this.subType = BsonBinarySubType.Binary; }
/// <summary> /// Writes a BSON binary data element to the writer. /// </summary> /// <param name="name">The name of the element.</param> /// <param name="bytes">The binary data.</param> /// <param name="subType">The binary data subtype.</param> public override void WriteBinaryData( string name, byte[] bytes, BsonBinarySubType subType ) { WriteName(name); WriteBinaryData(bytes, subType); }
/// <summary> /// Writes a binary sub type to the stream. /// </summary> /// <param name="stream">The stream.</param> /// <param name="value">The value.</param> public static void WriteBinarySubType(this BsonStream stream, BsonBinarySubType value) { if (stream == null) { throw new ArgumentNullException("stream"); } stream.WriteByte((byte)value); }
/// <summary> /// Writes a BSON binary data element to the writer. /// </summary> /// <param name="name">The name of the element.</param> /// <param name="bytes">The binary data.</param> /// <param name="subType">The binary data subtype.</param> public void WriteBinaryData( string name, byte[] bytes, BsonBinarySubType subType ) { WriteName(name); WriteBinaryData(bytes, subType); }
/// <summary> /// Writes a BSON binary data element to the writer. /// </summary> /// <param name="bytes">The binary data.</param> /// <param name="subType">The binary data subtype.</param> public void WriteBinaryData( byte[] bytes, BsonBinarySubType subType ) { var guidRepresentation = (subType == BsonBinarySubType.UuidStandard) ? GuidRepresentation.Standard : GuidRepresentation.Unspecified; WriteBinaryData(bytes, subType, guidRepresentation); }
/// <summary> /// Initializes a new instance of the BsonBinaryData class. /// </summary> /// <param name="bytes">The binary data.</param> /// <param name="subType">The binary data subtype.</param> public BsonBinaryData( byte[] bytes, BsonBinarySubType subType ) : base(BsonType.Binary) { this.bytes = bytes; this.subType = subType; }
public void WriteBinaryData( string name, byte[] bytes, BsonBinarySubType subType, GuidRepresentation guidRepresentation) { WriteName(name); WriteBinaryData(bytes, subType, guidRepresentation); }
public void WriteBinaryData( byte[] bytes, BsonBinarySubType subType, GuidRepresentation guidRepresentation) { var binaryData = new BsonBinaryData(bytes, subType, guidRepresentation); WriteBinaryData(binaryData); }
public void ReadJson_should_return_expected_result_when_using_native_json_reader(string json, string nullableHexBytes, BsonBinarySubType subType) { var subject = new BsonBinaryDataConverter(); var expectedResult = nullableHexBytes == null ? null : new BsonBinaryData(BsonUtils.ParseHexString(nullableHexBytes), subType); var result = ReadJsonUsingNativeJsonReader<BsonBinaryData>(subject, json); result.Should().Be(expectedResult); }
public void ReadBinaryData( string name, out byte[] bytes, out BsonBinarySubType subType, out GuidRepresentation guidRepresentation) { VerifyName(name); ReadBinaryData(out bytes, out subType, out guidRepresentation); }
/// <summary> /// Reads a BSON binary data element from the reader. /// </summary> /// <param name="name">The name of the element.</param> /// <param name="bytes">The binary data.</param> /// <param name="subType">The binary data subtype.</param> public override void ReadBinaryData( string name, out byte[] bytes, out BsonBinarySubType subType ) { VerifyName(name); ReadBinaryData(out bytes, out subType); }
/// <summary> /// Reads BSON binary data from the reader. /// </summary> /// <param name="bytes">The binary data.</param> /// <param name="subType">The binary data subtype.</param> public void ReadBinaryData( out byte[] bytes, out BsonBinarySubType subType ) { GuidRepresentation guidRepresentation; ReadBinaryData(out bytes, out subType, out guidRepresentation); }
public void TestBinaryData(string json, byte[] expectedBytes, BsonBinarySubType expectedSubType) { using (var reader = new JsonReader(json)) { var result = reader.ReadBinaryData(); result.Should().Be(new BsonBinaryData(expectedBytes, expectedSubType)); reader.IsAtEndOfFile().Should().BeTrue(); } }
/// <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 (_binaryReaderSettings.FixOldBinarySubTypeOnInput) { subType = BsonBinarySubType.Binary; // replace obsolete OldBinary with new Binary sub type } } switch (subType) { case BsonBinarySubType.UuidLegacy: case 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; break; default: guidRepresentation = GuidRepresentation.Unspecified; break; } bytes = _buffer.ReadBytes(size); State = GetNextState(); }
public void ReadBinaryData( out byte[] bytes, out BsonBinarySubType subType, out GuidRepresentation guidRepresentation) { var binaryData = ReadBinaryData(); bytes = binaryData.Bytes; subType = binaryData.SubType; guidRepresentation = binaryData.GuidRepresentation; }
#pragma warning restore 618 /// <summary> /// Initializes a new instance of the BsonBinaryData class. /// </summary> /// <param name="guid">A Guid.</param> /// <param name="guidRepresentation">The representation for Guids.</param> public BsonBinaryData(Guid guid, GuidRepresentation guidRepresentation) { _bytes = GuidConverter.ToBytes(guid, guidRepresentation); _subType = GuidConverter.GetSubType(guidRepresentation); #pragma warning disable 618 if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2) { _guidRepresentation = guidRepresentation; } #pragma warning restore 618 }
/// <summary> /// Creates a new BsonBinaryData. /// </summary> /// <param name="bytes">The binary data.</param> /// <param name="subType">The binary data subtype.</param> /// <returns>A BsonBinaryData or null.</returns> public static BsonBinaryData Create(byte[] bytes, BsonBinarySubType subType) { if (bytes != null) { return(new BsonBinaryData(bytes, subType)); } else { return(null); } }
/// <summary> /// Creates a new BsonBinaryData. /// </summary> /// <param name="bytes">The binary data.</param> /// <param name="subType">The binary data subtype.</param> /// <param name="guidRepresentation">The representation for Guids.</param> /// <returns>A BsonBinaryData or null.</returns> public static BsonBinaryData Create(byte[] bytes, BsonBinarySubType subType, GuidRepresentation guidRepresentation) { if (bytes != null) { return(new BsonBinaryData(bytes, subType, guidRepresentation)); } else { return(null); } }
/// <summary> /// Reads BSON binary data from the reader. /// </summary> /// <param name="bytes">The binary data.</param> /// <param name="subType">The binary data subtype.</param> public override void ReadBinaryData( out byte[] bytes, out BsonBinarySubType subType ) { if (disposed) { ThrowObjectDisposedException(); } VerifyBsonType("ReadBinaryData", BsonType.Binary); state = GetNextState(); var binaryData = currentValue.AsBsonBinaryData; bytes = binaryData.Bytes; subType = binaryData.SubType; }
public void ReadBinarySubType_should_return_expected_result(int n, BsonBinarySubType expectedResult) { var bytes = new byte[] { (byte)n }; using (var memoryStream = new MemoryStream(bytes)) using (var stream = new BsonStreamAdapter(memoryStream)) { var result = stream.ReadBinarySubType(); result.Should().Be(expectedResult); } }
/// <summary> /// Initializes a new instance of the BsonBinaryData class. /// </summary> /// <param name="bytes">The binary data.</param> /// <param name="subType">The binary data subtype.</param> /// <param name="guidRepresentation">The representation for Guids.</param> public BsonBinaryData(byte[] bytes, BsonBinarySubType subType, GuidRepresentation guidRepresentation) { if (bytes == null) { throw new ArgumentNullException(nameof(bytes)); } if (subType == BsonBinarySubType.UuidStandard || subType == BsonBinarySubType.UuidLegacy) { if (bytes.Length != 16) { var message = string.Format( "Length must be 16, not {0}, when subType is {1}.", bytes.Length, subType); throw new ArgumentException(message, nameof(bytes)); } BsonBinarySubType expectedSubType; switch (guidRepresentation) { case GuidRepresentation.CSharpLegacy: case GuidRepresentation.JavaLegacy: case GuidRepresentation.PythonLegacy: case GuidRepresentation.Unspecified: expectedSubType = BsonBinarySubType.UuidLegacy; break; case GuidRepresentation.Standard: expectedSubType = BsonBinarySubType.UuidStandard; break; default: throw new ArgumentException($"Invalid guidRepresentation: {guidRepresentation}.", nameof(guidRepresentation)); } if (subType != expectedSubType) { throw new ArgumentException($"GuidRepresentation {guidRepresentation} is only valid with subType {expectedSubType}, not with subType {subType}.", nameof(guidRepresentation)); } } else { if (guidRepresentation != GuidRepresentation.Unspecified) { var message = string.Format( "GuidRepresentation must be Unspecified, not {0}, when subType is not UuidStandard or UuidLegacy.", guidRepresentation); throw new ArgumentException(message, nameof(guidRepresentation)); } } _bytes = bytes; _subType = subType; _guidRepresentation = 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> /// <param name="guidRepresentation">The representation for Guids.</param> public override void ReadBinaryData( out byte[] bytes, out BsonBinarySubType subType, out GuidRepresentation guidRepresentation) { if (Disposed) { ThrowObjectDisposedException(); } VerifyBsonType("ReadBinaryData", BsonType.Binary); State = GetNextState(); var binaryData = _currentValue.AsBsonBinaryData; bytes = binaryData.Bytes; subType = binaryData.SubType; guidRepresentation = binaryData.GuidRepresentation; }
public void constructor_should_throw_when_sub_type_is_not_uuid_and_guid_representation_is_not_unspecified( [Values(BsonBinarySubType.Binary)] BsonBinarySubType subType, [Values(GuidRepresentation.CSharpLegacy, GuidRepresentation.Standard)] GuidRepresentation guidRepresentation) { var bytes = new byte[0]; var exception = Record.Exception(() => new BsonBinaryData(bytes, subType, guidRepresentation)); var e = exception.Should().BeOfType <ArgumentException>().Subject; e.Message.Should().StartWith($"GuidRepresentation must be Unspecified, not {guidRepresentation}, when subType is not UuidStandard or UuidLegacy."); e.ParamName.Should().Be("guidRepresentation"); }
public void WriteBinarySubType_should_have_expected_effect( BsonBinarySubType value, byte expectedByte) { using (var memoryStream = new MemoryStream()) using (var stream = new BsonStreamAdapter(memoryStream)) { var expectedBytes = new byte[] { expectedByte }; stream.WriteBinarySubType(value); memoryStream.ToArray().Should().Equal(expectedBytes); } }
public override void WriteBinaryData( byte[] bytes, BsonBinarySubType subType ) { if (disposed) { throw new ObjectDisposedException("BsonBinaryWriter"); } if (state != BsonWriterState.Value) { var message = string.Format("WriteBinaryData cannot be called when State is: {0}", state); throw new InvalidOperationException(message); } WriteValue(new BsonBinaryData(bytes, subType)); state = GetNextState(); }
/// <summary> /// Writes BSON binary data to the writer. /// </summary> /// <param name="bytes">The binary data.</param> /// <param name="subType">The binary data subtype.</param> /// <param name="guidRepresentation">The representation for Guids.</param> public override void WriteBinaryData(byte[] bytes, BsonBinarySubType subType, GuidRepresentation guidRepresentation) { if (disposed) { throw new ObjectDisposedException("BsonDocumentWriter"); } if (state != BsonWriterState.Value) { ThrowInvalidState("WriteBinaryData", BsonWriterState.Value); } WriteValue(new BsonBinaryData(bytes, subType, guidRepresentation)); state = GetNextState(); }
/// <summary> /// Writes BSON binary data to the writer. /// </summary> /// <param name="bytes">The binary data.</param> /// <param name="subType">The binary data subtype.</param> public override void WriteBinaryData( byte[] bytes, BsonBinarySubType subType ) { if (disposed) { throw new ObjectDisposedException("JsonWriter"); } if (state != BsonWriterState.Value && state != BsonWriterState.Initial) { var message = string.Format("WriteBinaryData cannot be called when State is: {0}", state); throw new InvalidOperationException(message); } WriteStartDocument(); WriteString("$binary", Convert.ToBase64String(bytes)); WriteString("$type", ((int) subType).ToString("x2")); WriteEndDocument(); state = GetNextState(); }
/// <summary> /// Writes BSON binary data to the writer. /// </summary> /// <param name="bytes">The binary data.</param> /// <param name="subType">The binary data subtype.</param> /// <param name="guidRepresentation">The representation for Guids.</param> public override void WriteBinaryData( byte[] bytes, BsonBinarySubType subType, GuidRepresentation guidRepresentation // ignored for now (until we figure out how to represent this in the generated JSON) ) { if (disposed) { throw new ObjectDisposedException("JsonWriter"); } if (state != BsonWriterState.Value && state != BsonWriterState.Initial) { ThrowInvalidState("WriteBinaryData", BsonWriterState.Value, BsonWriterState.Initial); } if (settings.OutputMode == JsonOutputMode.Shell) { WriteNameHelper(name); textWriter.Write("new BinData({0}, \"{1}\")", (int) subType, Convert.ToBase64String(bytes)); } else { WriteStartDocument(); WriteString("$binary", Convert.ToBase64String(bytes)); WriteString("$type", ((int) subType).ToString("x2")); WriteEndDocument(); } state = GetNextState(); }
/// <summary> /// Writes BSON binary data to the writer. /// </summary> /// <param name="bytes">The binary data.</param> /// <param name="subType">The binary data subtype.</param> public override void WriteBinaryData( byte[] bytes, BsonBinarySubType subType ) { if (disposed) { throw new ObjectDisposedException("JsonWriter"); } if (state != BsonWriterState.Value && state != BsonWriterState.Initial) { var message = string.Format("WriteBinaryData cannot be called when State is: {0}", state); throw new InvalidOperationException(message); } if (settings.OutputMode == JsonOutputMode.Shell) { WriteNameHelper(name); textWriter.Write("new BinData({0}, \"{1}\")", (int) subType, Convert.ToBase64String(bytes)); } else { WriteStartDocument(); WriteString("$binary", Convert.ToBase64String(bytes)); WriteString("$type", ((int) subType).ToString("x2")); WriteEndDocument(); } 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(); }
/// <summary> /// Reads a BSON binary data element from the reader. /// </summary> /// <param name="name">The name of the element.</param> /// <param name="bytes">The binary data.</param> /// <param name="subType">The binary data subtype.</param> public void ReadBinaryData(string name, out byte[] bytes, out BsonBinarySubType subType) { GuidRepresentation guidRepresentation; ReadBinaryData(name, out bytes, out subType, out guidRepresentation); }
/// <summary> /// Reads a BSON binary data element from the reader. /// </summary> /// <param name="name">The name of the element.</param> /// <param name="bytes">The binary data.</param> /// <param name="subType">The binary data subtype.</param> /// <param name="guidRepresentation">The representation for Guids.</param> public void ReadBinaryData(string name, out byte[] bytes, out BsonBinarySubType subType, out GuidRepresentation guidRepresentation) { VerifyName(name); ReadBinaryData(out bytes, out subType, out guidRepresentation); }
#pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary /// <summary> /// Writes BSON binary data to the writer. /// </summary> /// <param name="bytes">The binary data.</param> /// <param name="subType">The binary data subtype.</param> /// <param name="guidRepresentation">The representation for Guids.</param> public override void WriteBinaryData( byte[] bytes, BsonBinarySubType subType, GuidRepresentation guidRepresentation) { if (Disposed) { throw new ObjectDisposedException("BsonBinaryWriter"); } if (State != BsonWriterState.Value) { ThrowInvalidState("WriteBinaryData", BsonWriterState.Value); } 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(); }
/// <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> public abstract void ReadBinaryData(out byte[] bytes, out BsonBinarySubType subType, out GuidRepresentation guidRepresentation);
/// <summary> /// Writes BSON binary data to the writer. /// </summary> /// <param name="bytes">The binary data.</param> /// <param name="subType">The binary data subtype.</param> /// <param name="guidRepresentation">The respresentation for Guids.</param> public abstract void WriteBinaryData(byte[] bytes, BsonBinarySubType subType, GuidRepresentation guidRepresentation);
/// <summary> /// Writes BSON binary data to the writer. /// </summary> /// <param name="bytes">The binary data.</param> /// <param name="subType">The binary data subtype.</param> /// <param name="guidRepresentation">The representation for Guids.</param> public override void WriteBinaryData( byte[] bytes, BsonBinarySubType subType, GuidRepresentation guidRepresentation) { if (Disposed) { throw new ObjectDisposedException("JsonWriter"); } if (State != BsonWriterState.Value && State != BsonWriterState.Initial) { ThrowInvalidState("WriteBinaryData", BsonWriterState.Value, BsonWriterState.Initial); } if (_jsonWriterSettings.OutputMode == JsonOutputMode.Shell) { WriteNameHelper(Name); switch (subType) { case BsonBinarySubType.UuidLegacy: case BsonBinarySubType.UuidStandard: if (bytes.Length != 16) { var message = string.Format("Length of binary subtype {0} must be 16, not {1}.", subType, bytes.Length); throw new ArgumentException(message); } if (subType == BsonBinarySubType.UuidLegacy && guidRepresentation == GuidRepresentation.Standard) { throw new ArgumentException("GuidRepresentation for binary subtype UuidLegacy must not be Standard."); } if (subType == BsonBinarySubType.UuidStandard && guidRepresentation != GuidRepresentation.Standard) { var message = string.Format("GuidRepresentation for binary subtype UuidStandard must be Standard, not {0}.", guidRepresentation); throw new ArgumentException(message); } if (_jsonWriterSettings.ShellVersion >= new Version(2, 0, 0)) { if (guidRepresentation == GuidRepresentation.Unspecified) { var s = BsonUtils.ToHexString(bytes); var parts = new string[] { s.Substring(0, 8), s.Substring(8, 4), s.Substring(12, 4), s.Substring(16, 4), s.Substring(20, 12) }; _textWriter.Write("HexData({0}, \"{1}\")", (int)subType, string.Join("-", parts)); } else { string uuidConstructorName; switch (guidRepresentation) { case GuidRepresentation.CSharpLegacy: uuidConstructorName = "CSUUID"; break; case GuidRepresentation.JavaLegacy: uuidConstructorName = "JUUID"; break; case GuidRepresentation.PythonLegacy: uuidConstructorName = "PYUUID"; break; case GuidRepresentation.Standard: uuidConstructorName = "UUID"; break; default: throw new BsonInternalException("Unexpected GuidRepresentation"); } var guid = GuidConverter.FromBytes(bytes, guidRepresentation); _textWriter.Write("{0}(\"{1}\")", uuidConstructorName, guid.ToString()); } } else { _textWriter.Write("new BinData({0}, \"{1}\")", (int)subType, Convert.ToBase64String(bytes)); } break; default: _textWriter.Write("new BinData({0}, \"{1}\")", (int)subType, Convert.ToBase64String(bytes)); break; } } else { WriteStartDocument(); WriteString("$binary", Convert.ToBase64String(bytes)); WriteString("$type", ((int)subType).ToString("x2")); WriteEndDocument(); } State = GetNextState(); }
private string GuidToString(BsonBinarySubType subType, byte[] bytes, GuidRepresentation guidRepresentation) { if (bytes.Length != 16) { var message = string.Format("Length of binary subtype {0} must be 16, not {1}.", subType, bytes.Length); throw new ArgumentException(message); } if (subType == BsonBinarySubType.UuidLegacy && guidRepresentation == GuidRepresentation.Standard) { throw new ArgumentException("GuidRepresentation for binary subtype UuidLegacy must not be Standard."); } if (subType == BsonBinarySubType.UuidStandard && guidRepresentation != GuidRepresentation.Standard) { var message = string.Format("GuidRepresentation for binary subtype UuidStandard must be Standard, not {0}.", guidRepresentation); throw new ArgumentException(message); } if (guidRepresentation == GuidRepresentation.Unspecified) { var s = BsonUtils.ToHexString(bytes); var parts = new string[] { s.Substring(0, 8), s.Substring(8, 4), s.Substring(12, 4), s.Substring(16, 4), s.Substring(20, 12) }; return string.Format("HexData({0}, \"{1}\")", (int)subType, string.Join("-", parts)); } else { string uuidConstructorName; switch (guidRepresentation) { case GuidRepresentation.CSharpLegacy: uuidConstructorName = "CSUUID"; break; case GuidRepresentation.JavaLegacy: uuidConstructorName = "JUUID"; break; case GuidRepresentation.PythonLegacy: uuidConstructorName = "PYUUID"; break; case GuidRepresentation.Standard: uuidConstructorName = "UUID"; break; default: throw new BsonInternalException("Unexpected GuidRepresentation"); } var guid = GuidConverter.FromBytes(bytes, guidRepresentation); return string.Format("{0}(\"{1}\")", uuidConstructorName, guid.ToString()); } }
#pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary /// <summary> /// Writes BSON binary data to the writer. /// </summary> /// <param name="bytes">The binary data.</param> /// <param name="subType">The binary data subtype.</param> /// <param name="guidRepresentation">The representation for Guids.</param> public override void WriteBinaryData( byte[] bytes, BsonBinarySubType subType, GuidRepresentation guidRepresentation // ignored since BSON doesn't have a place to store this ) { if (disposed) { throw new ObjectDisposedException("BsonBinaryWriter"); } if (state != BsonWriterState.Value) { ThrowInvalidState("WriteBinaryData", BsonWriterState.Value); } buffer.WriteByte((byte) BsonType.Binary); WriteNameHelper(); if (subType == BsonBinarySubType.OldBinary && settings.FixOldBinarySubTypeOnOutput) { subType = BsonBinarySubType.Binary; // replace obsolete OldBinary with new Binary sub type } 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(); }
public abstract void ReadBinaryData( string name, out byte[] bytes, out BsonBinarySubType subType );
/// <summary> /// Writes a BSON binary data element to the writer. /// </summary> /// <param name="name">The name of the element.</param> /// <param name="bytes">The binary data.</param> /// <param name="subType">The binary data subtype.</param> public abstract void WriteBinaryData( string name, byte[] bytes, BsonBinarySubType subType );
#pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary /// <summary> /// Writes BSON binary data to the writer. /// </summary> /// <param name="bytes">The binary data.</param> /// <param name="subType">The binary data subtype.</param> public override void WriteBinaryData( byte[] bytes, BsonBinarySubType subType ) { if (disposed) { throw new ObjectDisposedException("BsonBinaryWriter"); } if (state != BsonWriterState.Value) { var message = string.Format("WriteBinaryData cannot be called when State is: {0}", state); throw new InvalidOperationException(message); } buffer.WriteByte((byte) BsonType.Binary); WriteNameHelper(); if (subType == BsonBinarySubType.OldBinary && settings.FixOldBinarySubTypeOnOutput) { subType = BsonBinarySubType.Binary; // replace obsolete OldBinary with new Binary sub type } 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(); }
/// <summary> /// Writes BSON binary data to the writer. /// </summary> /// <param name="bytes">The binary data.</param> /// <param name="subType">The binary data subtype.</param> /// <param name="guidRepresentation">The representation for Guids.</param> public override void WriteBinaryData(byte[] bytes, BsonBinarySubType subType, GuidRepresentation guidRepresentation) { if (_disposed) { throw new ObjectDisposedException("BsonDocumentWriter"); } if (_state != BsonWriterState.Value) { ThrowInvalidState("WriteBinaryData", BsonWriterState.Value); } WriteValue(new BsonBinaryData(bytes, subType, guidRepresentation)); _state = GetNextState(); }