KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > coyote > tomcat5 > CoyoteWriter


1
2
3 /*
4  * The contents of this file are subject to the terms
5  * of the Common Development and Distribution License
6  * (the "License"). You may not use this file except
7  * in compliance with the License.
8  *
9  * You can obtain a copy of the license at
10  * glassfish/bootstrap/legal/CDDLv1.0.txt or
11  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
12  * See the License for the specific language governing
13  * permissions and limitations under the License.
14  *
15  * When distributing Covered Code, include this CDDL
16  * HEADER in each file and include the License file at
17  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
18  * add the following below this CDDL HEADER, with the
19  * fields enclosed by brackets "[]" replaced with your
20  * own identifying information: Portions Copyright [yyyy]
21  * [name of copyright owner]
22  *
23  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24  *
25  * Portions Copyright Apache Software Foundation.
26  */

27
28
29 package org.apache.coyote.tomcat5;
30
31 import java.io.IOException JavaDoc;
32 import java.io.PrintWriter JavaDoc;
33
34 import javax.servlet.ServletOutputStream JavaDoc;
35
36 /**
37  * Coyote implementation of the servlet writer.
38  *
39  * @author Remy Maucherat
40  */

41 public class CoyoteWriter
42     extends PrintWriter JavaDoc {
43
44
45     // -------------------------------------------------------------- Constants
46

47
48     private static final char[] LINE_SEP = { '\r', '\n' };
49
50
51     // ----------------------------------------------------- Instance Variables
52

53
54     protected OutputBuffer ob;
55     protected boolean error = false;
56
57
58     // ----------------------------------------------------------- Constructors
59

60
61     public CoyoteWriter(OutputBuffer ob) {
62         super(ob);
63         this.ob = ob;
64     }
65
66
67     // --------------------------------------------------------- Public Methods
68

69
70     /**
71     * Prevent cloning the facade.
72     */

73     protected Object JavaDoc clone()
74         throws CloneNotSupportedException JavaDoc {
75         throw new CloneNotSupportedException JavaDoc();
76     }
77     
78     
79     // -------------------------------------------------------- Package Methods
80

81
82     /**
83      * Clear facade.
84      */

85     void clear() {
86         ob = null;
87     }
88
89     /**
90      * Recycle.
91      */

92     void recycle() {
93         error = false;
94     }
95
96
97     // --------------------------------------------------------- Writer Methods
98

99
100     public void flush() {
101
102         if (error)
103             return;
104
105         try {
106             ob.flush();
107         } catch (IOException JavaDoc e) {
108             error = true;
109         }
110
111     }
112
113
114     public void close() {
115
116         // We don't close the PrintWriter - super() is not called,
117
// so the stream can be reused. We close ob.
118
try {
119             ob.close();
120         } catch (IOException JavaDoc ex ) {
121             ;
122         }
123         error = false;
124
125     }
126
127
128     public boolean checkError() {
129         flush();
130         return error;
131     }
132
133
134     public void write(int c) {
135
136         if (error)
137             return;
138
139         try {
140             ob.write(c);
141         } catch (IOException JavaDoc e) {
142             error = true;
143         }
144
145     }
146
147
148     public void write(char buf[], int off, int len) {
149
150         if (error)
151             return;
152
153         try {
154             ob.write(buf, off, len);
155         } catch (IOException JavaDoc e) {
156             error = true;
157         }
158
159     }
160
161
162     public void write(char buf[]) {
163     write(buf, 0, buf.length);
164     }
165
166
167     public void write(String JavaDoc s, int off, int len) {
168
169         if (error)
170             return;
171
172         try {
173             ob.write(s, off, len);
174         } catch (IOException JavaDoc e) {
175             error = true;
176         }
177
178     }
179
180
181     public void write(String JavaDoc s) {
182         write(s, 0, s.length());
183     }
184
185
186     // ---------------------------------------------------- PrintWriter Methods
187

188
189     public void print(boolean b) {
190         if (b) {
191             write("true");
192         } else {
193             write("false");
194         }
195     }
196
197
198     public void print(char c) {
199         write(c);
200     }
201
202
203     public void print(int i) {
204         write(String.valueOf(i));
205     }
206
207
208     public void print(long l) {
209         write(String.valueOf(l));
210     }
211
212
213     public void print(float f) {
214         write(String.valueOf(f));
215     }
216
217
218     public void print(double d) {
219         write(String.valueOf(d));
220     }
221
222
223     public void print(char s[]) {
224         write(s);
225     }
226
227
228     public void print(String JavaDoc s) {
229         if (s == null) {
230             s = "null";
231         }
232         write(s);
233     }
234
235
236     public void print(Object JavaDoc obj) {
237         write(String.valueOf(obj));
238     }
239
240
241     public void println() {
242         write(LINE_SEP);
243     }
244
245
246     public void println(boolean b) {
247         print(b);
248         println();
249     }
250
251
252     public void println(char c) {
253         print(c);
254         println();
255     }
256
257
258     public void println(int i) {
259         print(i);
260         println();
261     }
262
263
264     public void println(long l) {
265         print(l);
266         println();
267     }
268
269
270     public void println(float f) {
271         print(f);
272         println();
273     }
274
275
276     public void println(double d) {
277         print(d);
278         println();
279     }
280
281
282     public void println(char c[]) {
283         print(c);
284         println();
285     }
286
287
288     public void println(String JavaDoc s) {
289         print(s);
290         println();
291     }
292
293
294     public void println(Object JavaDoc o) {
295         print(o);
296         println();
297     }
298
299
300 }
301
Popular Tags