< Summary

Line coverage
32%
Covered lines: 29
Uncovered lines: 59
Coverable lines: 88
Total lines: 385
Line coverage: 32.9%
Branch coverage
27%
Covered branches: 10
Total branches: 36
Branch coverage: 27.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

C:\projects\jeremyansel-io-locator\JeremyAnsel.IO.Locator\JeremyAnsel.IO.Locator\MemoryFileLocator.cs

#LineLine coverage
 1// <copyright file="MemoryFileLocator.cs" company="Jérémy Ansel">
 2// Copyright (c) 2015, 2019 Jérémy Ansel
 3// </copyright>
 4// <license>
 5// Licensed under the MIT license. See LICENSE.txt
 6// </license>
 7
 8namespace JeremyAnsel.IO.Locator
 9{
 10    using System;
 11    using System.Collections.Generic;
 12    using System.IO;
 13
 14    /// <summary>
 15    /// An in memory file locator.
 16    /// </summary>
 17    public sealed class MemoryFileLocator : IFileLocator, IWritableFileLocator
 18    {
 19        /// <summary>
 20        /// The database.
 21        /// </summary>
 22        private readonly Dictionary<string, byte[]> database;
 23
 24        /// <summary>
 25        /// Initializes a new instance of the <see cref="MemoryFileLocator"/> class.
 26        /// </summary>
 027        public MemoryFileLocator()
 28        {
 029            this.database = new Dictionary<string, byte[]>();
 030        }
 31
 32        /// <summary>
 33        /// Gets a value indicating whether the file locator is empty.
 34        /// </summary>
 35        public bool IsEmpty
 36        {
 37            get
 38            {
 039                return this.database.Count == 0;
 40            }
 41        }
 42
 43        /// <summary>
 44        /// Immediately releases the unmanaged resources used by the <see cref="MemoryFileLocator"/> object.
 45        /// </summary>
 46        public void Dispose()
 47        {
 048            GC.SuppressFinalize(this);
 049        }
 50
 51        /// <summary>
 52        /// Clears the file locator.
 53        /// </summary>
 54        public void Clear()
 55        {
 056            this.database.Clear();
 057        }
 58
 59        /// <summary>
 60        /// Indicates whether the specified path exists.
 61        /// </summary>
 62        /// <param name="path">A path.</param>
 63        /// <returns>A boolean.</returns>
 64        public bool Exists(string path)
 65        {
 066            return this.database.ContainsKey(Utilities.PathNormalize(path));
 67        }
 68
 69        /// <summary>
 70        /// Open a file.
 71        /// </summary>
 72        /// <param name="path">A path.</param>
 73        /// <returns>A stream.</returns>
 74        public Stream Open(string path)
 75        {
 076            path = Utilities.PathNormalize(path);
 77
 078            var buffer = this.database[path];
 079            return new MemoryStream(buffer, false);
 80        }
 81
 82        /// <summary>
 83        /// Enumerate the files.
 84        /// </summary>
 85        /// <returns>An enumeration.</returns>
 86        public IEnumerable<string> EnumerateFiles()
 87        {
 088            return this.EnumerateFiles(string.Empty);
 89        }
 90
 91        /// <summary>
 92        /// Enumerate the files.
 93        /// </summary>
 94        /// <param name="root">The root path.</param>
 95        /// <returns>An enumeration.</returns>
 96        public IEnumerable<string> EnumerateFiles(string root)
 97        {
 098            foreach (var file in this.database.Keys)
 99            {
 0100                if (Utilities.PathStartsWith(file, root))
 101                {
 0102                    yield return file;
 103                }
 104            }
 0105        }
 106
 107        /// <summary>
 108        /// Create a file.
 109        /// </summary>
 110        /// <param name="path">A path.</param>
 111        public void Create(string? path)
 112        {
 0113            if (string.IsNullOrEmpty(path))
 114            {
 0115                throw new ArgumentNullException(nameof(path));
 116            }
 117
 0118            path = Utilities.PathNormalize(path);
 119
 0120            if (this.database.ContainsKey(path))
 121            {
 0122                throw new ArgumentOutOfRangeException(nameof(path));
 123            }
 124
 125#if NET45
 126            this.database.Add(path, new byte[0]);
 127#else
 0128            this.database.Add(path, Array.Empty<byte>());
 129#endif
 0130        }
 131
 132        /// <summary>
 133        /// Write a file.
 134        /// </summary>
 135        /// <param name="path">A path.</param>
 136        /// <param name="data">The data.</param>
 137        public void Write(string path, Stream? data)
 138        {
 0139            if (string.IsNullOrEmpty(path))
 140            {
 0141                throw new ArgumentNullException(nameof(path));
 142            }
 143
 0144            if (data == null)
 145            {
 0146                throw new ArgumentNullException(nameof(data));
 147            }
 148
 0149            path = Utilities.PathNormalize(path);
 150
 0151            if (this.database.ContainsKey(path))
 152            {
 0153                throw new ArgumentOutOfRangeException(nameof(path));
 154            }
 155
 0156            using (var buffer = new MemoryStream())
 157            {
 0158                data.CopyTo(buffer);
 159
 0160                this.database.Add(path, buffer.ToArray());
 0161            }
 0162        }
 163
 164        /// <summary>
 165        /// Write the files from a file locator.
 166        /// </summary>
 167        /// <param name="locator">A file locator.</param>
 168        public void WriteAll(IFileLocator? locator)
 169        {
 0170            this.WriteAll(locator, string.Empty);
 0171        }
 172
 173        /// <summary>
 174        /// Write the files from a file locator.
 175        /// </summary>
 176        /// <param name="locator">A file locator.</param>
 177        /// <param name="root">The root path.</param>
 178        public void WriteAll(IFileLocator? locator, string root)
 179        {
 0180            if (locator == null)
 181            {
 0182                throw new ArgumentNullException(nameof(locator));
 183            }
 184
 0185            foreach (var file in locator.EnumerateFiles(root))
 186            {
 0187                using var stream = locator.Open(file);
 0188                this.Write(file, stream);
 189            }
 0190        }
 191    }
 192}

