2 // System.Web.HttpWriter
5 // Patrik Torstensson (Patrik.Torstensson@labs2.com)
13 public sealed class HttpWriter : TextWriter
15 HttpResponse _Response;
17 HttpResponseStream _ResponseStream;
19 MemoryStream _OutputStream;
20 StreamWriter _OutputHelper;
25 HttpResponseStreamProxy _OutputProxy;
27 internal HttpWriter (HttpResponse Response)
31 _OutputStream = new MemoryStream (32768);
32 _OutputHelper = new StreamWriter (_OutputStream, Encoding.Unicode);
33 _ResponseStream = new HttpResponseStream (this);
38 internal void Dispose ()
40 _OutputHelper.Close ();
41 _OutputStream.Close ();
42 _OutputFilter.Close ();
45 internal Stream GetActiveFilter()
47 if (null == _OutputFilter) {
48 // Create a filter proxy to allow us to know if we have a valid filter
49 if (null == _OutputProxy)
50 _OutputProxy = new HttpResponseStreamProxy (this);
57 internal void ActivateFilter (Stream OutputFilter)
59 if (null == _OutputProxy)
60 throw new HttpException ("Invalid filter usage");
62 _OutputFilter = OutputFilter;
65 internal void FilterData (bool CloseStream)
67 // Check if we have any filter at all
68 if (null == _OutputFilter)
73 // Save our current data
74 byte [] arrData = _OutputStream.ToArray ();
76 // Remove our internal data
79 // If we have a filter then we have a proxy
80 _OutputProxy.Active = true;
83 // Call the filter (it does a callback into our HttpWriter again)
84 _OutputFilter.Write (arrData, 0, arrData.Length);
87 _OutputFilter.Close ();
89 _OutputProxy.Active = false;
93 internal void Clear ()
95 _OutputHelper.Close ();
96 _OutputStream.Close ();
98 // Quick way of doing cleanup
99 _OutputStream = new MemoryStream (32768);
100 _OutputHelper = new StreamWriter (_OutputStream, Encoding.Unicode);
103 internal byte [] ToArray ()
105 if (_OutputStream.Length == 0)
109 byte [] arrStream = _OutputStream.ToArray ();
110 byte [] arrData = new byte [arrStream.Length - 2];
112 Buffer.BlockCopy (arrStream, 2, arrData, 0, arrStream.Length - 2);
114 byte [] arrContent = _Encoding.GetBytes (Encoding.Unicode.GetString (arrData));
119 internal void SendContent (HttpWorkerRequest Handler)
123 if (_OutputStream.Length > 0) {
124 byte [] arrContent = ToArray ();
125 Handler.SendResponseFromMemory (arrContent, arrContent.Length);
129 internal void Update ()
131 _Encoder = _Response.ContentEncoder;
132 _Encoding = _Response.ContentEncoding;
135 internal long BufferSize
140 if (_OutputStream.Length == 0)
143 return ToArray ().Length;
147 internal void FlushBuffers ()
149 _OutputHelper.Flush ();
150 _OutputStream.Flush ();
153 public override Encoding Encoding
155 get { return _Encoding; }
158 public Stream OutputStream
160 get { return _ResponseStream; }
163 public override void Close ()
170 public override void Flush ()
176 public override void Write (char ch)
178 _OutputHelper.Write (ch);
179 if (!_Response.BufferOutput) {
185 public override void Write (object obj)
187 _OutputHelper.Write (obj.ToString ());
188 if (!_Response.BufferOutput) {
194 public override void Write (string s)
196 _OutputHelper.Write (s);
197 if (!_Response.BufferOutput) {
203 public override void Write (char [] buffer, int index, int count)
205 _OutputHelper.Write (buffer, index, count);
206 if (!_Response.BufferOutput) {
212 public void WriteBytes (byte[] buffer, int index, int count)
214 _OutputStream.Write (buffer, index, count);
215 if (!_Response.BufferOutput) {
221 public override void WriteLine ()
223 _OutputHelper.Write ("\r\n");
224 if (!_Response.BufferOutput) {
230 public void WriteString (string s, int index, int count)
232 _OutputHelper.Write (s.Substring (index, count));
233 if (!_Response.BufferOutput) {