protected override ReplicationBatchItem CloneInternal(JsonOperationContext context)
        {
            MemoryStream stream = null;

            if (Stream != null)
            {
                stream = new MemoryStream();
                Stream.CopyTo(stream);

                stream.Position = 0;
                Stream.Position = 0;
            }

            var item = new AttachmentReplicationItem
            {
                ContentType = ContentType.Clone(context),
                Name        = Name.Clone(context),
                Stream      = stream
            };

            var baseMem = Base64Hash.CloneToJsonContext(context, out item.Base64Hash);
            var keyMem  = Key.CloneToJsonContext(context, out item.Key);


            item.ToDispose(new DisposableAction(() =>
            {
                context.ReturnMemory(baseMem);
                context.ReturnMemory(keyMem);
            }));

            return(item);
        }
        public static unsafe AttachmentReplicationItem From(DocumentsOperationContext context, Attachment attachment)
        {
            var item = new AttachmentReplicationItem
            {
                Type              = ReplicationItemType.Attachment,
                Etag              = attachment.Etag,
                ChangeVector      = attachment.ChangeVector,
                Name              = attachment.Name,
                ContentType       = attachment.ContentType,
                Base64Hash        = attachment.Base64Hash,
                Stream            = attachment.Stream,
                TransactionMarker = attachment.TransactionMarker
            };

            // although the key is LSV but is treated as slice and doesn't respect escaping
            item.ToDispose(Slice.From(context.Allocator, attachment.Key.Buffer, attachment.Key.Size, ByteStringType.Immutable, out item.Key));
            return(item);
        }