StreamWriter.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 MediaBrowser.Common.IO;
  9. using ServiceStack;
  10. namespace MediaBrowser.Server.Implementations.HttpServer
  11. {
  12. /// <summary>
  13. /// Class StreamWriter
  14. /// </summary>
  15. public class StreamWriter : IStreamWriter, IAsyncStreamWriter, IHasOptions
  16. {
  17. private ILogger Logger { get; set; }
  18. private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
  19. /// <summary>
  20. /// Gets or sets the source stream.
  21. /// </summary>
  22. /// <value>The source stream.</value>
  23. private Stream SourceStream { get; set; }
  24. /// <summary>
  25. /// The _options
  26. /// </summary>
  27. private readonly IDictionary<string, string> _options = new Dictionary<string, string>();
  28. /// <summary>
  29. /// Gets the options.
  30. /// </summary>
  31. /// <value>The options.</value>
  32. public IDictionary<string, string> Options
  33. {
  34. get { return _options; }
  35. }
  36. public Action OnComplete { get; set; }
  37. public Action OnError { get; set; }
  38. private readonly byte[] _bytes;
  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. if (string.IsNullOrEmpty(contentType))
  69. {
  70. throw new ArgumentNullException("contentType");
  71. }
  72. _bytes = source;
  73. Logger = logger;
  74. Options["Content-Type"] = contentType;
  75. Options["Content-Length"] = source.Length.ToString(UsCulture);
  76. }
  77. private const int BufferSize = 81920;
  78. /// <summary>
  79. /// Writes to.
  80. /// </summary>
  81. /// <param name="responseStream">The response stream.</param>
  82. public void WriteTo(Stream responseStream)
  83. {
  84. try
  85. {
  86. if (_bytes != null)
  87. {
  88. responseStream.Write(_bytes, 0, _bytes.Length);
  89. }
  90. else
  91. {
  92. using (var src = SourceStream)
  93. {
  94. src.CopyTo(responseStream, BufferSize);
  95. }
  96. }
  97. }
  98. catch (Exception ex)
  99. {
  100. Logger.ErrorException("Error streaming data", ex);
  101. if (OnError != null)
  102. {
  103. OnError();
  104. }
  105. throw;
  106. }
  107. finally
  108. {
  109. if (OnComplete != null)
  110. {
  111. OnComplete();
  112. }
  113. }
  114. }
  115. public async Task WriteToAsync(Stream responseStream)
  116. {
  117. try
  118. {
  119. if (_bytes != null)
  120. {
  121. await responseStream.WriteAsync(_bytes, 0, _bytes.Length);
  122. }
  123. else
  124. {
  125. using (var src = SourceStream)
  126. {
  127. await src.CopyToAsync(responseStream, BufferSize).ConfigureAwait(false);
  128. }
  129. }
  130. }
  131. catch (Exception ex)
  132. {
  133. Logger.ErrorException("Error streaming data", ex);
  134. if (OnError != null)
  135. {
  136. OnError();
  137. }
  138. throw;
  139. }
  140. finally
  141. {
  142. if (OnComplete != null)
  143. {
  144. OnComplete();
  145. }
  146. }
  147. }
  148. }
  149. }