2
0

StreamWriter.cs 3.2 KB

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