StreamWriter.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using MediaBrowser.Model.Services;
  7. using Microsoft.Net.Http.Headers;
  8. namespace Emby.Server.Implementations.HttpServer
  9. {
  10. /// <summary>
  11. /// Class StreamWriter
  12. /// </summary>
  13. public class StreamWriter : IAsyncStreamWriter, IHasHeaders
  14. {
  15. /// <summary>
  16. /// Gets or sets the source stream.
  17. /// </summary>
  18. /// <value>The source stream.</value>
  19. private Stream SourceStream { get; set; }
  20. private byte[] SourceBytes { get; set; }
  21. /// <summary>
  22. /// The _options
  23. /// </summary>
  24. private readonly IDictionary<string, string> _options = new Dictionary<string, string>();
  25. /// <summary>
  26. /// Gets the options.
  27. /// </summary>
  28. /// <value>The options.</value>
  29. public IDictionary<string, string> Headers => _options;
  30. public Action OnComplete { get; set; }
  31. public Action OnError { get; set; }
  32. /// <summary>
  33. /// Initializes a new instance of the <see cref="StreamWriter" /> class.
  34. /// </summary>
  35. /// <param name="source">The source.</param>
  36. /// <param name="contentType">Type of the content.</param>
  37. /// <param name="logger">The logger.</param>
  38. public StreamWriter(Stream source, string contentType)
  39. {
  40. if (string.IsNullOrEmpty(contentType))
  41. {
  42. throw new ArgumentNullException(nameof(contentType));
  43. }
  44. SourceStream = source;
  45. Headers[HeaderNames.ContentType] = contentType;
  46. }
  47. /// <summary>
  48. /// Initializes a new instance of the <see cref="StreamWriter"/> class.
  49. /// </summary>
  50. /// <param name="source">The source.</param>
  51. /// <param name="contentType">Type of the content.</param>
  52. public StreamWriter(byte[] source, string contentType)
  53. {
  54. if (string.IsNullOrEmpty(contentType))
  55. {
  56. throw new ArgumentNullException(nameof(contentType));
  57. }
  58. SourceBytes = source;
  59. Headers[HeaderNames.ContentType] = contentType;
  60. }
  61. public async Task WriteToAsync(Stream responseStream, CancellationToken cancellationToken)
  62. {
  63. try
  64. {
  65. var bytes = SourceBytes;
  66. if (bytes != null)
  67. {
  68. await responseStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
  69. }
  70. else
  71. {
  72. using (var src = SourceStream)
  73. {
  74. await src.CopyToAsync(responseStream).ConfigureAwait(false);
  75. }
  76. }
  77. }
  78. catch
  79. {
  80. if (OnError != null)
  81. {
  82. OnError();
  83. }
  84. throw;
  85. }
  86. finally
  87. {
  88. if (OnComplete != null)
  89. {
  90. OnComplete();
  91. }
  92. }
  93. }
  94. }
  95. }