https://raw.githubusercontent.com/JeremyAnsel/JeremyAnsel.IO.Locator/90359a183919dae2d6e9fc4cabae797b0df998d4/JeremyAnsel.IO.Locator/JeremyAnsel.IO.Locator/MemoryFileLocator.cs

#LineLine coverage
 1// <copyright file="MemoryFileLocator.cs" company="Jérémy Ansel">
 2// Copyright (c) 2015, 2019 Jérémy Ansel
 3// </copyright>
 4// <license>
 5// Licensed under the MIT license. See LICENSE.txt
 6// </license>
 7
 8namespace JeremyAnsel.IO.Locator
 9{
 10    using System;
 11    using System.Collections.Generic;
 12    using System.IO;
 13
 14    /// <summary>
 15    /// An in memory file locator.
 16    /// </summary>
 17    public sealed class MemoryFileLocator : IFileLocator, IWritableFileLocator
 18    {
 19        /// <summary>
 20        /// The database.
 21        /// </summary>
 22        private readonly Dictionary<string, byte[]> database;
 23
 24        /// <summary>
 25        /// Initializes a new instance of the <see cref="MemoryFileLocator"/> class.
 26        /// </summary>
 627        public MemoryFileLocator()
 28        {
 629            this.database = new Dictionary<string, byte[]>();
 630        }
 31
 32        /// <summary>
 33        /// Gets a value indicating whether the file locator is empty.
 34        /// </summary>
 35        public bool IsEmpty
 36        {
 37            get
 38            {
 639                return this.database.Count == 0;
 40            }
 41        }
 42
 43        /// <summary>
 44        /// Immediately releases the unmanaged resources used by the <see cref="MemoryFileLocator"/> object.
 45        /// </summary>
 46        public void Dispose()
 47        {
 648            GC.SuppressFinalize(this);
 649        }
 50
 51        /// <summary>
 52        /// Clears the file locator.
 53        /// </summary>
 54        public void Clear()
 55        {
 056            this.database.Clear();
 057        }
 58
 59        /// <summary>
 60        /// Indicates whether the specified path exists.
 61        /// </summary>
 62        /// <param name="path">A path.</param>
 63        /// <returns>A boolean.</returns>
 64        public bool Exists(string path)
 65        {
 066            return this.database.ContainsKey(Utilities.PathNormalize(path));
 67        }
 68
 69        /// <summary>
 70        /// Open a file.
 71        /// </summary>
 72        /// <param name="path">A path.</param>
 73        /// <returns>A stream.</returns>
 74        public Stream Open(string path)
 75        {
 376            path = Utilities.PathNormalize(path);
 77
 378            var buffer = this.database[path];
 379            return new MemoryStream(buffer, false);
 80        }
 81
 82        /// <summary>
 83        /// Enumerate the files.
 84        /// </summary>
 85        /// <returns>An enumeration.</returns>
 86        public IEnumerable<string> EnumerateFiles()
 87        {
 088            return this.EnumerateFiles(string.Empty);
 89        }
 90
 91        /// <summary>
 92        /// Enumerate the files.
 93        /// </summary>
 94        /// <param name="root">The root path.</param>
 95        /// <returns>An enumeration.</returns>
 96        public IEnumerable<string> EnumerateFiles(string root)
 97        {
 1298            foreach (var file in this.database.Keys)
 99            {
 3100                if (Utilities.PathStartsWith(file, root))
 101                {
 3102                    yield return file;
 103                }
 104            }
 3105        }
 106
 107        /// <summary>
 108        /// Create a file.
 109        /// </summary>
 110        /// <param name="path">A path.</param>
 111        public void Create(string? path)
 112        {
 0113            if (string.IsNullOrEmpty(path))
 114            {
 0115                throw new ArgumentNullException(nameof(path));
 116            }
 117
 0118            path = Utilities.PathNormalize(path);
 119
 0120            if (this.database.ContainsKey(path))
 121            {
 0122                throw new ArgumentOutOfRangeException(nameof(path));
 123            }
 124
 125#if NET45
 126            this.database.Add(path, new byte[0]);
 127#else
 0128            this.database.Add(path, Array.Empty<byte>());
 129#endif
 0130        }
 131
 132        /// <summary>
 133        /// Write a file.
 134        /// </summary>
 135        /// <param name="path">A path.</param>
 136        /// <param name="data">The data.</param>
 137        public void Write(string path, Stream? data)
 138        {
 3139            if (string.IsNullOrEmpty(path))
 140            {
 0141                throw new ArgumentNullException(nameof(path));
 142            }
 143
 3144            if (data == null)
 145            {
 0146                throw new ArgumentNullException(nameof(data));
 147            }
 148
 3149            path = Utilities.PathNormalize(path);
 150
 3151            if (this.database.ContainsKey(path))
 152            {
 0153                throw new ArgumentOutOfRangeException(nameof(path));
 154            }
 155
 3156            using (var buffer = new MemoryStream())
 157            {
 3158                data.CopyTo(buffer);
 159
 3160                this.database.Add(path, buffer.ToArray());
 3161            }
 3162        }
 163
 164        /// <summary>
 165        /// Write the files from a file locator.
 166        /// </summary>
 167        /// <param name="locator">A file locator.</param>
 168        public void WriteAll(IFileLocator? locator)
 169        {
 3170            this.WriteAll(locator, string.Empty);
 3171        }
 172
 173        /// <summary>
 174        /// Write the files from a file locator.
 175        /// </summary>
 176        /// <param name="locator">A file locator.</param>
 177        /// <param name="root">The root path.</param>
 178        public void WriteAll(IFileLocator? locator, string root)
 179        {
 3180            if (locator == null)
 181            {
 0182                throw new ArgumentNullException(nameof(locator));
 183            }
 184
 12185            foreach (var file in locator.EnumerateFiles(root))
 186            {
 3187                using var stream = locator.Open(file);
 3188                this.Write(file, stream);
 189            }
 3190        }
 191    }
 192}
 193