2006-02-01 Chris Toshok <toshok@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web / HttpWriter.cs
1 //
2 // System.Web.HttpWriter.cs 
3 //
4 // 
5 // Author:
6 //      Miguel de Icaza (miguel@novell.com)
7 //
8 //
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.IO;
32 using System.Text;
33 using System.Globalization;
34 using System.Runtime.InteropServices;
35 using System.Security.Permissions;
36         
37 namespace System.Web {
38         
39         // CAS - no InheritanceDemand here as the class is sealed
40         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41         public sealed class HttpWriter : TextWriter {
42                 HttpResponseStream output_stream;
43                 HttpResponse response;
44                 Encoding encoding;
45
46                 internal HttpWriter (HttpResponse response)
47                 {
48                         this.response = response;
49                         encoding = response.ContentEncoding;
50                         output_stream = response.output_stream;
51                 }
52
53                 public override Encoding Encoding {
54                         get {
55                                 return encoding;
56                         }
57                 }
58
59                 internal void SetEncoding (Encoding new_encoding)
60                 {
61                         encoding = new_encoding;
62                 }
63
64                 public Stream OutputStream {
65                         get {
66                                 return output_stream;
67                         }
68                 }
69
70                 //
71                 // Flush data, and closes socket
72                 //
73                 public override void Close ()
74                 {
75                         output_stream.Close ();
76                 }
77
78                 public override void Flush ()
79                 {
80                         output_stream.Flush ();
81                 }
82
83                 char [] chars = new char [1];
84                 public override void Write (char ch)
85                 {
86                         chars [0] = ch;
87                         Write (chars, 0, 1);
88                 }
89
90                 public override void Write (object obj)
91                 {
92                         if (obj == null)
93                                 return;
94                         
95                         Write (obj.ToString ());
96                 }
97                 
98                 public override void Write (string s)
99                 {
100                         if (s == null)
101                                 return;
102                         
103                         byte [] xx = encoding.GetBytes (s);
104
105                         output_stream.Write (xx, 0, xx.Length);
106                         
107                         if (response.buffer)
108                                 return;
109
110                         response.Flush ();
111                 }
112                 
113                 public override void Write (char [] buffer, int index, int count)
114                 {
115                         byte [] xx = encoding.GetBytes (buffer, index, count);
116                         output_stream.Write (xx, 0, xx.Length);
117
118                         if (response.buffer)
119                                 return;
120
121                         response.Flush ();
122                 }
123
124                 static byte [] newline = new byte [2] { 13, 10 };
125                 
126                 public override void WriteLine ()
127                 {
128                         output_stream.Write (newline, 0, 2);
129                         
130                         if (response.buffer)
131                                 return;
132
133                         response.Flush ();
134                 }
135
136                 public void WriteString (string s, int index, int count)
137                 {
138                         char [] a = s.ToCharArray (index, count);
139
140                         byte [] xx = encoding.GetBytes (a, 0, count);
141                         
142                         output_stream.Write (xx, 0, xx.Length);
143
144                         if (response.buffer)
145                                 return;
146
147                         response.Flush ();
148                 }
149
150                 public void WriteBytes (byte [] buffer, int index, int count)
151                 {
152                         output_stream.Write (buffer, index, count);
153
154                         if (response.buffer)
155                                 return;
156
157                         response.Flush ();
158                 }
159         }
160 }
161