StreamWriter.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using MediaBrowser.Model.Logging;
  2. using ServiceStack.Web;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. using System.IO;
  7. namespace MediaBrowser.Server.Implementations.HttpServer
  8. {
  9. /// <summary>
  10. /// Class StreamWriter
  11. /// </summary>
  12. public class StreamWriter : IStreamWriter, IHasOptions
  13. {
  14. private ILogger Logger { get; set; }
  15. private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
  16. /// <summary>
  17. /// Gets or sets the source stream.
  18. /// </summary>
  19. /// <value>The source stream.</value>
  20. private Stream SourceStream { 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> Options
  30. {
  31. get { return _options; }
  32. }
  33. public Action OnComplete { 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, ILogger logger)
  41. {
  42. if (string.IsNullOrEmpty(contentType))
  43. {
  44. throw new ArgumentNullException("contentType");
  45. }
  46. SourceStream = source;
  47. Logger = logger;
  48. Options["Content-Type"] = contentType;
  49. if (source.CanSeek)
  50. {
  51. Options["Content-Length"] = source.Length.ToString(UsCulture);
  52. }
  53. }
  54. /// <summary>
  55. /// Initializes a new instance of the <see cref="StreamWriter"/> class.
  56. /// </summary>
  57. /// <param name="source">The source.</param>
  58. /// <param name="contentType">Type of the content.</param>
  59. /// <param name="logger">The logger.</param>
  60. public StreamWriter(byte[] source, string contentType, ILogger logger)
  61. : this(new MemoryStream(source), contentType, logger)
  62. {
  63. }
  64. /// <summary>
  65. /// Writes to.
  66. /// </summary>
  67. /// <param name="responseStream">The response stream.</param>
  68. public void WriteTo(Stream responseStream)
  69. {
  70. WriteToInternal(responseStream);
  71. }
  72. // 256k
  73. private const int BufferSize = 262144;
  74. /// <summary>
  75. /// Writes to async.
  76. /// </summary>
  77. /// <param name="responseStream">The response stream.</param>
  78. /// <returns>Task.</returns>
  79. private void WriteToInternal(Stream responseStream)
  80. {
  81. try
  82. {
  83. using (var src = SourceStream)
  84. {
  85. src.CopyTo(responseStream, BufferSize);
  86. }
  87. }
  88. catch (Exception ex)
  89. {
  90. Logger.ErrorException("Error streaming data", ex);
  91. throw;
  92. }
  93. finally
  94. {
  95. if (OnComplete != null)
  96. {
  97. OnComplete();
  98. }
  99. }
  100. }
  101. }
  102. }