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 MemoryStream _OutputStream;
44 StreamWriter _OutputHelper;
48 HttpResponseStreamProxy _OutputProxy;
50 internal HttpWriter (HttpResponse Response)
54 _OutputStream = new MemoryStream (MaxBufferSize);
55 _OutputHelper = new StreamWriter (_OutputStream, _Response.ContentEncoding);
56 _ResponseStream = new HttpResponseStream (this);
61 internal void Dispose ()
63 _OutputHelper.Close ();
64 _OutputStream.Close ();
65 _OutputFilter.Close ();
68 internal Stream GetActiveFilter ()
70 if (null == _OutputFilter) {
71 // Create a filter proxy to allow us to know if we have a valid filter
72 if (null == _OutputProxy)
73 _OutputProxy = new HttpResponseStreamProxy (this);
80 internal void ActivateFilter (Stream OutputFilter)
82 if (null == _OutputProxy)
83 throw new HttpException ("Invalid filter usage");
85 _OutputFilter = OutputFilter;
88 internal void FilterData (bool CloseStream)
90 // Check if we have any filter at all
91 if (null == _OutputFilter)
96 // Save our current data
97 byte [] arrData = _OutputStream.GetBuffer ();
98 int size = (int) _OutputStream.Length;
100 // Remove our internal data
103 // If we have a filter then we have a proxy
104 _OutputProxy.Active = true;
107 // Call the filter (it does a callback into our HttpWriter again)
108 _OutputFilter.Write (arrData, 0, size);
109 _OutputFilter.Flush ();
112 _OutputFilter.Close ();
114 _OutputProxy.Active = false;
118 internal void Clear ()
120 _OutputHelper.Close ();
121 _OutputStream.Close ();
123 _OutputStream = new MemoryStream (32768);
124 _OutputHelper = new StreamWriter (_OutputStream, _Response.ContentEncoding);
127 internal void SendContent (HttpWorkerRequest Handler)
131 int l = (int)_OutputStream.Length;
133 byte [] arrContent = _OutputStream.GetBuffer ();
134 Handler.SendResponseFromMemory (arrContent, l);
138 internal void Update ()
140 _Encoding = _Response.ContentEncoding;
141 _OutputHelper.Flush ();
142 _OutputHelper = new StreamWriter (_OutputStream, _Encoding);
145 internal long BufferSize
149 return _OutputStream.Length;
153 internal void FlushBuffers ()
155 _OutputHelper.Flush ();
156 _OutputStream.Flush ();
159 internal byte[] GetBuffer () {
160 return _OutputStream.GetBuffer ();
163 public override Encoding Encoding
165 get { return _Encoding; }
168 public Stream OutputStream
170 get { return _ResponseStream; }
173 public override void Close ()
179 public override void Flush ()
183 private void CheckIfFlush ()
185 if (!_Response.BufferOutput) {
190 public override void Write (char ch)
192 _OutputHelper.Write (ch);
196 public override void Write (object obj)
200 _OutputHelper.Write (obj.ToString ());
205 public override void Write (string s)
207 _OutputHelper.Write (s);
211 public override void Write (char [] buffer, int index, int count)
213 _OutputHelper.Write (buffer, index, count);
217 public void WriteBytes (byte [] buffer, int index, int count)
219 _OutputStream.Write (buffer, index, count);
223 public override void WriteLine ()
225 _OutputHelper.Write ("\r\n");
229 public void WriteString (string s, int index, int count)
231 _OutputHelper.Write (s.Substring (index, count));