2
0

StreamWriter.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using MediaBrowser.Model.Logging;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using MediaBrowser.Common.IO;
  9. using MediaBrowser.Model.Services;
  10. namespace Emby.Server.Implementations.HttpServer
  11. {
  12. /// <summary>
  13. /// Class StreamWriter
  14. /// </summary>
  15. public class StreamWriter : IAsyncStreamWriter, IHasHeaders
  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. private byte[] SourceBytes { get; set; }
  25. /// <summary>
  26. /// The _options
  27. /// </summary>
  28. private readonly IDictionary<string, string> _options = new Dictionary<string, string>();
  29. /// <summary>
  30. /// Gets the options.
  31. /// </summary>
  32. /// <value>The options.</value>
  33. public IDictionary<string, string> Headers
  34. {
  35. get { return _options; }
  36. }
  37. public Action OnComplete { get; set; }
  38. public Action OnError { get; set; }
  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. Headers["Content-Type"] = contentType;
  54. if (source.CanSeek)
  55. {
  56. Headers["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. {
  67. if (string.IsNullOrEmpty(contentType))
  68. {
  69. throw new ArgumentNullException("contentType");
  70. }
  71. SourceBytes = source;
  72. Logger = logger;
  73. Headers["Content-Type"] = contentType;
  74. Headers["Content-Length"] = source.Length.ToString(UsCulture);
  75. }
  76. public async Task WriteToAsync(Stream responseStream, CancellationToken cancellationToken)
  77. {
  78. try
  79. {
  80. var bytes = SourceBytes;
  81. if (bytes != null)
  82. {
  83. await responseStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
  84. }
  85. else
  86. {
  87. using (var src = SourceStream)
  88. {
  89. await src.CopyToAsync(responseStream).ConfigureAwait(false);
  90. }
  91. }
  92. }
  93. catch (Exception ex)
  94. {
  95. Logger.ErrorException("Error streaming data", ex);
  96. if (OnError != null)
  97. {
  98. OnError();
  99. }
  100. throw;
  101. }
  102. finally
  103. {
  104. if (OnComplete != null)
  105. {
  106. OnComplete();
  107. }
  108. }
  109. }
  110. }
  111. }