StreamWriter.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using MediaBrowser.Model.Logging;
  2. using ServiceStack.Web;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Threading.Tasks;
  7. namespace MediaBrowser.Server.Implementations.HttpServer
  8. {
  9. /// <summary>
  10. /// Class StreamWriter
  11. /// </summary>
  12. public class StreamWriter : IStreamWriter, IHasOptions
  13. {
  14. private ILogger Logger { get; set; }
  15. /// <summary>
  16. /// Gets or sets the source stream.
  17. /// </summary>
  18. /// <value>The source stream.</value>
  19. private Stream SourceStream { get; set; }
  20. /// <summary>
  21. /// The _options
  22. /// </summary>
  23. private readonly IDictionary<string, string> _options = new Dictionary<string, string>();
  24. /// <summary>
  25. /// Gets the options.
  26. /// </summary>
  27. /// <value>The options.</value>
  28. public IDictionary<string, string> Options
  29. {
  30. get { return _options; }
  31. }
  32. /// <summary>
  33. /// Initializes a new instance of the <see cref="StreamWriter" /> class.
  34. /// </summary>
  35. /// <param name="source">The source.</param>
  36. /// <param name="contentType">Type of the content.</param>
  37. /// <param name="logger">The logger.</param>
  38. public StreamWriter(Stream source, string contentType, ILogger logger)
  39. {
  40. if (string.IsNullOrEmpty(contentType))
  41. {
  42. throw new ArgumentNullException("contentType");
  43. }
  44. SourceStream = source;
  45. Logger = logger;
  46. Options["Content-Type"] = contentType;
  47. }
  48. /// <summary>
  49. /// Initializes a new instance of the <see cref="StreamWriter"/> class.
  50. /// </summary>
  51. /// <param name="source">The source.</param>
  52. /// <param name="contentType">Type of the content.</param>
  53. /// <param name="logger">The logger.</param>
  54. public StreamWriter(byte[] source, string contentType, ILogger logger)
  55. : this(new MemoryStream(source), contentType, logger)
  56. {
  57. }
  58. /// <summary>
  59. /// Writes to.
  60. /// </summary>
  61. /// <param name="responseStream">The response stream.</param>
  62. public void WriteTo(Stream responseStream)
  63. {
  64. var task = WriteToAsync(responseStream);
  65. Task.WaitAll(task);
  66. }
  67. /// <summary>
  68. /// Writes to async.
  69. /// </summary>
  70. /// <param name="responseStream">The response stream.</param>
  71. /// <returns>Task.</returns>
  72. private async Task WriteToAsync(Stream responseStream)
  73. {
  74. try
  75. {
  76. using (var src = SourceStream)
  77. {
  78. await src.CopyToAsync(responseStream).ConfigureAwait(false);
  79. }
  80. }
  81. catch (Exception ex)
  82. {
  83. Logger.ErrorException("Error streaming media", ex);
  84. throw;
  85. }
  86. }
  87. }
  88. }