StreamWriter.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. 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[HeaderNames.ContentType] = contentType;
  48. }
  49. /// <summary>
  50. /// Initializes a new instance of the <see cref="StreamWriter"/> class.
  51. /// </summary>
  52. /// <param name="source">The source.</param>
  53. /// <param name="contentType">Type of the content.</param>
  54. public StreamWriter(byte[] source, string contentType)
  55. {
  56. if (string.IsNullOrEmpty(contentType))
  57. {
  58. throw new ArgumentNullException(nameof(contentType));
  59. }
  60. SourceBytes = source;
  61. Headers[HeaderNames.ContentType] = contentType;
  62. }
  63. public async Task WriteToAsync(Stream responseStream, CancellationToken cancellationToken)
  64. {
  65. try
  66. {
  67. var bytes = SourceBytes;
  68. if (bytes != null)
  69. {
  70. await responseStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
  71. }
  72. else
  73. {
  74. using (var src = SourceStream)
  75. {
  76. await src.CopyToAsync(responseStream).ConfigureAwait(false);
  77. }
  78. }
  79. }
  80. catch
  81. {
  82. if (OnError != null)
  83. {
  84. OnError();
  85. }
  86. throw;
  87. }
  88. finally
  89. {
  90. if (OnComplete != null)
  91. {
  92. OnComplete();
  93. }
  94. }
  95. }
  96. }
  97. }