Ejemplo n.º 1
0
        public async Task SaveAsync()
        {
            var fileInfo = this.directory.CreateFileInfoInDirectory("SaveAsyncTest.cfg");

            using (var stream = PooledMemoryStream.Borrow())
            {
                using var writer = new StreamWriter(stream);
                writer.Write("1 2");
                writer.Flush();
                stream.Position = 0;
                await FileHelper.SaveAsync(fileInfo, stream).ConfigureAwait(false);
            }

            var text = File.ReadAllText(fileInfo.FullName);

            Assert.AreEqual("1 2", text);
            using (var stream = PooledMemoryStream.Borrow())
            {
                using var writer = new StreamWriter(stream);
                writer.Write("3");
                writer.Flush();
                stream.Position = 0;
                await FileHelper.SaveAsync(fileInfo, stream).ConfigureAwait(false);
            }

            text = File.ReadAllText(fileInfo.FullName);
            Assert.AreEqual("3", text);
        }
Ejemplo n.º 2
0
#pragma warning restore SA1401 // Fields should be private

            public override bool TryUpdate(Stream stream, out Stream updated)
            {
                this.WasCalled = true;
                updated        = PooledMemoryStream.Borrow();
                stream.CopyTo(updated);
                updated.Position = 0;
                return(true);
            }
