<commit_before>using System;
using System.IO;
using GLib;
using Hyena;
using TagLib.Image;
using TagLib.Xmp;

namespace FSpot.Utils
{
    public static class SidecarXmpExtensions
    {
        /// <summary>
        ///    Parses the XMP file identified by resource and replaces the XMP
        ///    tag of file by the parsed data.
        /// </summary>
        public static void ParseXmpSidecar (this TagLib.Image.File file, TagLib.File.IFileAbstraction resource)
        {
            string xmp;
            using (var stream = resource.ReadStream) {
                using (var reader = new StreamReader (stream)) {
                    xmp = reader.ReadToEnd ();
                }
            }

            var tag = new XmpTag (xmp);
            var xmp_tag = file.GetTag (TagLib.TagTypes.XMP, true) as XmpTag;
            xmp_tag.ReplaceFrom (tag);
        }

        public static void SaveXmpSidecar (this TagLib.Image.File file, TagLib.File.IFileAbstraction resource)
        {
            var xmp_tag = file.GetTag (TagLib.TagTypes.XMP, false) as XmpTag;
            if (xmp_tag == null)
                return;

            var xmp = xmp_tag.Render ();

            using (var stream = resource.WriteStream) {
                using (var writer = new StreamWriter (stream)) {
                    writer.Write (xmp);
                }
                resource.CloseStream (stream);
            }
        }
    }
}<commit_msg>Truncate stream when writing sidecar XMP files.<commit_after>using System;
using System.IO;
using GLib;
using Hyena;
using TagLib.Image;
using TagLib.Xmp;

namespace FSpot.Utils
{
    public static class SidecarXmpExtensions
    {
        /// <summary>
        ///    Parses the XMP file identified by resource and replaces the XMP
        ///    tag of file by the parsed data.
        /// </summary>
        public static void ParseXmpSidecar (this TagLib.Image.File file, TagLib.File.IFileAbstraction resource)
        {
            string xmp;
            using (var stream = resource.ReadStream) {
                using (var reader = new StreamReader (stream)) {
                    xmp = reader.ReadToEnd ();
                }
            }

            var tag = new XmpTag (xmp);
            var xmp_tag = file.GetTag (TagLib.TagTypes.XMP, true) as XmpTag;
            xmp_tag.ReplaceFrom (tag);
        }

        public static void SaveXmpSidecar (this TagLib.Image.File file, TagLib.File.IFileAbstraction resource)
        {
            var xmp_tag = file.GetTag (TagLib.TagTypes.XMP, false) as XmpTag;
            if (xmp_tag == null)
                return;

            var xmp = xmp_tag.Render ();

            using (var stream = resource.WriteStream) {
                stream.SetLength (0);
                using (var writer = new StreamWriter (stream)) {
                    writer.Write (xmp);
                }
                resource.CloseStream (stream);
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.08791581805838425, 'fraction_numerical': 0.00033944331296673454, 'mean_word_length': 1.6010591350397176, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6625787', 'n_tokens_mistral': 847, 'n_tokens_neox': 816, 'n_words': 232}