StreamWriter.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. public Action OnError { get; set; }
  35. /// <summary>
  36. /// Initializes a new instance of the <see cref="StreamWriter" /> class.
  37. /// </summary>
  38. /// <param name="source">The source.</param>
  39. /// <param name="contentType">Type of the content.</param>
  40. /// <param name="logger">The logger.</param>
  41. public StreamWriter(Stream source, string contentType, ILogger logger)
  42. {
  43. if (string.IsNullOrEmpty(contentType))
  44. {
  45. throw new ArgumentNullException("contentType");
  46. }
  47. SourceStream = source;
  48. Logger = logger;
  49. Options["Content-Type"] = contentType;
  50. if (source.CanSeek)
  51. {
  52. Options["Content-Length"] = source.Length.ToString(UsCulture);
  53. }
  54. }
  55. /// <summary>
  56. /// Initializes a new instance of the <see cref="StreamWriter"/> class.
  57. /// </summary>
  58. /// <param name="source">The source.</param>
  59. /// <param name="contentType">Type of the content.</param>
  60. /// <param name="logger">The logger.</param>
  61. public StreamWriter(byte[] source, string contentType, ILogger logger)
  62. : this(new MemoryStream(source), contentType, logger)
  63. {
  64. }
  65. /// <summary>
  66. /// Writes to.
  67. /// </summary>
  68. /// <param name="responseStream">The response stream.</param>
  69. public void WriteTo(Stream responseStream)
  70. {
  71. WriteToInternal(responseStream);
  72. }
  73. // 256k
  74. private const int BufferSize = 262144;
  75. /// <summary>
  76. /// Writes to async.
  77. /// </summary>
  78. /// <param name="responseStream">The response stream.</param>
  79. /// <returns>Task.</returns>
  80. private void WriteToInternal(Stream responseStream)
  81. {
  82. try
  83. {
  84. using (var src = SourceStream)
  85. {
  86. src.CopyTo(responseStream, BufferSize);
  87. }
  88. }
  89. catch (Exception ex)
  90. {
  91. Logger.ErrorException("Error streaming data", ex);
  92. if (OnError != null)
  93. {
  94. OnError();
  95. }
  96. throw;
  97. }
  98. finally
  99. {
  100. if (OnComplete != null)
  101. {
  102. OnComplete();
  103. }
  104. }
  105. }
  106. }
  107. }