public static bool WriteToOutputStream(IResponse response, object result, byte[] bodyPrefix, byte[] bodySuffix)
{
var partialResult = result as IPartialWriter;
if (HostContext.Config.AllowPartialResponses && partialResult != null && partialResult.IsPartialRequest)
{
partialResult.WritePartialTo(response);
return true;
}
var streamWriter = result as IStreamWriter;
if (streamWriter != null)
{
if (bodyPrefix != null) response.OutputStream.Write(bodyPrefix, 0, bodyPrefix.Length);
streamWriter.WriteTo(response.OutputStream);
if (bodySuffix != null) response.OutputStream.Write(bodySuffix, 0, bodySuffix.Length);
return true;
}
var stream = result as Stream;
if (stream != null)
{
if (bodyPrefix != null) response.OutputStream.Write(bodyPrefix, 0, bodyPrefix.Length);
stream.WriteTo(response.OutputStream);
if (bodySuffix != null) response.OutputStream.Write(bodySuffix, 0, bodySuffix.Length);
return true;
}
var bytes = result as byte[];
if (bytes != null)
{
var bodyPadding = bodyPrefix?.Length ?? 0;
if (bodySuffix != null)
bodyPadding += bodySuffix.Length;
response.ContentType = MimeTypes.Binary;
response.SetContentLength(bytes.Length + bodyPadding);
if (bodyPrefix != null) response.OutputStream.Write(bodyPrefix, 0, bodyPrefix.Length);
response.OutputStream.Write(bytes, 0, bytes.Length);
if (bodySuffix != null) response.OutputStream.Write(bodySuffix, 0, bodySuffix.Length);
return true;
}
return false;
}