StaticRemoteStreamWriter.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using ServiceStack.Service;
  2. using ServiceStack.ServiceHost;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Net.Http;
  6. using System.Threading.Tasks;
  7. namespace MediaBrowser.Api.Playback
  8. {
  9. /// <summary>
  10. /// Class StaticRemoteStreamWriter
  11. /// </summary>
  12. public class StaticRemoteStreamWriter : IStreamWriter, IHasOptions
  13. {
  14. /// <summary>
  15. /// The _input stream
  16. /// </summary>
  17. private readonly HttpResponseMessage _msg;
  18. private readonly HttpClient _client;
  19. /// <summary>
  20. /// The _options
  21. /// </summary>
  22. private readonly IDictionary<string, string> _options = new Dictionary<string, string>();
  23. /// <summary>
  24. /// Initializes a new instance of the <see cref="StaticRemoteStreamWriter"/> class.
  25. /// </summary>
  26. public StaticRemoteStreamWriter(HttpResponseMessage msg, HttpClient client)
  27. {
  28. _msg = msg;
  29. _client = client;
  30. }
  31. /// <summary>
  32. /// Gets the options.
  33. /// </summary>
  34. /// <value>The options.</value>
  35. public IDictionary<string, string> Options
  36. {
  37. get { return _options; }
  38. }
  39. /// <summary>
  40. /// Writes to.
  41. /// </summary>
  42. /// <param name="responseStream">The response stream.</param>
  43. public void WriteTo(Stream responseStream)
  44. {
  45. var task = WriteToAsync(responseStream);
  46. Task.WaitAll(task);
  47. }
  48. /// <summary>
  49. /// Writes to async.
  50. /// </summary>
  51. /// <param name="responseStream">The response stream.</param>
  52. /// <returns>Task.</returns>
  53. public async Task WriteToAsync(Stream responseStream)
  54. {
  55. using (_client)
  56. {
  57. using (_msg)
  58. {
  59. using (var input = await _msg.Content.ReadAsStreamAsync().ConfigureAwait(false))
  60. {
  61. await input.CopyToAsync(responseStream).ConfigureAwait(false);
  62. }
  63. }
  64. }
  65. }
  66. }
  67. }