style
[mono.git] / mcs / class / System.Web / System.Web / HttpWriter.cs
1 // 
2 // System.Web.HttpWriter
3 //
4 // Author:
5 //   Patrik Torstensson (Patrik.Torstensson@labs2.com)
6 //
7 using System;
8 using System.IO;
9 using System.Text;
10
11 namespace System.Web
12 {
13         public sealed class HttpWriter : TextWriter
14         {
15                 HttpResponse _Response;
16
17                 HttpResponseStream _ResponseStream;
18
19                 MemoryStream _OutputStream;
20                 StreamWriter _OutputHelper;
21                 Encoder _Encoder;
22                 Encoding _Encoding;     
23
24                 Stream _OutputFilter;
25                 HttpResponseStreamProxy _OutputProxy;
26
27                 internal HttpWriter (HttpResponse Response)
28                 {
29                         _Response = Response;
30
31                         _OutputStream = new MemoryStream (32768);
32                         _OutputHelper = new StreamWriter (_OutputStream, Encoding.Unicode);
33                         _ResponseStream = new HttpResponseStream (this);
34
35                         Update ();
36                 }  
37
38                 internal void Dispose ()
39                 {
40                         _OutputHelper.Close ();
41                         _OutputStream.Close ();
42                         _OutputFilter.Close ();
43                 }
44
45                 internal Stream GetActiveFilter()
46                 {
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);
51
52                                 return _OutputProxy;
53                         }
54                         return _OutputFilter;
55                 }
56
57                 internal void ActivateFilter (Stream OutputFilter)
58                 {
59                         if (null == _OutputProxy)
60                                 throw new HttpException ("Invalid filter usage");
61
62                         _OutputFilter = OutputFilter;
63                 }
64
65                 internal void FilterData (bool CloseStream)
66                 {
67                         // Check if we have any filter at all
68                         if (null == _OutputFilter)
69                                 return;
70
71                         FlushBuffers ();
72
73                         // Save our current data
74                         byte [] arrData = _OutputStream.ToArray ();
75
76                         // Remove our internal data
77                         Clear ();
78
79                         // If we have a filter then we have a proxy
80                         _OutputProxy.Active = true;
81
82                         try {
83                                 // Call the filter (it does a callback into our HttpWriter again)
84                                 _OutputFilter.Write (arrData, 0, arrData.Length);
85
86                                 if (CloseStream)
87                                         _OutputFilter.Close ();
88                         } finally {
89                                 _OutputProxy.Active = false;
90                         }
91                 }
92
93                 internal void Clear ()
94                 {
95                         _OutputHelper.Close ();
96                         _OutputStream.Close ();
97                                         
98                         // Quick way of doing cleanup
99                         _OutputStream = new MemoryStream (32768);
100                         _OutputHelper = new StreamWriter (_OutputStream, Encoding.Unicode);
101                 }
102
103                 internal byte [] ToArray ()
104                 {
105                         if (_OutputStream.Length == 0)
106                                 return null;
107
108                         // TODO: Optimize
109                         byte [] arrStream = _OutputStream.ToArray ();
110                         byte [] arrData = new byte [arrStream.Length - 2];
111
112                         Buffer.BlockCopy (arrStream, 2, arrData, 0, arrStream.Length - 2);
113
114                         byte [] arrContent = _Encoding.GetBytes (Encoding.Unicode.GetString (arrData));
115
116                         return arrContent;
117                 }
118
119                 internal void SendContent (HttpWorkerRequest Handler)
120                 {
121                         FlushBuffers();
122
123                         if (_OutputStream.Length > 0) {
124                                 byte [] arrContent = ToArray ();
125                                 Handler.SendResponseFromMemory (arrContent, arrContent.Length);
126                         }
127                 }
128
129                 internal void Update ()
130                 {
131                         _Encoder = _Response.ContentEncoder;
132                         _Encoding = _Response.ContentEncoding;
133                 }
134
135                 internal long BufferSize
136                 {
137                         get {
138                                 FlushBuffers ();
139
140                                 if (_OutputStream.Length == 0)
141                                         return 0;
142
143                                 return ToArray ().Length;
144                         }
145                 }
146
147                 internal void FlushBuffers ()
148                 {
149                         _OutputHelper.Flush ();
150                         _OutputStream.Flush ();
151                 }
152
153                 public override Encoding Encoding
154                 {
155                         get { return _Encoding; }
156                 }
157
158                 public Stream OutputStream
159                 {
160                         get { return _ResponseStream; }
161                 }
162
163                 public override void Close ()
164                 {
165                         FlushBuffers ();
166                         _Response.Flush ();
167                         _Response.Close ();
168                 }
169
170                 public override void Flush ()
171                 {
172                         FlushBuffers ();
173                         _Response.Flush ();
174                 }
175
176                 public override void Write (char ch)
177                 {
178                         _OutputHelper.Write (ch);
179                         if (!_Response.BufferOutput) {
180                                 FlushBuffers ();
181                                 _Response.Flush ();
182                         }
183                 }
184
185                 public override void Write (object obj)
186                 {
187                         _OutputHelper.Write (obj.ToString ());
188                         if (!_Response.BufferOutput) {
189                                 FlushBuffers ();
190                                 _Response.Flush ();
191                         }
192                 }
193
194                 public override void Write (string s)
195                 {
196                         _OutputHelper.Write (s);
197                         if (!_Response.BufferOutput) {
198                                 FlushBuffers ();
199                                 _Response.Flush ();
200                         }
201                 }
202
203                 public override void Write (char [] buffer, int index, int count)
204                 {
205                         _OutputHelper.Write (buffer, index, count);
206                         if (!_Response.BufferOutput) {
207                                 FlushBuffers ();
208                                 _Response.Flush ();
209                         }
210                 }
211
212                 public void WriteBytes (byte[] buffer, int index, int count)
213                 {
214                         _OutputStream.Write (buffer, index, count);
215                         if (!_Response.BufferOutput) {
216                                 FlushBuffers ();
217                                 _Response.Flush ();
218                         }
219                 }
220
221                 public override void WriteLine ()
222                 {
223                         _OutputHelper.Write ("\r\n");
224                         if (!_Response.BufferOutput) {
225                                 FlushBuffers ();
226                                 _Response.Flush ();
227                         }
228                 }
229
230                 public void WriteString (string s, int index, int count)
231                 {
232                         _OutputHelper.Write (s.Substring (index, count));
233                         if (!_Response.BufferOutput) {
234                                 FlushBuffers ();
235                                 _Response.Flush ();
236                         }
237                 }
238         }
239 }
240