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