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.tomcat4;
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: final 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: * Recycle.
049: */
050: void recycle() {
051: error = false;
052: }
053:
054: // --------------------------------------------------------- Writer Methods
055:
056: public void flush() {
057:
058: if (error)
059: return;
060:
061: try {
062: ob.flush();
063: } catch (IOException e) {
064: error = true;
065: }
066:
067: }
068:
069: public void close() {
070:
071: // We don't close the PrintWriter - super() is not called,
072: // so the stream can be reused. We close ob.
073: try {
074: ob.close();
075: } catch (IOException ex) {
076: ;
077: }
078: error = false;
079:
080: }
081:
082: public boolean checkError() {
083: flush();
084: return error;
085: }
086:
087: public void write(int c) {
088:
089: if (error)
090: return;
091:
092: try {
093: ob.write(c);
094: } catch (IOException e) {
095: error = true;
096: }
097:
098: }
099:
100: public void write(char buf[], int off, int len) {
101:
102: if (error)
103: return;
104:
105: try {
106: ob.write(buf, off, len);
107: } catch (IOException e) {
108: error = true;
109: }
110:
111: }
112:
113: public void write(char buf[]) {
114: write(buf, 0, buf.length);
115: }
116:
117: public void write(String s, int off, int len) {
118:
119: if (error)
120: return;
121:
122: try {
123: ob.write(s, off, len);
124: } catch (IOException e) {
125: error = true;
126: }
127:
128: }
129:
130: public void write(String s) {
131: write(s, 0, s.length());
132: }
133:
134: // ---------------------------------------------------- PrintWriter Methods
135:
136: public void print(boolean b) {
137: if (b) {
138: write("true");
139: } else {
140: write("false");
141: }
142: }
143:
144: public void print(char c) {
145: write(c);
146: }
147:
148: public void print(int i) {
149: write(String.valueOf(i));
150: }
151:
152: public void print(long l) {
153: write(String.valueOf(l));
154: }
155:
156: public void print(float f) {
157: write(String.valueOf(f));
158: }
159:
160: public void print(double d) {
161: write(String.valueOf(d));
162: }
163:
164: public void print(char s[]) {
165: write(s);
166: }
167:
168: public void print(String s) {
169: if (s == null) {
170: s = "null";
171: }
172: write(s);
173: }
174:
175: public void print(Object obj) {
176: write(String.valueOf(obj));
177: }
178:
179: public void println() {
180: write(LINE_SEP);
181: }
182:
183: public void println(boolean b) {
184: print(b);
185: println();
186: }
187:
188: public void println(char c) {
189: print(c);
190: println();
191: }
192:
193: public void println(int i) {
194: print(i);
195: println();
196: }
197:
198: public void println(long l) {
199: print(l);
200: println();
201: }
202:
203: public void println(float f) {
204: print(f);
205: println();
206: }
207:
208: public void println(double d) {
209: print(d);
210: println();
211: }
212:
213: public void println(char c[]) {
214: print(c);
215: println();
216: }
217:
218: public void println(String s) {
219: print(s);
220: println();
221: }
222:
223: public void println(Object o) {
224: print(o);
225: println();
226: }
227:
228: }
|