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 {
85 // Flush data, and closes socket
87 public override void Close ()
89 output_stream.Close ();
92 public override void Flush ()
94 output_stream.Flush ();
97 char [] chars = new char [1];
98 public override void Write (char ch)
104 public override void Write (object obj)
109 Write (obj.ToString ());
112 public override void Write (string s)
115 WriteString (s, 0, s.Length);
118 public override void Write (char [] buffer, int index, int count)
120 if (buffer == null || index < 0 || count < 0 || (buffer.Length - index) < count)
121 throw new ArgumentOutOfRangeException ();
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);
133 static char [] newline = new char [2] { '\r', '\n' };
135 public override void WriteLine ()
137 Write (newline, 0, 2);
140 public void WriteString (string s, int index, int count)
145 if (index < 0 || count < 0 || ((index + count > s.Length)))
146 throw new ArgumentOutOfRangeException ();
149 if (index == 0 && count == s.Length) {
150 length = encoding.GetByteCount (s);
152 char [] chars = s.ToCharArray (index, count);
153 length = encoding.GetByteCount (chars);
156 byte [] bytebuffer = GetByteBuffer (length);
157 encoding.GetBytes (s, index, count, bytebuffer, 0);
158 output_stream.Write (bytebuffer, 0, length);
165 public void WriteBytes (byte [] buffer, int index, int count)
167 output_stream.Write (buffer, index, count);