//All code is copyright � 2007, InsomniaSoftware | Manuel Then, All rights reserved.
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace InsomniaSoftware.Server.Sources
{
/// <summary>
/// Represents a source which is able to send a stream to the client
/// </summary>
public class StreamSource : Source
{
Stream stream;
ContentType type;
SourceInfo sourceInfo = null;
bool closeAfterRequest;
/// <summary>
/// Creates a new StreamSource instance
/// </summary>
/// <param name="stream">Stream to be sent to the client</param>
/// <param name="contentType">ContentType of the data within the stream</param>
/// <param name="closeAfterRequest">If true, the stream is disposed after the end of this request</param>
public StreamSource(Stream stream, ContentType contentType, bool closeAfterRequest)
{
if (stream == null)
throw new Exception("Passed stream must not be null");
if (!stream.CanRead)
throw new Exception("Passed stream must be readable");
if (!stream.CanSeek)
throw new Exception("Passed stream must be seekable");
this.stream = stream;
this.type = contentType;
this.closeAfterRequest = closeAfterRequest;
}
/// <summary>
/// Creates a new StreamSource instance
/// </summary>
/// <param name="stream">Stream to be sent to the client</param>
/// <param name="contentType">ContentType of the data within the stream</param>
/// <param name="info">SourceInfo for this source</param>
/// /// <param name="closeAfterRequest">If true, the stream is disposed after the end of this request</param>
public StreamSource(Stream stream, ContentType contentType, SourceInfo info, bool closeAfterRequest)
: this(stream, contentType, closeAfterRequest)
{
this.sourceInfo = info;
}
/// <summary>
/// Destructor
/// </summary>
~StreamSource()
{
Dispose();
}
/// <summary>
/// Disposes the StreamSource
/// </summary>
public override void Dispose()
{
try
{
if (closeAfterRequest)
{
if (stream != null)
{
stream.Close();
stream.Dispose();
stream = null;
}
}
}
catch { }
}
/// <summary>
/// Gets the length of the source's content
/// </summary>
/// <returns>Content length</returns>
public override int GetLength()
{
return (int)stream.Length;
}
/// <summary>
/// Moves current the position withing the binary data
/// </summary>
/// <param name="offset">Offset by which the location will move</param>
/// <param name="origin">Position to start moving at</param>
/// <returns>The absolute position within the data</returns>
public override int Seek(int offset, SeekOrigin origin)
{
return (int)stream.Seek(offset, origin);
}
/// <summary>
/// Reads a specified number of bytes into the given buffer
/// </summary>
/// <param name="buffer">Buffer to be filled</param>
/// <param name="offset">First position in the buffer to be filled</param>
/// <param name="count">Number of bytes to be read</param>
/// <returns>The actual number of bytes read</returns>
public override int Read(ref byte[] buffer, int offset, int count)
{
return stream.Read(buffer, offset, count);
}
/// <summary>
/// Gets the content type of this source's content
/// </summary>
/// <returns>ContentType instance</returns>
public override ContentType GetContentType()
{
return type;
}
/// <summary>
/// Gets the SourceInfo
/// </summary>
/// <returns>SourceInfo of this StreamSource</returns>
public override SourceInfo GetInformation()
{
if (sourceInfo != null)
return sourceInfo;
else
return new SourceInfo();
}
}
}