Ejemplo n.º 3
0
        /// <summary>
        /// Serialize <paramref name="item"/> to a <see cref="MemoryStream"/>
        /// </summary>
        internal static PooledMemoryStream ToStream <T>(T item)
        {
            var formatter = new BinaryFormatter();
            var ms        = PooledMemoryStream.Borrow();

            formatter.Serialize(ms, item);
            ms.Flush();
            ms.Position = 0;
            return(ms);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Serialize <paramref name="item"/> to a <see cref="PooledMemoryStream"/>.
        /// </summary>
        /// <typeparam name="T">The type of <paramref name="item"/>.</typeparam>
        /// <param name="item">The instance to serialize.</param>
        /// <returns>The <see cref="PooledMemoryStream"/>.</returns>
        internal static PooledMemoryStream ToStream <T>(T item)
        {
            var ms         = PooledMemoryStream.Borrow();
            var serializer = Serializers.GetOrAdd(item?.GetType() ?? typeof(T), x => new DataContractSerializer(x));

            lock (serializer)
            {
                serializer.WriteObject(ms, item);
            }

            ms.Flush();
            ms.Position = 0;
            return(ms);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Serialize <paramref name="item"/> to a <see cref="MemoryStream"/>
        /// </summary>
        internal static PooledMemoryStream ToStream <T>(T item)
        {
            var ms         = PooledMemoryStream.Borrow();
            var serializer = Serializers.GetOrAdd(item.GetType(), x => new XmlSerializer(item.GetType()));

            lock (serializer)
            {
                serializer.Serialize(ms, item);
            }

            ms.Flush();
            ms.Position = 0;
            return(ms);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Serialize <paramref name="item"/> to a <see cref="MemoryStream"/>
        /// </summary>
        internal static PooledMemoryStream ToStream <T>(T item, JsonSerializerSettings settings)
        {
            var stream     = PooledMemoryStream.Borrow();
            var serializer = settings != null
                ? JsonSerializer.Create(settings)
                : JsonSerializer.Create();

            using (var writer = new JsonTextWriter(new StreamWriter(stream, DefaultEncoding, bufferSize: 1024, leaveOpen: true)))
            {
                serializer.Serialize(writer, item);
            }

            stream.Position = 0;
            return(stream);
        }
Ejemplo n.º 7
0
        public void SerializationRoundtrip()
        {
            var exception       = new RestoreException(new Exception("Save failed"), new Exception("Restore failed"));
            var binaryFormatter = new BinaryFormatter();

            using (var steram = PooledMemoryStream.Borrow())
            {
                binaryFormatter.Serialize(steram, exception);
                steram.Position = 0;
                var roundtripped = (RestoreException)binaryFormatter.Deserialize(steram);
                Assert.AreEqual("Save failed", roundtripped.SaveException.Message);
                Assert.NotNull(roundtripped.InnerException);
                Assert.AreEqual("Restore failed", roundtripped.InnerException.Message);
            }
        }
Ejemplo n.º 8
0
        public void SerializationRoundtrip()
        {
            var exception       = new RestoreException(new Exception("Save failed"), new Exception("Restore failed"));
            var binaryFormatter = new BinaryFormatter();

            using var stream = PooledMemoryStream.Borrow();
            binaryFormatter.Serialize(stream, exception);
            stream.Position = 0;
#pragma warning disable CA2300, CA2301 // Do not use insecure deserializer BinaryFormatter
            var roundtripped = (RestoreException)binaryFormatter.Deserialize(stream);
#pragma warning restore CA2300, CA2301 // Do not use insecure deserializer BinaryFormatter
            Assert.AreEqual("Save failed", roundtripped.SaveException.Message);
            Assert.NotNull(roundtripped.InnerException);
            Assert.AreEqual("Restore failed", roundtripped.InnerException?.Message);
        }
Ejemplo n.º 9
0
        public void RoundtripRepositorySettingsWithNullBackupSettings()
        {
            var settings = new RepositorySettings(
                directory: Directory.FullName,
                isTrackingDirty: true,
                backupSettings: null,
                extension: ".abc",
                tempExtension: ".def");
            var serializer = new BinaryFormatter();

            using Stream stream = PooledMemoryStream.Borrow();
            serializer.Serialize(stream, settings);
            stream.Position = 0;
            var roundtripped = (RepositorySettings)serializer.Deserialize(stream);

            AssertProperties(settings, roundtripped);
        }
        public void RoundtripRepositorySettingsWithNullBackupSettings()
        {
            var settings = new RepositorySettings(
                directory: Directory.FullName,
                isTrackingDirty: true,
                backupSettings: null,
                extension: ".cde",
                tempExtension: ".fgh");
            var serializer = new DataContractSerializer(settings.GetType());

            using Stream stream = PooledMemoryStream.Borrow();
            serializer.WriteObject(stream, settings);
            stream.Position = 0;
            var roundtripped = (RepositorySettings)serializer.ReadObject(stream);

            AssertProperties(settings, roundtripped);
        }
Ejemplo n.º 11
0
        public void RoundtripRepositorySettings()
        {
            var settings = new DataRepositorySettings(
                directory: Directory.FullName,
                isTrackingDirty: true,
                saveNullDeletesFile: true,
                backupSettings: BackupSettings,
                extension: ".cde",
                tempExtension: ".fgh");
            var serializer = new BinaryFormatter();

            using Stream stream = PooledMemoryStream.Borrow();
            serializer.Serialize(stream, settings);
            stream.Position = 0;
            var roundtripped = (DataRepositorySettings)serializer.Deserialize(stream);

            AssertProperties(settings, roundtripped);
        }
Ejemplo n.º 12
0
        /// <inheritdoc/>
        public override bool TryUpdate(Stream stream, [NotNullWhen(true)] out Stream?updated)
        {
            var jObject = Load();
            var hash    = JsonEqualsComparer <JObject> .Default.GetHashCode(jObject);

            var updatedJObject = jObject;

            foreach (var step in this.steps)
            {
                updatedJObject = step(updatedJObject);
            }

            if (ReferenceEquals(jObject, updatedJObject) &&
                hash == JsonEqualsComparer <JObject> .Default.GetHashCode(updatedJObject))
            {
                updated = null;
                return(false);
            }

            updated = PooledMemoryStream.Borrow();
            using (var streamWriter = new StreamWriter(updated, Encoding.UTF8, 1024, leaveOpen: true))
            {
                using var jsonWriter = new JsonTextWriter(streamWriter);
                jObject.WriteTo(jsonWriter);
            }

            updated.Position = 0;
            return(true);

            JObject Load()
            {
                using var streamReader = new StreamReader(stream, Encoding.UTF8, detectEncodingFromByteOrderMarks: true, bufferSize: 1024, leaveOpen: true);
                using var reader       = new JsonTextReader(streamReader);
                return(JObject.Load(reader));
            }
        }