001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.catalina.connector;
019:
020: import java.io.IOException;
021: import java.io.PrintWriter;
022:
023: /**
024: * Coyote implementation of the servlet writer.
025: *
026: * @author Remy Maucherat
027: */
028: public class CoyoteWriter extends PrintWriter {
029:
030: // -------------------------------------------------------------- Constants
031:
032: private static final char[] LINE_SEP = { '\r', '\n' };
033:
034: // ----------------------------------------------------- Instance Variables
035:
036: protected OutputBuffer ob;
037: protected boolean error = false;
038:
039: // ----------------------------------------------------------- Constructors
040:
041: public CoyoteWriter(OutputBuffer ob) {
042: super (ob);
043: this .ob = ob;
044: }
045:
046: // --------------------------------------------------------- Public Methods
047:
048: /**
049: * Prevent cloning the facade.
050: */
051: protected Object clone() throws CloneNotSupportedException {
052: throw new CloneNotSupportedException();
053: }
054:
055: // -------------------------------------------------------- Package Methods
056:
057: /**
058: * Clear facade.
059: */
060: void clear() {
061: ob = null;
062: }
063:
064: /**
065: * Recycle.
066: */
067: void recycle() {
068: error = false;
069: }
070:
071: // --------------------------------------------------------- Writer Methods
072:
073: public void flush() {
074:
075: if (error)
076: return;
077:
078: try {
079: ob.flush();
080: } catch (IOException e) {
081: error = true;
082: }
083:
084: }
085:
086: public void close() {
087:
088: // We don't close the PrintWriter - super() is not called,
089: // so the stream can be reused. We close ob.
090: try {
091: ob.close();
092: } catch (IOException ex) {
093: ;
094: }
095: error = false;
096:
097: }
098:
099: public boolean checkError() {
100: flush();
101: return error;
102: }
103:
104: public void write(int c) {
105:
106: if (error)
107: return;
108:
109: try {
110: ob.write(c);
111: } catch (IOException e) {
112: error = true;
113: }
114:
115: }
116:
117: public void write(char buf[], int off, int len) {
118:
119: if (error)
120: return;
121:
122: try {
123: ob.write(buf, off, len);
124: } catch (IOException e) {
125: error = true;
126: }
127:
128: }
129:
130: public void write(char buf[]) {
131: write(buf, 0, buf.length);
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: public void write(String s) {
148: write(s, 0, s.length());
149: }
150:
151: // ---------------------------------------------------- PrintWriter Methods
152:
153: public void print(boolean b) {
154: if (b) {
155: write("true");
156: } else {
157: write("false");
158: }
159: }
160:
161: public void print(char c) {
162: write(c);
163: }
164:
165: public void print(int i) {
166: write(String.valueOf(i));
167: }
168:
169: public void print(long l) {
170: write(String.valueOf(l));
171: }
172:
173: public void print(float f) {
174: write(String.valueOf(f));
175: }
176:
177: public void print(double d) {
178: write(String.valueOf(d));
179: }
180:
181: public void print(char s[]) {
182: write(s);
183: }
184:
185: public void print(String s) {
186: if (s == null) {
187: s = "null";
188: }
189: write(s);
190: }
191:
192: public void print(Object obj) {
193: write(String.valueOf(obj));
194: }
195:
196: public void println() {
197: write(LINE_SEP);
198: }
199:
200: public void println(boolean b) {
201: print(b);
202: println();
203: }
204:
205: public void println(char c) {
206: print(c);
207: println();
208: }
209:
210: public void println(int i) {
211: print(i);
212: println();
213: }
214:
215: public void println(long l) {
216: print(l);
217: println();
218: }
219:
220: public void println(float f) {
221: print(f);
222: println();
223: }
224:
225: public void println(double d) {
226: print(d);
227: println();
228: }
229:
230: public void println(char c[]) {
231: print(c);
232: println();
233: }
234:
235: public void println(String s) {
236: print(s);
237: println();
238: }
239:
240: public void println(Object o) {
241: print(o);
242: println();
243: }
244:
245: }
|