StaticRemoteStreamWriter.cs 2.1 KB

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