StreamWriter.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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.Net.Http.Headers;
  9. namespace Emby.Server.Implementations.HttpServer
  10. {
  11. /// <summary>
  12. /// Class StreamWriter.
  13. /// </summary>
  14. public class StreamWriter : IAsyncStreamWriter, IHasHeaders
  15. {
  16. /// <summary>
  17. /// The options.
  18. /// </summary>
  19. private readonly IDictionary<string, string> _options = new Dictionary<string, string>();
  20. /// <summary>
  21. /// Initializes a new instance of the <see cref="StreamWriter" /> class.
  22. /// </summary>
  23. /// <param name="source">The source.</param>
  24. /// <param name="contentType">Type of the content.</param>
  25. public StreamWriter(Stream source, string contentType)
  26. {
  27. if (string.IsNullOrEmpty(contentType))
  28. {
  29. throw new ArgumentNullException(nameof(contentType));
  30. }
  31. SourceStream = source;
  32. Headers["Content-Type"] = contentType;
  33. if (source.CanSeek)
  34. {
  35. Headers[HeaderNames.ContentLength] = source.Length.ToString(CultureInfo.InvariantCulture);
  36. }
  37. Headers[HeaderNames.ContentType] = contentType;
  38. }
  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="contentLength">The content length.</param>
  45. public StreamWriter(byte[] source, string contentType, int contentLength)
  46. {
  47. if (string.IsNullOrEmpty(contentType))
  48. {
  49. throw new ArgumentNullException(nameof(contentType));
  50. }
  51. SourceBytes = source;
  52. Headers[HeaderNames.ContentLength] = contentLength.ToString(CultureInfo.InvariantCulture);
  53. Headers[HeaderNames.ContentType] = contentType;
  54. }
  55. /// <summary>
  56. /// Gets or sets the source stream.
  57. /// </summary>
  58. /// <value>The source stream.</value>
  59. private Stream SourceStream { get; set; }
  60. private byte[] SourceBytes { get; set; }
  61. /// <summary>
  62. /// Gets the options.
  63. /// </summary>
  64. /// <value>The options.</value>
  65. public IDictionary<string, string> Headers => _options;
  66. /// <summary>
  67. /// Fires when complete.
  68. /// </summary>
  69. public Action OnComplete { get; set; }
  70. /// <summary>
  71. /// Fires when an error occours.
  72. /// </summary>
  73. public Action OnError { get; set; }
  74. /// <inheritdoc />
  75. public async Task WriteToAsync(Stream responseStream, CancellationToken cancellationToken)
  76. {
  77. try
  78. {
  79. var bytes = SourceBytes;
  80. if (bytes != null)
  81. {
  82. await responseStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
  83. }
  84. else
  85. {
  86. using (var src = SourceStream)
  87. {
  88. await src.CopyToAsync(responseStream).ConfigureAwait(false);
  89. }
  90. }
  91. }
  92. catch
  93. {
  94. OnError?.Invoke();
  95. throw;
  96. }
  97. finally
  98. {
  99. OnComplete?.Invoke();
  100. }
  101. }
  102. }
  103. }