public MemoryStream AsSeekableStream() { if (ResultStream.CanSeek) { ResultStream.Seek(0, SeekOrigin.Begin); } using (ResultStream) { var ms = new MemoryStream(); ResultStream.CopyTo(ms); ResultStream.Flush(); return(ms); } }
//public Stream AsRawStream() { // var rawStream = new MemoryStream(); // using (var writer = new StreamWriter(rawStream, new UTF8Encoding(), 8192, true)) { // writer.WriteLine($"HTTP/1.1 {StatusCode} {StatusDescription}"); // lock (this.Headers) { // this.Headers.ForEach(header => { // writer.WriteLine(); // }); // } // writer.WriteLine(); // } // ResultStream.CopyTo(rawStream); // rawStream.Seek(0, SeekOrigin.Begin); // return rawStream; //} public byte[] AsBuffer() { if (ResultStream.CanSeek) { ResultStream.Seek(0, SeekOrigin.Begin); } byte[] bytes; if (ResultStream is MemoryStream rsms) { bytes = rsms.ToArray(); } else { using (var ms = new MemoryStream()) { ResultStream.CopyTo(ms); ms.Seek(0, SeekOrigin.Begin); bytes = ms.ToArray(); } } return(bytes); }
public override void Output(HttpListenerResponse output) { output.StatusCode = StatusCode; output.ContentType = ContentType; ResultStream.CopyTo(output.OutputStream); }