StreamWriter.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. public bool Throttle { get; set; }
  35. public long ThrottleLimit { get; set; }
  36. /// <summary>
  37. /// Initializes a new instance of the <see cref="StreamWriter" /> class.
  38. /// </summary>
  39. /// <param name="source">The source.</param>
  40. /// <param name="contentType">Type of the content.</param>
  41. /// <param name="logger">The logger.</param>
  42. public StreamWriter(Stream source, string contentType, ILogger logger)
  43. {
  44. if (string.IsNullOrEmpty(contentType))
  45. {
  46. throw new ArgumentNullException("contentType");
  47. }
  48. SourceStream = source;
  49. Logger = logger;
  50. Options["Content-Type"] = contentType;
  51. if (source.CanSeek)
  52. {
  53. Options["Content-Length"] = source.Length.ToString(UsCulture);
  54. }
  55. }
  56. /// <summary>
  57. /// Initializes a new instance of the <see cref="StreamWriter"/> class.
  58. /// </summary>
  59. /// <param name="source">The source.</param>
  60. /// <param name="contentType">Type of the content.</param>
  61. /// <param name="logger">The logger.</param>
  62. public StreamWriter(byte[] source, string contentType, ILogger logger)
  63. : this(new MemoryStream(source), contentType, logger)
  64. {
  65. }
  66. /// <summary>
  67. /// Writes to.
  68. /// </summary>
  69. /// <param name="responseStream">The response stream.</param>
  70. public void WriteTo(Stream responseStream)
  71. {
  72. if (Throttle)
  73. {
  74. responseStream = new ThrottledStream(responseStream, ThrottleLimit)
  75. {
  76. MinThrottlePosition = ThrottleLimit * 180
  77. };
  78. }
  79. var task = WriteToAsync(responseStream);
  80. Task.WaitAll(task);
  81. }
  82. /// <summary>
  83. /// Writes to async.
  84. /// </summary>
  85. /// <param name="responseStream">The response stream.</param>
  86. /// <returns>Task.</returns>
  87. private async Task WriteToAsync(Stream responseStream)
  88. {
  89. try
  90. {
  91. using (var src = SourceStream)
  92. {
  93. await src.CopyToAsync(responseStream).ConfigureAwait(false);
  94. }
  95. }
  96. catch (Exception ex)
  97. {
  98. Logger.ErrorException("Error streaming data", ex);
  99. throw;
  100. }
  101. }
  102. }
  103. }