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