< Summary

Line coverage
33%
Covered lines: 30
Uncovered lines: 60
Coverable lines: 90
Total lines: 389
Line coverage: 33.3%
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 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))
 188                {
 0189                    this.Write(file, stream);
 0190                }
 191            }
 0192        }
 193    }
 194}

https://raw.githubusercontent.com/JeremyAnsel/JeremyAnsel.IO.Locator/16c8f4344390c6376cd2208cc1ae63a39862df37/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 Dictionary<string, byte[]> database;
 23
 24        /// <summary>
 25        /// Initializes a new instance of the <see cref="MemoryFileLocator"/> class.
 26        /// </summary>
 427        public MemoryFileLocator()
 28        {
 429            this.database = new Dictionary<string, byte[]>();
 430        }
 31
 32        /// <summary>
 33        /// Gets a value indicating whether the file locator is empty.
 34        /// </summary>
 35        public bool IsEmpty
 36        {
 37            get
 38            {
 439                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        {
 448            GC.SuppressFinalize(this);
 449        }
 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        {
 276            path = Utilities.PathNormalize(path);
 77
 278            var buffer = this.database[path];
 279            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        {
 898            foreach (var file in this.database.Keys)
 99            {
 2100                if (Utilities.PathStartsWith(file, root))
 101                {
 2102                    yield return file;
 103                }
 104            }
 2105        }
 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        {
 2139            if (string.IsNullOrEmpty(path))
 140            {
 0141                throw new ArgumentNullException(nameof(path));
 142            }
 143
 2144            if (data == null)
 145            {
 0146                throw new ArgumentNullException(nameof(data));
 147            }
 148
 2149            path = Utilities.PathNormalize(path);
 150
 2151            if (this.database.ContainsKey(path))
 152            {
 0153                throw new ArgumentOutOfRangeException(nameof(path));
 154            }
 155
 2156            using (var buffer = new MemoryStream())
 157            {
 2158                data.CopyTo(buffer);
 159
 2160                this.database.Add(path, buffer.ToArray());
 2161            }
 2162        }
 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        {
 2170            this.WriteAll(locator, string.Empty);
 2171        }
 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        {
 2180            if (locator == null)
 181            {
 0182                throw new ArgumentNullException(nameof(locator));
 183            }
 184
 8185            foreach (var file in locator.EnumerateFiles(root))
 186            {
 2187                using (var stream = locator.Open(file))
 188                {
 2189                    this.Write(file, stream);
 2190                }
 191            }
 2192        }
 193    }
 194}
 195