1 /*
2 * Copyright 1999-2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.apache.coyote.tomcat4;
18
19 import java.io.IOException;
20 import java.io.PrintWriter;
21
22 /**
23 * Coyote implementation of the servlet writer.
24 *
25 * @author Remy Maucherat
26 */
27 final class CoyoteWriter
28 extends PrintWriter {
29
30
31 // -------------------------------------------------------------- Constants
32
33
34 private static final char[] LINE_SEP = { '\r', '\n' };
35
36
37 // ----------------------------------------------------- Instance Variables
38
39
40 protected OutputBuffer ob;
41 protected boolean error = false;
42
43
44 // ----------------------------------------------------------- Constructors
45
46
47 public CoyoteWriter(OutputBuffer ob) {
48 super(ob);
49 this.ob = ob;
50 }
51
52
53 // -------------------------------------------------------- Package Methods
54
55
56 /**
57 * Recycle.
58 */
59 void recycle() {
60 error = false;
61 }
62
63
64 // --------------------------------------------------------- Writer Methods
65
66
67 public void flush() {
68
69 if (error)
70 return;
71
72 try {
73 ob.flush();
74 } catch (IOException e) {
75 error = true;
76 }
77
78 }
79
80
81 public void close() {
82
83 // We don't close the PrintWriter - super() is not called,
84 // so the stream can be reused. We close ob.
85 try {
86 ob.close();
87 } catch (IOException ex ) {
88 ;
89 }
90 error = false;
91
92 }
93
94
95 public boolean checkError() {
96 flush();
97 return error;
98 }
99
100
101 public void write(int c) {
102
103 if (error)
104 return;
105
106 try {
107 ob.write(c);
108 } catch (IOException e) {
109 error = true;
110 }
111
112 }
113
114
115 public void write(char buf[], int off, int len) {
116
117 if (error)
118 return;
119
120 try {
121 ob.write(buf, off, len);
122 } catch (IOException e) {
123 error = true;
124 }
125
126 }
127
128
129 public void write(char buf[]) {
130 write(buf, 0, buf.length);
131 }
132
133
134 public void write(String s, int off, int len) {
135
136 if (error)
137 return;
138
139 try {
140 ob.write(s, off, len);
141 } catch (IOException e) {
142 error = true;
143 }
144
145 }
146
147
148 public void write(String s) {
149 write(s, 0, s.length());
150 }
151
152
153 // ---------------------------------------------------- PrintWriter Methods
154
155
156 public void print(boolean b) {
157 if (b) {
158 write("true");
159 } else {
160 write("false");
161 }
162 }
163
164
165 public void print(char c) {
166 write(c);
167 }
168
169
170 public void print(int i) {
171 write(String.valueOf(i));
172 }
173
174
175 public void print(long l) {
176 write(String.valueOf(l));
177 }
178
179
180 public void print(float f) {
181 write(String.valueOf(f));
182 }
183
184
185 public void print(double d) {
186 write(String.valueOf(d));
187 }
188
189
190 public void print(char s[]) {
191 write(s);
192 }
193
194
195 public void print(String s) {
196 if (s == null) {
197 s = "null";
198 }
199 write(s);
200 }
201
202
203 public void print(Object obj) {
204 write(String.valueOf(obj));
205 }
206
207
208 public void println() {
209 write(LINE_SEP);
210 }
211
212
213 public void println(boolean b) {
214 print(b);
215 println();
216 }
217
218
219 public void println(char c) {
220 print(c);
221 println();
222 }
223
224
225 public void println(int i) {
226 print(i);
227 println();
228 }
229
230
231 public void println(long l) {
232 print(l);
233 println();
234 }
235
236
237 public void println(float f) {
238 print(f);
239 println();
240 }
241
242
243 public void println(double d) {
244 print(d);
245 println();
246 }
247
248
249 public void println(char c[]) {
250 print(c);
251 println();
252 }
253
254
255 public void println(String s) {
256 print(s);
257 println();
258 }
259
260
261 public void println(Object o) {
262 print(o);
263 println();
264 }
265
266
267 }