StreamWriter.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. private const int BufferSize = 81920;
  68. /// <summary>
  69. /// Writes to.
  70. /// </summary>
  71. /// <param name="responseStream">The response stream.</param>
  72. public void WriteTo(Stream responseStream)
  73. {
  74. try
  75. {
  76. using (var src = SourceStream)
  77. {
  78. src.CopyTo(responseStream, BufferSize);
  79. }
  80. }
  81. catch (Exception ex)
  82. {
  83. Logger.ErrorException("Error streaming data", ex);
  84. if (OnError != null)
  85. {
  86. OnError();
  87. }
  88. throw;
  89. }
  90. finally
  91. {
  92. if (OnComplete != null)
  93. {
  94. OnComplete();
  95. }
  96. }
  97. }
  98. public async Task WriteToAsync(Stream responseStream)
  99. {
  100. try
  101. {
  102. using (var src = SourceStream)
  103. {
  104. await src.CopyToAsync(responseStream, BufferSize).ConfigureAwait(false);
  105. }
  106. }
  107. catch (Exception ex)
  108. {
  109. Logger.ErrorException("Error streaming data", ex);
  110. if (OnError != null)
  111. {
  112. OnError();
  113. }
  114. throw;
  115. }
  116. finally
  117. {
  118. if (OnComplete != null)
  119. {
  120. OnComplete();
  121. }
  122. }
  123. }
  124. }
  125. }