2 // System.Web.HttpWriter.cs
6 // Miguel de Icaza (miguel@novell.com)
9 // Copyright (C) 2005-2009 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
33 using System.Globalization;
34 using System.Runtime.InteropServices;
35 using System.Security.Permissions;
39 // CAS - no InheritanceDemand here as the class is sealed
40 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41 public sealed class HttpWriter : TextWriter
43 HttpResponseStream output_stream;
44 HttpResponse response;
46 byte [] _bytebuffer = new byte [1024];
48 internal HttpWriter (HttpResponse response)
50 this.response = response;
51 encoding = response.ContentEncoding;
52 output_stream = response.output_stream;
55 byte [] GetByteBuffer (int length)
57 // We will reuse the buffer if its size is < 32K
58 if (_bytebuffer.Length >= length)
61 if (length > 32 * 1024)
62 return new byte [length];
64 _bytebuffer = new byte [length];
68 public override Encoding Encoding {
74 internal void SetEncoding (Encoding new_encoding)
76 encoding = new_encoding;
79 public Stream OutputStream {
85 internal HttpResponse Response {
86 get { return response; }
89 // Flush data, and closes socket
91 public override void Close ()
93 output_stream.Close ();
96 public override void Flush ()
98 output_stream.Flush ();
101 char [] chars = new char [1];
102 public override void Write (char ch)
108 public override void Write (object obj)
113 Write (obj.ToString ());
116 public override void Write (string s)
119 WriteString (s, 0, s.Length);
122 public override void Write (char [] buffer, int index, int count)
124 if (buffer == null || index < 0 || count < 0 || (buffer.Length - index) < count)
125 throw new ArgumentOutOfRangeException ();
127 output_stream.Write (buffer, index, count);
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);
140 static char [] newline = new char [2] { '\r', '\n' };
142 public override void WriteLine ()
144 Write (newline, 0, 2);
147 public void WriteString (string s, int index, int count)
152 if (index < 0 || count < 0 || ((index + count > s.Length)))
153 throw new ArgumentOutOfRangeException ();
155 output_stream.Write (s, index, count);
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);
168 internal void WriteUTF8Ptr (IntPtr ptr, int length)
170 output_stream.WritePtr (ptr, length);
173 public void WriteBytes (byte [] buffer, int index, int count)
175 output_stream.Write (buffer, index, count);