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