StreamWriter.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. using ServiceStack;
  9. namespace MediaBrowser.Server.Implementations.HttpServer
  10. {
  11. /// <summary>
  12. /// Class StreamWriter
  13. /// </summary>
  14. public class StreamWriter : IStreamWriter, IAsyncStreamWriter, IHasOptions
  15. {
  16. private ILogger Logger { get; set; }
  17. private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
  18. /// <summary>
  19. /// Gets or sets the source stream.
  20. /// </summary>
  21. /// <value>The source stream.</value>
  22. private Stream SourceStream { get; set; }
  23. /// <summary>
  24. /// The _options
  25. /// </summary>
  26. private readonly IDictionary<string, string> _options = new Dictionary<string, string>();
  27. /// <summary>
  28. /// Gets the options.
  29. /// </summary>
  30. /// <value>The options.</value>
  31. public IDictionary<string, string> Options
  32. {
  33. get { return _options; }
  34. }
  35. public Action OnComplete { get; set; }
  36. public Action OnError { get; set; }
  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. // 256k
  68. private const int BufferSize = 262144;
  69. /// <summary>
  70. /// Writes to.
  71. /// </summary>
  72. /// <param name="responseStream">The response stream.</param>
  73. public void WriteTo(Stream responseStream)
  74. {
  75. try
  76. {
  77. using (var src = SourceStream)
  78. {
  79. src.CopyTo(responseStream, BufferSize);
  80. }
  81. }
  82. catch (Exception ex)
  83. {
  84. Logger.ErrorException("Error streaming data", ex);
  85. if (OnError != null)
  86. {
  87. OnError();
  88. }
  89. throw;
  90. }
  91. finally
  92. {
  93. if (OnComplete != null)
  94. {
  95. OnComplete();
  96. }
  97. }
  98. }
  99. public async Task WriteToAsync(Stream responseStream)
  100. {
  101. try
  102. {
  103. using (var src = SourceStream)
  104. {
  105. await src.CopyToAsync(responseStream, BufferSize).ConfigureAwait(false);
  106. }
  107. }
  108. catch (Exception ex)
  109. {
  110. Logger.ErrorException("Error streaming data", ex);
  111. if (OnError != null)
  112. {
  113. OnError();
  114. }
  115. throw;
  116. }
  117. finally
  118. {
  119. if (OnComplete != null)
  120. {
  121. OnComplete();
  122. }
  123. }
  124. }
  125. }
  126. }