New test.
[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                 byte [] _bytebuffer = new byte [1024];
46
47                 internal HttpWriter (HttpResponse response)
48                 {
49                         this.response = response;
50                         encoding = response.ContentEncoding;
51                         output_stream = response.output_stream;
52                 }
53
54                 byte [] GetByteBuffer (int length)
55                 {
56                         // We will reuse the buffer if its size is < 32K
57                         if (_bytebuffer.Length >= length)
58                                 return _bytebuffer;
59
60                         if (length > 32 * 1024)
61                                 return new byte [length];
62
63                         _bytebuffer = new byte [length];
64                         return _bytebuffer;
65                 }
66
67                 public override Encoding Encoding {
68                         get {
69                                 return encoding;
70                         }
71                 }
72
73                 internal void SetEncoding (Encoding new_encoding)
74                 {
75                         encoding = new_encoding;
76                 }
77
78                 public Stream OutputStream {
79                         get {
80                                 return output_stream;
81                         }
82                 }
83
84                 //
85                 // Flush data, and closes socket
86                 //
87                 public override void Close ()
88                 {
89                         output_stream.Close ();
90                 }
91
92                 public override void Flush ()
93                 {
94                         output_stream.Flush ();
95                 }
96
97                 char [] chars = new char [1];
98                 public override void Write (char ch)
99                 {
100                         chars [0] = ch;
101                         Write (chars, 0, 1);
102                 }
103
104                 public override void Write (object obj)
105                 {
106                         if (obj == null)
107                                 return;
108                         
109                         Write (obj.ToString ());
110                 }
111                 
112                 public override void Write (string s)
113                 {
114                         if (s != null)
115                                 WriteString (s, 0, s.Length);
116                 }
117                 
118                 public override void Write (char [] buffer, int index, int count)
119                 {
120                         if (buffer == null || index < 0 || count < 0 || (buffer.Length - index) < count)
121                                 throw new ArgumentOutOfRangeException ();
122                         
123                         int length = encoding.GetByteCount (buffer, index, count);
124                         byte [] bytebuffer = GetByteBuffer (length);
125                         encoding.GetBytes (buffer, index, count, bytebuffer, 0);
126                         output_stream.Write (bytebuffer, 0, length);
127                         if (response.buffer)
128                                 return;
129
130                         response.Flush ();
131                 }
132
133                 static char [] newline = new char [2] { '\r', '\n' };
134                 
135                 public override void WriteLine ()
136                 {
137                         Write (newline, 0, 2);
138                 }
139
140                 public void WriteString (string s, int index, int count)
141                 {
142                         if (s == null)
143                                 return;
144
145                         if (index < 0 || count < 0 || ((index + count > s.Length)))
146                                 throw new ArgumentOutOfRangeException ();
147                         
148                         int length;
149                         if (index == 0 && count == s.Length) {
150                                 length = encoding.GetByteCount (s); 
151                         } else {
152                                 char [] chars = s.ToCharArray (index, count);
153                                 length = encoding.GetByteCount (chars);
154                         }
155
156                         byte [] bytebuffer = GetByteBuffer (length);
157                         encoding.GetBytes (s, index, count, bytebuffer, 0);
158                         output_stream.Write (bytebuffer, 0, length);
159                         if (response.buffer)
160                                 return;
161
162                         response.Flush ();
163                 }
164
165                 public void WriteBytes (byte [] buffer, int index, int count)
166                 {
167                         output_stream.Write (buffer, index, count);
168
169                         if (response.buffer)
170                                 return;
171
172                         response.Flush ();
173                 }
174         }
175 }
176