| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- //
- // System.Web.HttpWriter
- //
- // Author:
- // Patrik Torstensson ([email protected])
- //
- using System;
- using System.IO;
- using System.Text;
- using System.Web.Util;
- namespace System.Web
- {
- public sealed class HttpWriter : TextWriter
- {
- HttpResponse _Response;
- HttpResponseStream _ResponseStream;
- MemoryStream _OutputStream;
- StreamWriter _OutputHelper;
- Encoding _Encoding;
- Stream _OutputFilter;
- HttpResponseStreamProxy _OutputProxy;
- internal HttpWriter (HttpResponse Response)
- {
- _Response = Response;
- _OutputStream = new MemoryStream (32768);
- _OutputHelper = new StreamWriter (_OutputStream, _Response.ContentEncoding);
- _ResponseStream = new HttpResponseStream (this);
- Update ();
- }
- internal void Dispose ()
- {
- _OutputHelper.Close ();
- _OutputStream.Close ();
- _OutputFilter.Close ();
- }
- internal Stream GetActiveFilter ()
- {
- if (null == _OutputFilter) {
- // Create a filter proxy to allow us to know if we have a valid filter
- if (null == _OutputProxy)
- _OutputProxy = new HttpResponseStreamProxy (this);
- return _OutputProxy;
- }
- return _OutputFilter;
- }
- internal void ActivateFilter (Stream OutputFilter)
- {
- if (null == _OutputProxy)
- throw new HttpException ("Invalid filter usage");
- _OutputFilter = OutputFilter;
- }
- internal void FilterData (bool CloseStream)
- {
- // Check if we have any filter at all
- if (null == _OutputFilter)
- return;
- FlushBuffers ();
- // Save our current data
- byte [] arrData = _OutputStream.GetBuffer ();
- int size = (int) _OutputStream.Length;
- // Remove our internal data
- Clear ();
- // If we have a filter then we have a proxy
- _OutputProxy.Active = true;
- try {
- // Call the filter (it does a callback into our HttpWriter again)
- _OutputFilter.Write (arrData, 0, size);
- _OutputFilter.Flush ();
- if (CloseStream)
- _OutputFilter.Close ();
- } finally {
- _OutputProxy.Active = false;
- }
- }
- internal void Clear ()
- {
- _OutputHelper.Close ();
- _OutputStream.Close ();
-
- // Quick way of doing cleanup
- _OutputStream = new MemoryStream (32768);
- _OutputHelper = new StreamWriter (_OutputStream, _Response.ContentEncoding);
- }
- internal void SendContent (HttpWorkerRequest Handler)
- {
- FlushBuffers();
- int l = (int)_OutputStream.Length;
- if (l > 0) {
- byte [] arrContent = _OutputStream.GetBuffer ();
- Handler.SendResponseFromMemory (arrContent, l);
- }
- }
- internal void Update ()
- {
- _Encoding = _Response.ContentEncoding;
- _OutputHelper.Flush ();
- _OutputHelper = new StreamWriter (_OutputStream, _Encoding);
- }
- internal long BufferSize
- {
- get {
- FlushBuffers ();
- return _OutputStream.Length;
- }
- }
- internal void FlushBuffers ()
- {
- _OutputHelper.Flush ();
- _OutputStream.Flush ();
- }
- public override Encoding Encoding
- {
- get { return _Encoding; }
- }
- public Stream OutputStream
- {
- get { return _ResponseStream; }
- }
- public override void Close ()
- {
- FlushBuffers ();
- _Response.Flush ();
- _Response.Close ();
- }
- public override void Flush ()
- {
- }
- private void CheckIfFlush ()
- {
- if (!_Response.BufferOutput) {
- FlushBuffers ();
- _Response.Flush ();
- }
- }
- public override void Write (char ch)
- {
- _OutputHelper.Write (ch);
- CheckIfFlush ();
- }
- public override void Write (object obj)
- {
- _OutputHelper.Write (obj.ToString ());
- CheckIfFlush ();
- }
- public override void Write (string s)
- {
- _OutputHelper.Write (s);
- CheckIfFlush ();
- }
- public override void Write (char [] buffer, int index, int count)
- {
- _OutputHelper.Write (buffer, index, count);
- CheckIfFlush ();
- }
- public void WriteBytes (byte [] buffer, int index, int count)
- {
- _OutputStream.Write (buffer, index, count);
- CheckIfFlush ();
- }
- public override void WriteLine ()
- {
- _OutputHelper.Write ("\r\n");
- CheckIfFlush ();
- }
- public void WriteString (string s, int index, int count)
- {
- _OutputHelper.Write (s.Substring (index, count));
- CheckIfFlush ();
- }
- }
- }
|