using MediaBrowser.Model.Logging;
using ServiceStack.Service;
using System;
using System.IO;
using System.Threading.Tasks;
namespace MediaBrowser.Server.Implementations.HttpServer
{
///
/// Class StreamWriter
///
public class StreamWriter : IStreamWriter
{
private ILogger Logger { get; set; }
///
/// Gets or sets the source stream.
///
/// The source stream.
public Stream SourceStream { get; set; }
///
/// Initializes a new instance of the class.
///
/// The source.
/// The logger.
public StreamWriter(Stream source, ILogger logger)
{
SourceStream = source;
Logger = logger;
}
///
/// Writes to.
///
/// The response stream.
public void WriteTo(Stream responseStream)
{
var task = WriteToAsync(responseStream);
Task.WaitAll(task);
}
///
/// Writes to async.
///
/// The response stream.
/// Task.
private async Task WriteToAsync(Stream responseStream)
{
try
{
using (var src = SourceStream)
{
await src.CopyToAsync(responseStream).ConfigureAwait(false);
}
}
catch (Exception ex)
{
Logger.ErrorException("Error streaming media", ex);
throw;
}
}
}
}