StreamWriter.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using MediaBrowser.Model.Services;
  8. using Microsoft.Extensions.Logging;
  9. using Microsoft.Net.Http.Headers;
  10. namespace Emby.Server.Implementations.HttpServer
  11. {
  12. /// <summary>
  13. /// Class StreamWriter
  14. /// </summary>
  15. public class StreamWriter : IAsyncStreamWriter, IHasHeaders
  16. {
  17. private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
  18. /// <summary>
  19. /// Gets or sets the source stream.
  20. /// </summary>
  21. /// <value>The source stream.</value>
  22. private Stream SourceStream { get; set; }
  23. private byte[] SourceBytes { get; set; }
  24. /// <summary>
  25. /// The _options
  26. /// </summary>
  27. private readonly IDictionary<string, string> _options = new Dictionary<string, string>();
  28. /// <summary>
  29. /// Gets the options.
  30. /// </summary>
  31. /// <value>The options.</value>
  32. public IDictionary<string, string> Headers => _options;
  33. public Action OnComplete { get; set; }
  34. public Action OnError { get; set; }
  35. /// <summary>
  36. /// Initializes a new instance of the <see cref="StreamWriter" /> class.
  37. /// </summary>
  38. /// <param name="source">The source.</param>
  39. /// <param name="contentType">Type of the content.</param>
  40. /// <param name="logger">The logger.</param>
  41. public StreamWriter(Stream source, string contentType)
  42. {
  43. if (string.IsNullOrEmpty(contentType))
  44. {
  45. throw new ArgumentNullException(nameof(contentType));
  46. }
  47. SourceStream = source;
  48. Headers[HeaderNames.ContentType] = contentType;
  49. }
  50. /// <summary>
  51. /// Initializes a new instance of the <see cref="StreamWriter"/> class.
  52. /// </summary>
  53. /// <param name="source">The source.</param>
  54. /// <param name="contentType">Type of the content.</param>
  55. public StreamWriter(byte[] source, string contentType)
  56. {
  57. if (string.IsNullOrEmpty(contentType))
  58. {
  59. throw new ArgumentNullException(nameof(contentType));
  60. }
  61. SourceBytes = source;
  62. Headers[HeaderNames.ContentType] = contentType;
  63. }
  64. public async Task WriteToAsync(Stream responseStream, CancellationToken cancellationToken)
  65. {
  66. try
  67. {
  68. var bytes = SourceBytes;
  69. if (bytes != null)
  70. {
  71. await responseStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
  72. }
  73. else
  74. {
  75. using (var src = SourceStream)
  76. {
  77. await src.CopyToAsync(responseStream).ConfigureAwait(false);
  78. }
  79. }
  80. }
  81. catch
  82. {
  83. if (OnError != null)
  84. {
  85. OnError();
  86. }
  87. throw;
  88. }
  89. finally
  90. {
  91. if (OnComplete != null)
  92. {
  93. OnComplete();
  94. }
  95. }
  96. }
  97. }
  98. }