2 // System.Web.HttpWriter
5 // Patrik Torstensson (Patrik.Torstensson@labs2.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Web.Util;
35 public sealed class HttpWriter : TextWriter
37 internal const int MaxBufferSize = 32768;
39 HttpResponse _Response;
41 HttpResponseStream _ResponseStream;
43 ReusableMemoryStream _OutputStream;
44 StreamWriter _OutputHelper;
48 HttpResponseStreamProxy _OutputProxy;
50 internal HttpWriter (HttpResponse Response)
54 _Encoding = _Response.ContentEncoding;
55 _OutputStream = new ReusableMemoryStream (MaxBufferSize);
56 _OutputHelper = new StreamWriter (_OutputStream, _Response.ContentEncoding);
57 _ResponseStream = new HttpResponseStream (this);
60 internal void Dispose ()
62 _OutputHelper.Close ();
63 _OutputStream.Close ();
64 _OutputFilter.Close ();
67 internal Stream GetActiveFilter ()
69 if (null == _OutputFilter) {
70 // Create a filter proxy to allow us to know if we have a valid filter
71 if (null == _OutputProxy)
72 _OutputProxy = new HttpResponseStreamProxy (this);
79 internal void ActivateFilter (Stream OutputFilter)
81 if (null == _OutputProxy)
82 throw new HttpException ("Invalid filter usage");
84 _OutputFilter = OutputFilter;
87 internal void FilterData (bool CloseStream)
89 // Check if we have any filter at all
90 if (null == _OutputFilter)
95 // Save our current data
96 byte [] arrData = _OutputStream.GetBuffer ();
97 int size = (int) _OutputStream.Length;
99 // Remove our internal data
102 // If we have a filter then we have a proxy
103 _OutputProxy.Active = true;
106 // Call the filter (it does a callback into our HttpWriter again)
107 _OutputFilter.Write (arrData, 0, size);
108 _OutputFilter.Flush ();
111 _OutputFilter.Close ();
113 _OutputProxy.Active = false;
117 internal void Clear ()
119 _OutputHelper.Close ();
120 _OutputStream = new ReusableMemoryStream (_OutputStream.GetBuffer ());
121 _OutputHelper = new StreamWriter (_OutputStream, _Response.ContentEncoding);
124 internal void SendContent (HttpWorkerRequest Handler)
128 int l = (int)_OutputStream.Length;
130 byte [] arrContent = _OutputStream.GetBuffer ();
131 Handler.SendResponseFromMemory (arrContent, l);
135 internal void Update ()
137 _Encoding = _Response.ContentEncoding;
138 _OutputHelper.Flush ();
139 _OutputHelper = new StreamWriter (_OutputStream, _Encoding);
142 internal long BufferSize
146 return _OutputStream.Length;
150 internal void FlushBuffers ()
152 _OutputHelper.Flush ();
153 _OutputStream.Flush ();
156 internal byte[] GetBuffer () {
157 return _OutputStream.GetBuffer ();
160 public override Encoding Encoding
162 get { return _Encoding; }
165 public Stream OutputStream
167 get { return _ResponseStream; }
170 public override void Close ()
176 public override void Flush ()
180 private void CheckIfFlush ()
182 if (!_Response.BufferOutput) {
187 public override void Write (char ch)
189 _OutputHelper.Write (ch);
193 public override void Write (object obj)
197 _OutputHelper.Write (obj.ToString ());
202 public override void Write (string s)
204 _OutputHelper.Write (s);
208 public override void Write (char [] buffer, int index, int count)
210 _OutputHelper.Write (buffer, index, count);
214 public void WriteBytes (byte [] buffer, int index, int count)
216 _OutputStream.Write (buffer, index, count);
220 public override void WriteLine ()
222 _OutputHelper.Write ("\r\n");
226 public void WriteString (string s, int index, int count)
228 _OutputHelper.Write (s.Substring (index, count));