1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  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 package org.apache.catalina.connector;
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 public class CoyoteWriter extends PrintWriter {
28
29
30     // -------------------------------------------------------------- Constants
31
32     private static final char[] LINE_SEP = System.lineSeparator().toCharArray();
33
34
35     // ----------------------------------------------------- Instance Variables
36
37
38     protected OutputBuffer ob;
39     protected boolean error = false;
40
41
42     // ----------------------------------------------------------- Constructors
43
44
45     public CoyoteWriter(OutputBuffer ob) {
46         super(ob);
47         this.ob = ob;
48     }
49
50
51     // --------------------------------------------------------- Public Methods
52
53
54     /**
55      * Prevent cloning the facade.
56      */

57     @Override
58     protected Object clone()
59         throws CloneNotSupportedException {
60         throw new CloneNotSupportedException();
61     }
62
63
64     // -------------------------------------------------------- Package Methods
65
66
67     /**
68      * Clear facade.
69      */

70     void clear() {
71         ob = null;
72     }
73
74
75     /**
76      * Recycle.
77      */

78     void recycle() {
79         error = false;
80     }
81
82
83     // --------------------------------------------------------- Writer Methods
84
85
86     @Override
87     public void flush() {
88
89         if (error) {
90             return;
91         }
92
93         try {
94             ob.flush();
95         } catch (IOException e) {
96             error = true;
97         }
98
99     }
100
101
102     @Override
103     public void close() {
104
105         // We don't close the PrintWriter - super() is not called,
106         // so the stream can be reused. We close ob.
107         try {
108             ob.close();
109         } catch (IOException ex ) {
110             // Ignore
111         }
112         error = false;
113
114     }
115
116
117     @Override
118     public boolean checkError() {
119         flush();
120         return error;
121     }
122
123
124     @Override
125     public void write(int c) {
126
127         if (error) {
128             return;
129         }
130
131         try {
132             ob.write(c);
133         } catch (IOException e) {
134             error = true;
135         }
136
137     }
138
139
140     @Override
141     public void write(char buf[], int off, int len) {
142
143         if (error) {
144             return;
145         }
146
147         try {
148             ob.write(buf, off, len);
149         } catch (IOException e) {
150             error = true;
151         }
152
153     }
154
155
156     @Override
157     public void write(char buf[]) {
158         write(buf, 0, buf.length);
159     }
160
161
162     @Override
163     public void write(String s, int off, int len) {
164
165         if (error) {
166             return;
167         }
168
169         try {
170             ob.write(s, off, len);
171         } catch (IOException e) {
172             error = true;
173         }
174
175     }
176
177
178     @Override
179     public void write(String s) {
180         write(s, 0, s.length());
181     }
182
183
184     // ---------------------------------------------------- PrintWriter Methods
185
186
187     @Override
188     public void print(boolean b) {
189         if (b) {
190             write("true");
191         } else {
192             write("false");
193         }
194     }
195
196
197     @Override
198     public void print(char c) {
199         write(c);
200     }
201
202
203     @Override
204     public void print(int i) {
205         write(String.valueOf(i));
206     }
207
208
209     @Override
210     public void print(long l) {
211         write(String.valueOf(l));
212     }
213
214
215     @Override
216     public void print(float f) {
217         write(String.valueOf(f));
218     }
219
220
221     @Override
222     public void print(double d) {
223         write(String.valueOf(d));
224     }
225
226
227     @Override
228     public void print(char s[]) {
229         write(s);
230     }
231
232
233     @Override
234     public void print(String s) {
235         if (s == null) {
236             s = "null";
237         }
238         write(s);
239     }
240
241
242     @Override
243     public void print(Object obj) {
244         write(String.valueOf(obj));
245     }
246
247
248     @Override
249     public void println() {
250         write(LINE_SEP);
251     }
252
253
254     @Override
255     public void println(boolean b) {
256         print(b);
257         println();
258     }
259
260
261     @Override
262     public void println(char c) {
263         print(c);
264         println();
265     }
266
267
268     @Override
269     public void println(int i) {
270         print(i);
271         println();
272     }
273
274
275     @Override
276     public void println(long l) {
277         print(l);
278         println();
279     }
280
281
282     @Override
283     public void println(float f) {
284         print(f);
285         println();
286     }
287
288
289     @Override
290     public void println(double d) {
291         print(d);
292         println();
293     }
294
295
296     @Override
297     public void println(char c[]) {
298         print(c);
299         println();
300     }
301
302
303     @Override
304     public void println(String s) {
305         print(s);
306         println();
307     }
308
309
310     @Override
311     public void println(Object o) {
312         print(o);
313         println();
314     }
315
316
317 }
318