StreamWriter.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using MediaBrowser.Model.Logging;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using MediaBrowser.Common.IO;
  9. using MediaBrowser.Model.Services;
  10. namespace MediaBrowser.Server.Implementations.HttpServer
  11. {
  12. /// <summary>
  13. /// Class StreamWriter
  14. /// </summary>
  15. public class StreamWriter : IAsyncStreamWriter, IHasHeaders
  16. {
  17. private ILogger Logger { get; set; }
  18. private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
  19. /// <summary>
  20. /// Gets or sets the source stream.
  21. /// </summary>
  22. /// <value>The source stream.</value>
  23. private Stream SourceStream { 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
  33. {
  34. get { return _options; }
  35. }
  36. public Action OnComplete { get; set; }
  37. public Action OnError { get; set; }
  38. private readonly byte[] _bytes;
  39. /// <summary>
  40. /// Initializes a new instance of the <see cref="StreamWriter" /> class.
  41. /// </summary>
  42. /// <param name="source">The source.</param>
  43. /// <param name="contentType">Type of the content.</param>
  44. /// <param name="logger">The logger.</param>
  45. public StreamWriter(Stream source, string contentType, ILogger logger)
  46. {
  47. if (string.IsNullOrEmpty(contentType))
  48. {
  49. throw new ArgumentNullException("contentType");
  50. }
  51. SourceStream = source;
  52. Logger = logger;
  53. Headers["Content-Type"] = contentType;
  54. if (source.CanSeek)
  55. {
  56. Headers["Content-Length"] = source.Length.ToString(UsCulture);
  57. }
  58. }
  59. /// <summary>
  60. /// Initializes a new instance of the <see cref="StreamWriter"/> class.
  61. /// </summary>
  62. /// <param name="source">The source.</param>
  63. /// <param name="contentType">Type of the content.</param>
  64. /// <param name="logger">The logger.</param>
  65. public StreamWriter(byte[] source, string contentType, ILogger logger)
  66. : this(new MemoryStream(source), contentType, logger)
  67. {
  68. if (string.IsNullOrEmpty(contentType))
  69. {
  70. throw new ArgumentNullException("contentType");
  71. }
  72. _bytes = source;
  73. Logger = logger;
  74. Headers["Content-Type"] = contentType;
  75. Headers["Content-Length"] = source.Length.ToString(UsCulture);
  76. }
  77. public async Task WriteToAsync(Stream responseStream, CancellationToken cancellationToken)
  78. {
  79. try
  80. {
  81. if (_bytes != null)
  82. {
  83. await responseStream.WriteAsync(_bytes, 0, _bytes.Length);
  84. }
  85. else
  86. {
  87. using (var src = SourceStream)
  88. {
  89. await src.CopyToAsync(responseStream).ConfigureAwait(false);
  90. }
  91. }
  92. }
  93. catch (Exception ex)
  94. {
  95. Logger.ErrorException("Error streaming data", ex);
  96. if (OnError != null)
  97. {
  98. OnError();
  99. }
  100. throw;
  101. }
  102. finally
  103. {
  104. if (OnComplete != null)
  105. {
  106. OnComplete();
  107. }
  108. }
  109. }
  110. }
  111. }