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
26 import javax.servlet.ServletOutputStream;
27
28 /***
29 * This is a specialized class implementing a ServletOutputStream that works in
30 * conjunction with a PrintWriter to send data to the browser. It is used when
31 * a J2EE server throws an IllegalStateException when you call getOutputStream
32 * on a response which someone has previously called getWriter on.
33 */
34 public class PrintWriterServletOutputStream extends ServletOutputStream
35 {
36
37 /***
38 * The PrintWriter that is wrapped on top of the base input stream
39 */
40 PrintWriter mPrintWriter;
41
42 /***
43 * Construct a ServletOutputStream that coordinates output using a base
44 * ServletOutputStream and a PrintWriter that is wrapped on top of that
45 * OutputStream.
46 */
47 public PrintWriterServletOutputStream(PrintWriter pO)
48 {
49 super();
50 mPrintWriter = pO;
51 }
52
53 /***
54 * Writes an array of bytes
55 *
56 * @param pBuf the array to be written
57 * @exception IOException if an I/O error occurred
58 */
59 public void write(byte[] pBuf) throws IOException
60 {
61 char[] cbuf = new char[pBuf.length];
62 for (int i = 0; i < cbuf.length; i++)
63 cbuf[i] = (char)(pBuf[i] & 0xff);
64 mPrintWriter.write(cbuf, 0, pBuf.length);
65 }
66
67 /***
68 * Writes a single byte to the output stream
69 */
70 public void write(int pVal) throws IOException
71 {
72 mPrintWriter.write(pVal);
73 }
74
75 /***
76 * Writes a subarray of bytes
77 *
78 * @param pBuf the array to be written
79 * @param pOffset the offset into the array
80 * @param pLength the number of bytes to write
81 * @exception IOException if an I/O error occurred
82 */
83 public void write(byte[] pBuf, int pOffset, int pLength) throws IOException
84 {
85 char[] cbuf = new char[pLength];
86 for (int i = 0; i < pLength; i++)
87 cbuf[i] = (char)(pBuf[i + pOffset] & 0xff);
88 mPrintWriter.write(cbuf, 0, pLength);
89 }
90
91 /***
92 * Flushes the stream, writing any buffered output bytes
93 *
94 * @exception IOException if an I/O error occurred
95 */
96 public void flush() throws IOException
97 {
98 mPrintWriter.flush();
99 }
100
101 /***
102 * Closes the stream
103 *
104 * @exception IOException if an I/O error occurred
105 */
106 public void close() throws IOException
107 {
108 mPrintWriter.close();
109 }
110
111 /***
112 *
113 * Prints a string.
114 *
115 * @param pVal the String to be printed
116 * @exception IOException if an I/O error has occurred
117 */
118 public void print(String pVal) throws IOException
119 {
120 mPrintWriter.print(pVal);
121 }
122
123 /***
124 *
125 * Prints an string followed by a CRLF.
126 *
127 * @param pVal the String to be printed
128 * @exception IOException if an I/O error has occurred
129 */
130 public void println(String pVal) throws IOException
131 {
132 mPrintWriter.println(pVal);
133 }
134
135 /***
136 *
137 * Prints a CRLF
138 *
139 * @exception IOException if an I/O error has occurred
140 *
141 */
142 public void println() throws IOException
143 {
144 mPrintWriter.println();
145 }
146
147 }