< Summary

Line coverage
20%
Covered lines: 7
Uncovered lines: 27
Coverable lines: 34
Total lines: 137
Line coverage: 20.5%
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 Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
Create(...)0%4260%
Create(...)0%7280%
Create(...)50%12.17644.44%
IsDisc(...)100%210%
IsDisc(...)100%210%

File(s)

https://raw.githubusercontent.com/JeremyAnsel/JeremyAnsel.IO.Locator/90359a183919dae2d6e9fc4cabae797b0df998d4/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        static DiscFileLocatorFactory()
 23        {
 324            DiscUtils.Setup.SetupHelper.RegisterAssembly(typeof(DiscUtils.Iso9660.BuildFileInfo).Assembly);
 325            DiscUtils.Setup.SetupHelper.RegisterAssembly(typeof(DiscUtils.Udf.UdfReader).Assembly);
 326        }
 27
 28        /// <summary>
 29        /// Creates a disc file locator from a file.
 30        /// </summary>
 31        /// <param name="root">The root path.</param>
 32        /// <returns>A file locator.</returns>
 33        public static IFileLocator Create(string? root)
 34        {
 035            if (string.IsNullOrEmpty(root))
 36            {
 037                throw new ArgumentNullException(nameof(root));
 38            }
 39
 040            if (DiscFileLocatorFactory.IsDisc(root!, IsoFileLocator.IsIsoFile))
 41            {
 042                return new IsoFileLocator(root!);
 43            }
 44
 045            if (DiscFileLocatorFactory.IsDisc(root!, UdfFileLocator.IsUdfFile))
 46            {
 047                return new UdfFileLocator(root!);
 48            }
 49
 050            throw new NotSupportedException();
 51        }
 52
 53        /// <summary>
 54        /// Creates a disc file locator from a locator.
 55        /// </summary>
 56        /// <param name="locator">The locator.</param>
 57        /// <param name="root">The root path.</param>
 58        /// <returns>A file locator.</returns>
 59        public static IFileLocator Create(IFileLocator? locator, string? root)
 60        {
 061            if (locator == null)
 62            {
 063                throw new ArgumentNullException(nameof(locator));
 64            }
 65
 066            if (string.IsNullOrEmpty(root))
 67            {
 068                throw new ArgumentNullException(nameof(root));
 69            }
 70
 071            if (DiscFileLocatorFactory.IsDisc(locator, root!, IsoFileLocator.IsIsoFile))
 72            {
 073                return new IsoFileLocator(locator, root!);
 74            }
 75
 076            if (DiscFileLocatorFactory.IsDisc(locator, root!, UdfFileLocator.IsUdfFile))
 77            {
 078                return new UdfFileLocator(locator, root!);
 79            }
 80
 081            throw new NotSupportedException();
 82        }
 83
 84        /// <summary>
 85        /// Creates a disc file locator from a stream.
 86        /// </summary>
 87        /// <param name="root">The stream.</param>
 88        /// <returns>A file locator.</returns>
 89        public static IFileLocator Create(Stream? root)
 90        {
 391            if (root == null)
 92            {
 093                throw new ArgumentNullException(nameof(root));
 94            }
 95
 396            if (IsoFileLocator.IsIsoFile(root))
 97            {
 098                root.Seek(0, SeekOrigin.Begin);
 099                return new IsoFileLocator(root);
 100            }
 101
 3102            if (UdfFileLocator.IsUdfFile(root))
 103            {
 0104                root.Seek(0, SeekOrigin.Begin);
 0105                return new UdfFileLocator(root);
 106            }
 107
 3108            throw new NotSupportedException();
 109        }
 110
 111        /// <summary>
 112        /// Detects if the specified root is a disc.
 113        /// </summary>
 114        /// <param name="root">The root.</param>
 115        /// <param name="detect">The detection function.</param>
 116        /// <returns>A boolean.</returns>
 117        private static bool IsDisc(string root, Func<Stream, bool> detect)
 118        {
 0119            using var file = File.OpenRead(root);
 0120            return detect(file);
 0121        }
 122
 123        /// <summary>
 124        /// Detects if the specified root from a locator is a disc.
 125        /// </summary>
 126        /// <param name="locator">The locator.</param>
 127        /// <param name="root">The root.</param>
 128        /// <param name="detect">The detection function.</param>
 129        /// <returns>A boolean.</returns>
 130        private static bool IsDisc(IFileLocator locator, string root, Func<Stream, bool> detect)
 131        {
 0132            using var file = locator.Open(root);
 0133            return detect(file);
 0134        }
 135    }
 136}
 137