| | | 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 | | |
| | | 8 | | namespace 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> |
| | 0 | 27 | | public MemoryFileLocator() |
| | | 28 | | { |
| | 0 | 29 | | this.database = new Dictionary<string, byte[]>(); |
| | 0 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Gets a value indicating whether the file locator is empty. |
| | | 34 | | /// </summary> |
| | | 35 | | public bool IsEmpty |
| | | 36 | | { |
| | | 37 | | get |
| | | 38 | | { |
| | 0 | 39 | | 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 | | { |
| | 0 | 48 | | GC.SuppressFinalize(this); |
| | 0 | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Clears the file locator. |
| | | 53 | | /// </summary> |
| | | 54 | | public void Clear() |
| | | 55 | | { |
| | 0 | 56 | | this.database.Clear(); |
| | 0 | 57 | | } |
| | | 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 | | { |
| | 0 | 66 | | 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 | | { |
| | 0 | 76 | | path = Utilities.PathNormalize(path); |
| | | 77 | | |
| | 0 | 78 | | var buffer = this.database[path]; |
| | 0 | 79 | | 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 | | { |
| | 0 | 88 | | 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 | | { |
| | 0 | 98 | | foreach (var file in this.database.Keys) |
| | | 99 | | { |
| | 0 | 100 | | if (Utilities.PathStartsWith(file, root)) |
| | | 101 | | { |
| | 0 | 102 | | yield return file; |
| | | 103 | | } |
| | | 104 | | } |
| | 0 | 105 | | } |
| | | 106 | | |
| | | 107 | | /// <summary> |
| | | 108 | | /// Create a file. |
| | | 109 | | /// </summary> |
| | | 110 | | /// <param name="path">A path.</param> |
| | | 111 | | public void Create(string? path) |
| | | 112 | | { |
| | 0 | 113 | | if (string.IsNullOrEmpty(path)) |
| | | 114 | | { |
| | 0 | 115 | | throw new ArgumentNullException(nameof(path)); |
| | | 116 | | } |
| | | 117 | | |
| | 0 | 118 | | path = Utilities.PathNormalize(path); |
| | | 119 | | |
| | 0 | 120 | | if (this.database.ContainsKey(path)) |
| | | 121 | | { |
| | 0 | 122 | | throw new ArgumentOutOfRangeException(nameof(path)); |
| | | 123 | | } |
| | | 124 | | |
| | | 125 | | #if NET45 |
| | | 126 | | this.database.Add(path, new byte[0]); |
| | | 127 | | #else |
| | 0 | 128 | | this.database.Add(path, Array.Empty<byte>()); |
| | | 129 | | #endif |
| | 0 | 130 | | } |
| | | 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 | | { |
| | 0 | 139 | | if (string.IsNullOrEmpty(path)) |
| | | 140 | | { |
| | 0 | 141 | | throw new ArgumentNullException(nameof(path)); |
| | | 142 | | } |
| | | 143 | | |
| | 0 | 144 | | if (data == null) |
| | | 145 | | { |
| | 0 | 146 | | throw new ArgumentNullException(nameof(data)); |
| | | 147 | | } |
| | | 148 | | |
| | 0 | 149 | | path = Utilities.PathNormalize(path); |
| | | 150 | | |
| | 0 | 151 | | if (this.database.ContainsKey(path)) |
| | | 152 | | { |
| | 0 | 153 | | throw new ArgumentOutOfRangeException(nameof(path)); |
| | | 154 | | } |
| | | 155 | | |
| | 0 | 156 | | using (var buffer = new MemoryStream()) |
| | | 157 | | { |
| | 0 | 158 | | data.CopyTo(buffer); |
| | | 159 | | |
| | 0 | 160 | | this.database.Add(path, buffer.ToArray()); |
| | 0 | 161 | | } |
| | 0 | 162 | | } |
| | | 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 | | { |
| | 0 | 170 | | this.WriteAll(locator, string.Empty); |
| | 0 | 171 | | } |
| | | 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 | | { |
| | 0 | 180 | | if (locator == null) |
| | | 181 | | { |
| | 0 | 182 | | throw new ArgumentNullException(nameof(locator)); |
| | | 183 | | } |
| | | 184 | | |
| | 0 | 185 | | foreach (var file in locator.EnumerateFiles(root)) |
| | | 186 | | { |
| | 0 | 187 | | using var stream = locator.Open(file); |
| | 0 | 188 | | this.Write(file, stream); |
| | | 189 | | } |
| | 0 | 190 | | } |
| | | 191 | | } |
| | | 192 | | } |