2 // Mainsoft.Web.Hosting.OutputStreamWrapper
\r
5 // Vladimir Krasnov (vladimirk@mainsoft.com)
\r
10 // Permission is hereby granted, free of charge, to any person obtaining
\r
11 // a copy of this software and associated documentation files (the
\r
12 // "Software"), to deal in the Software without restriction, including
\r
13 // without limitation the rights to use, copy, modify, merge, publish,
\r
14 // distribute, sublicense, and/or sell copies of the Software, and to
\r
15 // permit persons to whom the Software is furnished to do so, subject to
\r
16 // the following conditions:
\r
18 // The above copyright notice and this permission notice shall be
\r
19 // included in all copies or substantial portions of the Software.
\r
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
\r
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
\r
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
\r
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
\r
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
\r
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
\r
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
32 using System.Collections.Generic;
\r
39 namespace Mainsoft.Web.Hosting
\r
44 public sealed class OutputStreamWrapper : java.io.Writer
\r
52 readonly PrintWriter _printWriter;
\r
53 readonly OutputStream _outputStream;
\r
54 readonly OutputMode _outputMode;
\r
56 private byte [] _bytebuffer;
\r
57 private char [] _charBuffer;
\r
58 //sbyte [] _oneByte = new sbyte [1];
\r
60 Encoding _encoding = null;
\r
63 /// Ctor from writer
\r
65 /// <param name="writer"></param>
\r
66 public OutputStreamWrapper (PrintWriter writer)
\r
68 _printWriter = writer;
\r
69 _outputMode = OutputMode.CharWriter;
\r
73 /// Ctor from stream
\r
75 /// <param name="outputStream"></param>
\r
76 public OutputStreamWrapper (OutputStream outputStream)
\r
78 _outputStream = outputStream;
\r
79 _outputMode = OutputMode.ByteStream;
\r
85 public override void close ()
\r
88 // on WPS, closing the writer causes
\r
89 // any portlets rendered after us to be not shown...
\r
94 /// Flushes servers stream
\r
96 public override void flush ()
\r
98 if (_outputMode == OutputMode.CharWriter)
\r
99 _printWriter.flush ();
\r
101 _outputStream.flush();
\r
105 /// Writes char array
\r
107 /// <param name="chars"></param>
\r
108 /// <param name="index"></param>
\r
109 /// <param name="count"></param>
\r
110 public override void write (char [] chars, int index, int count)
\r
112 if (_outputMode == OutputMode.CharWriter) {
\r
113 _printWriter.write (chars, index, count);
\r
116 int length = Encoding.GetMaxByteCount (count);
\r
117 byte [] bytebuffer = GetByteBuffer (length);
\r
118 int realLength = Encoding.GetBytes (chars, index, count, bytebuffer, 0);
\r
120 _outputStream.write (TypeUtils.ToSByteArray (bytebuffer), 0, realLength);
\r
126 /// Writes byte array
\r
128 /// <param name="sbytes"></param>
\r
129 /// <param name="index"></param>
\r
130 /// <param name="count"></param>
\r
131 public void write (sbyte [] sbytes, int index, int count)
\r
133 if (_outputMode == OutputMode.ByteStream)
\r
134 _outputStream.write (sbytes, index, count);
\r
136 int length = Encoding.GetMaxCharCount (count);
\r
137 char [] charbuffer = GetCharBuffer (length);
\r
138 int realLength = Encoding.GetChars ((byte []) TypeUtils.ToByteArray (sbytes), index, count, charbuffer, 0);
\r
140 _printWriter.write (charbuffer, 0, realLength);
\r
144 private char [] GetCharBuffer (int length)
\r
146 // We will reuse the buffer if its size is < 32K
\r
147 if (_charBuffer != null && _charBuffer.Length >= length)
\r
148 return _charBuffer;
\r
150 if (length > 32 * 1024)
\r
151 return new char [length];
\r
156 _charBuffer = new char [length];
\r
157 return _charBuffer;
\r
160 private byte [] GetByteBuffer (int length)
\r
162 // We will reuse the buffer if its size is < 32K
\r
163 if (_bytebuffer != null && _bytebuffer.Length >= length)
\r
164 return _bytebuffer;
\r
166 if (length > 32 * 1024)
\r
167 return new byte [length];
\r
172 _bytebuffer = new byte [length];
\r
173 return _bytebuffer;
\r
176 private Encoding Encoding
\r
180 if (_encoding == null) {
\r
181 HttpContext current = HttpContext.Current;
\r
182 _encoding = (current != null) ? current.Response.ContentEncoding : Encoding.UTF8;
\r