/// <summary>
        /// Writes a raw BSON array.
        /// </summary>
        /// <param name="slice">The byte buffer containing the raw BSON array.</param>
        public virtual void WriteRawBsonArray(IByteBuffer slice)
        {
            // overridden in BsonBinaryWriter

            using (var bsonBuffer = new BsonBuffer())
            {
                BsonArray array;

                // wrap the array in a fake document so we can deserialize it
                var arrayLength    = slice.Length;
                var documentLength = arrayLength + 8;
                bsonBuffer.WriteInt32(documentLength);
                bsonBuffer.WriteByte((byte)BsonType.Array);
                bsonBuffer.WriteByte((byte)'x');
                bsonBuffer.WriteByte((byte)0);
                bsonBuffer.ByteBuffer.WriteBytes(slice);
                bsonBuffer.WriteByte((byte)0);

                bsonBuffer.Position = 0;
                using (var bsonReader = new BsonBinaryReader(bsonBuffer, true, BsonBinaryReaderSettings.Defaults))
                {
                    bsonReader.ReadStartDocument();
                    bsonReader.ReadName("x");
                    array = (BsonArray)BsonArraySerializer.Instance.Deserialize(bsonReader, typeof(BsonArray), null);
                    bsonReader.ReadEndDocument();
                }

                BsonArraySerializer.Instance.Serialize(this, typeof(BsonArray), array, null);
            }
        }
        /// <summary>
        /// Writes a raw BSON array.
        /// </summary>
        /// <param name="slice">The byte buffer containing the raw BSON array.</param>
        public virtual void WriteRawBsonArray(IByteBuffer slice)
        {
            // overridden in BsonBinaryWriter

            using (var bsonBuffer = new BsonBuffer())
            {
                BsonArray array;

                // wrap the array in a fake document so we can deserialize it
                var arrayLength = slice.Length;
                var documentLength = arrayLength + 8;
                bsonBuffer.WriteInt32(documentLength);
                bsonBuffer.WriteByte((byte)BsonType.Array);
                bsonBuffer.WriteByte((byte)'x');
                bsonBuffer.WriteByte((byte)0);
                bsonBuffer.ByteBuffer.WriteBytes(slice);
                bsonBuffer.WriteByte((byte)0);

                bsonBuffer.Position = 0;
                using (var bsonReader = new BsonBinaryReader(bsonBuffer, true, BsonBinaryReaderSettings.Defaults))
                {
                    bsonReader.ReadStartDocument();
                    bsonReader.ReadName("x");
                    array = (BsonArray)BsonArraySerializer.Instance.Deserialize(bsonReader, typeof(BsonArray), null);
                    bsonReader.ReadEndDocument();
                }

                BsonArraySerializer.Instance.Serialize(this, typeof(BsonArray), array, null);
            }
        }
Esempio n. 3
0
        #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();
        }
Esempio n. 4
0
        #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 (settings.FixOldBinarySubTypeOnOutput)
                {
                    subType = BsonBinarySubType.Binary;     // replace obsolete OldBinary with new Binary sub type
                }
                break;

            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 writer is {0}, which requires the subType argument to be {1}, not {2}.", settings.GuidRepresentation, expectedSubType, subType);
                        throw new BsonSerializationException(message);
                    }
                    if (guidRepresentation != settings.GuidRepresentation)
                    {
                        var message = string.Format("The GuidRepresentation for the writer is {0}, which requires the the guidRepresentation argument to also be {0}, not {1}.", settings.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 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();
        }