StreamWriter.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using MediaBrowser.Model.Services;
  8. using Microsoft.Extensions.Logging;
  9. namespace Emby.Server.Implementations.HttpServer
  10. {
  11. /// <summary>
  12. /// Class StreamWriter
  13. /// </summary>
  14. public class StreamWriter : IAsyncStreamWriter, IHasHeaders
  15. {
  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. private byte[] SourceBytes { 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> Headers => _options;
  32. public Action OnComplete { get; set; }
  33. public Action OnError { get; set; }
  34. /// <summary>
  35. /// Initializes a new instance of the <see cref="StreamWriter" /> class.
  36. /// </summary>
  37. /// <param name="source">The source.</param>
  38. /// <param name="contentType">Type of the content.</param>
  39. /// <param name="logger">The logger.</param>
  40. public StreamWriter(Stream source, string contentType)
  41. {
  42. if (string.IsNullOrEmpty(contentType))
  43. {
  44. throw new ArgumentNullException(nameof(contentType));
  45. }
  46. SourceStream = source;
  47. Headers["Content-Type"] = contentType;
  48. if (source.CanSeek)
  49. {
  50. Headers["Content-Length"] = source.Length.ToString(UsCulture);
  51. }
  52. }
  53. /// <summary>
  54. /// Initializes a new instance of the <see cref="StreamWriter"/> class.
  55. /// </summary>
  56. /// <param name="source">The source.</param>
  57. /// <param name="contentType">Type of the content.</param>
  58. /// <param name="logger">The logger.</param>
  59. public StreamWriter(byte[] source, string contentType, int contentLength)
  60. {
  61. if (string.IsNullOrEmpty(contentType))
  62. {
  63. throw new ArgumentNullException(nameof(contentType));
  64. }
  65. SourceBytes = source;
  66. Headers["Content-Type"] = contentType;
  67. Headers["Content-Length"] = contentLength.ToString(UsCulture);
  68. }
  69. public async Task WriteToAsync(Stream responseStream, CancellationToken cancellationToken)
  70. {
  71. try
  72. {
  73. var bytes = SourceBytes;
  74. if (bytes != null)
  75. {
  76. await responseStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
  77. }
  78. else
  79. {
  80. using (var src = SourceStream)
  81. {
  82. await src.CopyToAsync(responseStream).ConfigureAwait(false);
  83. }
  84. }
  85. }
  86. catch
  87. {
  88. if (OnError != null)
  89. {
  90. OnError();
  91. }
  92. throw;
  93. }
  94. finally
  95. {
  96. if (OnComplete != null)
  97. {
  98. OnComplete();
  99. }
  100. }
  101. }
  102. }
  103. }