StreamWriter.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. public long MinThrottlePosition;
  37. public Func<long, long, long> ThrottleCallback { get; set; }
  38. public Action OnComplete { get; set; }
  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. Options["Content-Type"] = contentType;
  54. if (source.CanSeek)
  55. {
  56. Options["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. }
  69. /// <summary>
  70. /// Writes to.
  71. /// </summary>
  72. /// <param name="responseStream">The response stream.</param>
  73. public void WriteTo(Stream responseStream)
  74. {
  75. if (Throttle)
  76. {
  77. responseStream = new ThrottledStream(responseStream, ThrottleLimit)
  78. {
  79. MinThrottlePosition = MinThrottlePosition,
  80. ThrottleCallback = ThrottleCallback
  81. };
  82. }
  83. WriteToAsync(responseStream);
  84. }
  85. /// <summary>
  86. /// Writes to async.
  87. /// </summary>
  88. /// <param name="responseStream">The response stream.</param>
  89. /// <returns>Task.</returns>
  90. private void WriteToAsync(Stream responseStream)
  91. {
  92. try
  93. {
  94. using (var src = SourceStream)
  95. {
  96. src.CopyTo(responseStream);
  97. }
  98. }
  99. catch (Exception ex)
  100. {
  101. Logger.ErrorException("Error streaming data", ex);
  102. throw;
  103. }
  104. finally
  105. {
  106. if (OnComplete != null)
  107. {
  108. OnComplete();
  109. }
  110. }
  111. }
  112. }
  113. }