1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.portals.gems.util;
23
24 import java.io.IOException;
25 import java.io.PrintWriter;
26 import java.io.UnsupportedEncodingException;
27
28 import javax.servlet.ServletOutputStream;
29
30 /***
31 * This is a specialized class implementing a ServletOutputStream that works in
32 * conjunction with a PrintWriter to send data to the browser. It is used when
33 * a J2EE server throws an IllegalStateException when you call getOutputStream
34 * on a response which someone has previously called getWriter on.
35 */
36 public class PrintWriterServletOutputStream extends ServletOutputStream
37 {
38
39 /***
40 * The PrintWriter that is wrapped on top of the base input stream
41 */
42 PrintWriter mPrintWriter;
43
44 /***
45 * The character encoding of the response.
46 */
47 private String characterEncoding;
48
49 /***
50 * @deprecated since 1.0RC3; use PrintWriterServletOutputStream
51 * <p>
52 * Construct a ServletOutputStream that coordinates output using a base
53 * ServletOutputStream and a PrintWriter that is wrapped on top of that
54 * OutputStream.
55 * </p>
56 */
57 public PrintWriterServletOutputStream(PrintWriter pO)
58 {
59 this(pO, null);
60 }
61
62 public PrintWriterServletOutputStream(PrintWriter pw, String encoding)
63 {
64 super();
65 mPrintWriter = pw;
66 characterEncoding = encoding;
67 }
68
69
70 /***
71 * Writes a single byte to the output stream
72 * This implementation writes the byte to the
73 * underlying PrintWriter.
74 */
75 public void write(int pVal) throws IOException
76 {
77 mPrintWriter.write(pVal);
78 }
79
80 /***
81 * Writes an array of bytes
82 *
83 * @param pBuf the array to be written
84 * @exception IOException if an I/O error occurred
85 */
86 public void write(byte[] pBuf) throws IOException
87 {
88 this.write(pBuf, 0, pBuf.length);
89 }
90
91 /***
92 * Writes a subarray of bytes
93 * This implementation redirects it's input into the
94 * underlying PrintWriter.
95 *
96 * @param pBuf the array to be written
97 * @param pOffset the offset into the array
98 * @param pLength the number of bytes to write
99 * @exception IOException if an I/O error occurred
100 */
101 public void write(byte[] pBuf, int pOffset, int pLength) throws IOException
102 {
103 String strValue = null;
104 if(characterEncoding != null && !"".equals(characterEncoding)) {
105 try {
106 strValue = new String(pBuf, pOffset, pLength, characterEncoding);
107 }
108 catch(UnsupportedEncodingException uee) {
109
110 }
111 }
112
113 if(strValue == null) {
114 strValue = new String(pBuf, pOffset, pLength);
115 }
116
117 mPrintWriter.write(strValue);
118 }
119
120 /***
121 * Flushes the stream, writing any buffered output bytes
122 *
123 * @exception IOException if an I/O error occurred
124 */
125 public void flush() throws IOException
126 {
127 mPrintWriter.flush();
128 }
129
130 /***
131 * Closes the stream
132 *
133 * @exception IOException if an I/O error occurred
134 */
135 public void close() throws IOException
136 {
137 mPrintWriter.close();
138 }
139
140 /***
141 *
142 * Prints a string.
143 *
144 * @param pVal the String to be printed
145 * @exception IOException if an I/O error has occurred
146 */
147 public void print(String pVal) throws IOException
148 {
149 mPrintWriter.print(pVal);
150 }
151
152 /***
153 *
154 * Prints an string followed by a CRLF.
155 *
156 * @param pVal the String to be printed
157 * @exception IOException if an I/O error has occurred
158 */
159 public void println(String pVal) throws IOException
160 {
161 mPrintWriter.println(pVal);
162 }
163
164 /***
165 *
166 * Prints a CRLF
167 *
168 * @exception IOException if an I/O error has occurred
169 *
170 */
171 public void println() throws IOException
172 {
173 mPrintWriter.println();
174 }
175
176 }