KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > pluto > util > PrintWriterServletOutputStream


1
2 /*
3  * Copyright 2003,2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 /*
18
19  */

20
21 package org.apache.pluto.util;
22
23 import java.io.IOException JavaDoc;
24 import java.io.PrintWriter JavaDoc;
25 import java.io.UnsupportedEncodingException JavaDoc;
26
27 import javax.servlet.ServletOutputStream JavaDoc;
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 JavaDoc
36 {
37
38   /**
39    * The PrintWriter that is wrapped on top of the base input stream
40    */

41   PrintWriter JavaDoc mPrintWriter;
42
43   /**
44    * The character encoding of the response.
45    */

46   private String JavaDoc 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 JavaDoc pO)
57   {
58       this(pO, null);
59   }
60
61   public PrintWriterServletOutputStream(PrintWriter JavaDoc pw, String JavaDoc 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 JavaDoc
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 JavaDoc
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 JavaDoc
101   {
102     String JavaDoc strValue = null;
103     if(characterEncoding != null && !"".equals(characterEncoding)) {
104         try {
105             strValue = new String JavaDoc(pBuf, pOffset, pLength, characterEncoding);
106         }
107         catch(UnsupportedEncodingException JavaDoc uee) {
108             // ignore and allow the null to handle.
109
}
110     }
111
112     if(strValue == null) {
113         strValue = new String JavaDoc(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 JavaDoc
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 JavaDoc
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 JavaDoc pVal) throws IOException JavaDoc
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 JavaDoc pVal) throws IOException JavaDoc
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 JavaDoc
171   {
172     mPrintWriter.println();
173   }
174
175 }
176
Popular Tags