aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Model/IO/AsyncFile.cs
blob: b888a416395d613866404f96664238c203f880a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using System;
using System.IO;

namespace MediaBrowser.Model.IO
{
    /// <summary>
    /// Helper class to create async <see cref="FileStream" />s.
    /// </summary>
    public static class AsyncFile
    {
        /// <summary>
        /// Gets a value indicating whether we should use async IO on this platform.
        /// <see href="https://github.com/dotnet/runtime/issues/16354" />.
        /// </summary>
        /// <returns>Returns <c>false</c> on Windows; otherwise <c>true</c>.</returns>
        public static bool UseAsyncIO => !OperatingSystem.IsWindows();

        /// <summary>
        /// Opens an existing file for reading.
        /// </summary>
        /// <param name="path">The file to be opened for reading.</param>
        /// <returns>A read-only <see cref="FileStream" /> on the specified path.</returns>
        public static FileStream OpenRead(string path)
            => new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, IODefaults.FileStreamBufferSize, UseAsyncIO);

        /// <summary>
        /// Opens an existing file for writing.
        /// </summary>
        /// <param name="path">The file to be opened for writing.</param>
        /// <returns>An unshared <see cref="FileStream" /> object on the specified path with Write access.</returns>
        public static FileStream OpenWrite(string path)
            => new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None, IODefaults.FileStreamBufferSize, UseAsyncIO);
    }
}