using System; using System.Collections.Generic; using System.IO; using System.Threading.Tasks; using ServiceStack; using ServiceStack.Web; namespace MediaBrowser.Server.Implementations.HttpServer { public class AsyncStreamWriterFunc : IStreamWriter, IAsyncStreamWriter, IHasOptions { /// /// Gets or sets the source stream. /// /// The source stream. private Func Writer { get; set; } /// /// Gets the options. /// /// The options. public IDictionary Options { get; private set; } public Action OnComplete { get; set; } public Action OnError { get; set; } /// /// Initializes a new instance of the class. /// public AsyncStreamWriterFunc(Func writer, IDictionary headers) { Writer = writer; if (headers == null) { headers = new Dictionary(StringComparer.OrdinalIgnoreCase); } Options = headers; } /// /// Writes to. /// /// The response stream. public void WriteTo(Stream responseStream) { var task = Writer(responseStream); Task.WaitAll(task); } public async Task WriteToAsync(Stream responseStream) { await Writer(responseStream).ConfigureAwait(false); } } }