merge from trunk revisions 58933, 58935, 58936
[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                 public override void Write (char ch)
84                 {
85                         Write (new string (ch, 1));
86                 }
87
88                 public override void Write (object obj)
89                 {
90                         if (obj == null)
91                                 return;
92                         
93                         Write (obj.ToString ());
94                 }
95                 
96                 public override void Write (string s)
97                 {
98                         if (s == null)
99                                 return;
100                         
101                         byte [] xx = encoding.GetBytes (s);
102
103                         output_stream.Write (xx, 0, xx.Length);
104                         
105                         if (response.buffer)
106                                 return;
107
108                         response.Flush ();
109                 }
110                 
111                 public override void Write (char [] buffer, int index, int count)
112                 {
113                         byte [] xx = encoding.GetBytes (buffer, index, count);
114                         output_stream.Write (xx, 0, xx.Length);
115
116                         if (response.buffer)
117                                 return;
118
119                         response.Flush ();
120                 }
121
122                 static byte [] newline = new byte [2] { 13, 10 };
123                 
124                 public override void WriteLine ()
125                 {
126                         output_stream.Write (newline, 0, 2);
127                         
128                         if (response.buffer)
129                                 return;
130
131                         response.Flush ();
132                 }
133
134                 public void WriteString (string s, int index, int count)
135                 {
136                         char [] a = s.ToCharArray (index, count);
137
138                         byte [] xx = encoding.GetBytes (a, 0, count);
139                         
140                         output_stream.Write (xx, 0, xx.Length);
141
142                         if (response.buffer)
143                                 return;
144
145                         response.Flush ();
146                 }
147
148                 public void WriteBytes (byte [] buffer, int index, int count)
149                 {
150                         output_stream.Write (buffer, index, count);
151
152                         if (response.buffer)
153                                 return;
154
155                         response.Flush ();
156                 }
157         }
158 }
159