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