StreamWriter.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. 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. private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
  16. /// <summary>
  17. /// Gets or sets the source stream.
  18. /// </summary>
  19. /// <value>The source stream.</value>
  20. private Stream SourceStream { get; set; }
  21. /// <summary>
  22. /// The _options
  23. /// </summary>
  24. private readonly IDictionary<string, string> _options = new Dictionary<string, string>();
  25. /// <summary>
  26. /// Gets the options.
  27. /// </summary>
  28. /// <value>The options.</value>
  29. public IDictionary<string, string> Options
  30. {
  31. get { return _options; }
  32. }
  33. public bool Throttle { get; set; }
  34. public long ThrottleLimit { get; set; }
  35. public long MinThrottlePosition;
  36. public Func<long, long, long> ThrottleCallback { get; set; }
  37. public Action OnComplete { get; set; }
  38. /// <summary>
  39. /// Initializes a new instance of the <see cref="StreamWriter" /> class.
  40. /// </summary>
  41. /// <param name="source">The source.</param>
  42. /// <param name="contentType">Type of the content.</param>
  43. /// <param name="logger">The logger.</param>
  44. public StreamWriter(Stream source, string contentType, ILogger logger)
  45. {
  46. if (string.IsNullOrEmpty(contentType))
  47. {
  48. throw new ArgumentNullException("contentType");
  49. }
  50. SourceStream = source;
  51. Logger = logger;
  52. Options["Content-Type"] = contentType;
  53. if (source.CanSeek)
  54. {
  55. Options["Content-Length"] = source.Length.ToString(UsCulture);
  56. }
  57. }
  58. /// <summary>
  59. /// Initializes a new instance of the <see cref="StreamWriter"/> class.
  60. /// </summary>
  61. /// <param name="source">The source.</param>
  62. /// <param name="contentType">Type of the content.</param>
  63. /// <param name="logger">The logger.</param>
  64. public StreamWriter(byte[] source, string contentType, ILogger logger)
  65. : this(new MemoryStream(source), contentType, logger)
  66. {
  67. }
  68. /// <summary>
  69. /// Writes to.
  70. /// </summary>
  71. /// <param name="responseStream">The response stream.</param>
  72. public void WriteTo(Stream responseStream)
  73. {
  74. if (Throttle)
  75. {
  76. responseStream = new ThrottledStream(responseStream, ThrottleLimit)
  77. {
  78. MinThrottlePosition = MinThrottlePosition,
  79. ThrottleCallback = ThrottleCallback
  80. };
  81. }
  82. WriteToInternal(responseStream);
  83. }
  84. /// <summary>
  85. /// Writes to async.
  86. /// </summary>
  87. /// <param name="responseStream">The response stream.</param>
  88. /// <returns>Task.</returns>
  89. private void WriteToInternal(Stream responseStream)
  90. {
  91. try
  92. {
  93. using (var src = SourceStream)
  94. {
  95. src.CopyTo(responseStream);
  96. }
  97. }
  98. catch (Exception ex)
  99. {
  100. Logger.ErrorException("Error streaming data", ex);
  101. throw;
  102. }
  103. finally
  104. {
  105. if (OnComplete != null)
  106. {
  107. OnComplete();
  108. }
  109. }
  110. }
  111. }
  112. }