AsyncFile.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334
  1. using System;
  2. using System.IO;
  3. namespace MediaBrowser.Model.IO
  4. {
  5. /// <summary>
  6. /// Helper class to create async <see cref="FileStream" />s.
  7. /// </summary>
  8. public static class AsyncFile
  9. {
  10. /// <summary>
  11. /// Gets a value indicating whether we should use async IO on this platform.
  12. /// <see href="https://github.com/dotnet/runtime/issues/16354" />.
  13. /// </summary>
  14. /// <returns>Returns <c>false</c> on Windows; otherwise <c>true</c>.</returns>
  15. public static bool UseAsyncIO => !OperatingSystem.IsWindows();
  16. /// <summary>
  17. /// Opens an existing file for reading.
  18. /// </summary>
  19. /// <param name="path">The file to be opened for reading.</param>
  20. /// <returns>A read-only <see cref="FileStream" /> on the specified path.</returns>
  21. public static FileStream OpenRead(string path)
  22. => new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, IODefaults.FileStreamBufferSize, UseAsyncIO);
  23. /// <summary>
  24. /// Opens an existing file for writing.
  25. /// </summary>
  26. /// <param name="path">The file to be opened for writing.</param>
  27. /// <returns>An unshared <see cref="FileStream" /> object on the specified path with Write access.</returns>
  28. public static FileStream OpenWrite(string path)
  29. => new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None, IODefaults.FileStreamBufferSize, UseAsyncIO);
  30. }
  31. }