< Summary

Line coverage
12%
Covered lines: 4
Uncovered lines: 27
Coverable lines: 31
Total lines: 135
Line coverage: 12.9%
Branch coverage
15%
Covered branches: 3
Total branches: 20
Branch coverage: 15%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
Create(...)0%60%
Create(...)0%80%
Create(...)50%644.44%
IsDisc(...)100%10%
IsDisc(...)100%10%

File(s)

https://raw.githubusercontent.com/JeremyAnsel/JeremyAnsel.IO.Locator/16c8f4344390c6376cd2208cc1ae63a39862df37/JeremyAnsel.IO.Locator/JeremyAnsel.IO.DiscLocator/DiscFileLocatorFactory.cs

#LineLine coverage
 1// <copyright file="DiscFileLocatorFactory.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.DiscLocator
 9{
 10    using System;
 11    using System.IO;
 12    using IO.Locator;
 13
 14    /// <summary>
 15    /// A factory to create a disc file locator.
 16    /// </summary>
 17    /// <remarks>
 18    /// The supported formats are: Iso, Udf.
 19    /// </remarks>
 20    public static class DiscFileLocatorFactory
 21    {
 22        /// <summary>
 23        /// Creates a disc file locator from a file.
 24        /// </summary>
 25        /// <param name="root">The root path.</param>
 26        /// <returns>A file locator.</returns>
 27        public static IFileLocator Create(string root)
 28        {
 029            if (string.IsNullOrEmpty(root))
 30            {
 031                throw new ArgumentNullException(nameof(root));
 32            }
 33
 034            if (DiscFileLocatorFactory.IsDisc(root, IsoFileLocator.IsIsoFile))
 35            {
 036                return new IsoFileLocator(root);
 37            }
 38
 039            if (DiscFileLocatorFactory.IsDisc(root, UdfFileLocator.IsUdfFile))
 40            {
 041                return new UdfFileLocator(root);
 42            }
 43
 044            throw new NotSupportedException();
 45        }
 46
 47        /// <summary>
 48        /// Creates a disc file locator from a locator.
 49        /// </summary>
 50        /// <param name="locator">The locator.</param>
 51        /// <param name="root">The root path.</param>
 52        /// <returns>A file locator.</returns>
 53        public static IFileLocator Create(IFileLocator locator, string root)
 54        {
 055            if (locator == null)
 56            {
 057                throw new ArgumentNullException(nameof(locator));
 58            }
 59
 060            if (string.IsNullOrEmpty(root))
 61            {
 062                throw new ArgumentNullException(nameof(root));
 63            }
 64
 065            if (DiscFileLocatorFactory.IsDisc(locator, root, IsoFileLocator.IsIsoFile))
 66            {
 067                return new IsoFileLocator(locator, root);
 68            }
 69
 070            if (DiscFileLocatorFactory.IsDisc(locator, root, UdfFileLocator.IsUdfFile))
 71            {
 072                return new UdfFileLocator(locator, root);
 73            }
 74
 075            throw new NotSupportedException();
 76        }
 77
 78        /// <summary>
 79        /// Creates a disc file locator from a stream.
 80        /// </summary>
 81        /// <param name="root">The stream.</param>
 82        /// <returns>A file locator.</returns>
 83        public static IFileLocator Create(Stream root)
 84        {
 285            if (root == null)
 86            {
 087                throw new ArgumentNullException(nameof(root));
 88            }
 89
 290            if (IsoFileLocator.IsIsoFile(root))
 91            {
 092                root.Seek(0, SeekOrigin.Begin);
 093                return new IsoFileLocator(root);
 94            }
 95
 296            if (UdfFileLocator.IsUdfFile(root))
 97            {
 098                root.Seek(0, SeekOrigin.Begin);
 099                return new UdfFileLocator(root);
 100            }
 101
 2102            throw new NotSupportedException();
 103        }
 104
 105        /// <summary>
 106        /// Detects if the specified root is a disc.
 107        /// </summary>
 108        /// <param name="root">The root.</param>
 109        /// <param name="detect">The detection function.</param>
 110        /// <returns>A boolean.</returns>
 111        private static bool IsDisc(string root, Func<Stream, bool> detect)
 112        {
 0113            using (var file = File.OpenRead(root))
 114            {
 0115                return detect(file);
 116            }
 0117        }
 118
 119        /// <summary>
 120        /// Detects if the specified root from a locator is a disc.
 121        /// </summary>
 122        /// <param name="locator">The locator.</param>
 123        /// <param name="root">The root.</param>
 124        /// <param name="detect">The detection function.</param>
 125        /// <returns>A boolean.</returns>
 126        private static bool IsDisc(IFileLocator locator, string root, Func<Stream, bool> detect)
 127        {
 0128            using (var file = locator.Open(root))
 129            {
 0130                return detect(file);
 131            }
 0132        }
 133    }
 134}
 135