2 // System.Web.HttpWriter.cs
6 // Miguel de Icaza (miguel@novell.com)
9 // Copyright (C) 2005 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;
37 namespace System.Web {
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;
45 byte [] _bytebuffer = new byte [1024];
47 internal HttpWriter (HttpResponse response)
49 this.response = response;
50 encoding = response.ContentEncoding;
51 output_stream = response.output_stream;
54 byte [] GetByteBuffer (int length)
56 // We will reuse the buffer if its size is < 32K
57 if (_bytebuffer.Length >= length)
60 if (length > 32 * 1024)
61 return new byte [length];
63 _bytebuffer = new byte [length];
67 public override Encoding Encoding {
73 internal void SetEncoding (Encoding new_encoding)
75 encoding = new_encoding;
78 public Stream OutputStream {
84 internal HttpResponse Response {
85 get { return response; }
88 // Flush data, and closes socket
90 public override void Close ()
92 output_stream.Close ();
95 public override void Flush ()
97 output_stream.Flush ();
100 char [] chars = new char [1];
101 public override void Write (char ch)
107 public override void Write (object obj)
112 Write (obj.ToString ());
115 public override void Write (string s)
118 WriteString (s, 0, s.Length);
121 public override void Write (char [] buffer, int index, int count)
123 if (buffer == null || index < 0 || count < 0 || (buffer.Length - index) < count)
124 throw new ArgumentOutOfRangeException ();
126 output_stream.Write (buffer, index, count);
128 int length = encoding.GetMaxByteCount (count);
129 byte [] bytebuffer = GetByteBuffer (length);
130 int realLength = encoding.GetBytes (buffer, index, count, bytebuffer, 0);
131 output_stream.Write (bytebuffer, 0, realLength);
139 static char [] newline = new char [2] { '\r', '\n' };
141 public override void WriteLine ()
143 Write (newline, 0, 2);
146 public void WriteString (string s, int index, int count)
151 if (index < 0 || count < 0 || ((index + count > s.Length)))
152 throw new ArgumentOutOfRangeException ();
154 output_stream.Write (s, index, count);
156 int length = encoding.GetMaxByteCount (count);
157 byte [] bytebuffer = GetByteBuffer (length);
158 int realLength = encoding.GetBytes (s, index, count, bytebuffer, 0);
159 output_stream.Write (bytebuffer, 0, realLength);
167 internal void WriteUTF8Ptr (IntPtr ptr, int length)
169 output_stream.WritePtr (ptr, length);
172 public void WriteBytes (byte [] buffer, int index, int count)
174 output_stream.Write (buffer, index, count);