StreamWriter.cs 3.6 KB

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