001: /*
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 2001-2004 Caucho Technology, Inc. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution, if
019: * any, must include the following acknowlegement:
020: * "This product includes software developed by the
021: * Caucho Technology (http://www.caucho.com/)."
022: * Alternately, this acknowlegement may appear in the software itself,
023: * if and wherever such third-party acknowlegements normally appear.
024: *
025: * 4. The names "Hessian", "Resin", and "Caucho" must not be used to
026: * endorse or promote products derived from this software without prior
027: * written permission. For written permission, please contact
028: * info@caucho.com.
029: *
030: * 5. Products derived from this software may not be called "Resin"
031: * nor may "Resin" appear in their names without prior written
032: * permission of Caucho Technology.
033: *
034: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
035: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
036: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
037: * DISCLAIMED. IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS
038: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
039: * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
040: * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
041: * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
042: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
043: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
044: * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
045: *
046: * @author Sam
047: */
048:
049: package com.caucho.portal.generic;
050:
051: import java.io.IOException;
052: import java.io.PipedWriter;
053: import java.io.PrintWriter;
054: import java.io.Writer;
055: import java.util.logging.Logger;
056:
057: /**
058: * An unsynchronized PrintWriter. Derived classes override three writeOut()
059: * methods to intercept the content being written.
060: */
061: public class FastPrintWriter extends PrintWriter {
062: static final public Logger log = Logger
063: .getLogger(FastPrintWriter.class.getName());
064:
065: final static Writer _dummy = new PipedWriter();
066: private final static char[] _newline = "\n".toCharArray();
067:
068: protected Writer _out;
069: private boolean _error;
070: private Exception _errorCause;
071:
072: public FastPrintWriter() {
073: super (_dummy);
074: }
075:
076: public FastPrintWriter(Writer out) throws IOException {
077: super (_dummy);
078: open(out);
079: }
080:
081: public void open(Writer out) throws IOException {
082: if (_out != null)
083: throw new IOException("already open");
084:
085: if (out == null)
086: throw new NullPointerException();
087:
088: _out = out;
089:
090: }
091:
092: protected void setError() {
093: if (_error == false) {
094: _error = true;
095: _errorCause = new IOException(
096: "stream failed (no exception)");
097: }
098: }
099:
100: protected void setError(Exception errorCause) {
101: if (_error == false) {
102: _error = true;
103: _errorCause = errorCause;
104: }
105: }
106:
107: public boolean checkError() {
108: return _error;
109: }
110:
111: public Exception getErrorCause() {
112: return _errorCause;
113: }
114:
115: public void clearError() {
116: _error = false;
117: _errorCause = null;
118: }
119:
120: public void flush() {
121: if (checkError())
122: return;
123:
124: try {
125: _out.flush();
126: } catch (Exception ex) {
127: setError(ex);
128: }
129: }
130:
131: public void writeOut(char buf[], int off, int len)
132: throws IOException {
133: _out.write(buf, off, len);
134: }
135:
136: public void writeOut(String str, int off, int len)
137: throws IOException {
138: _out.write(str, off, len);
139: }
140:
141: public void writeOut(char c) throws IOException {
142: _out.write((int) c);
143: }
144:
145: public void close() {
146: Writer out = _out;
147: boolean error = _error;
148:
149: flush();
150:
151: _out = null;
152: _error = false;
153: _errorCause = null;
154:
155: if (!_error) {
156: try {
157: out.close();
158: } catch (IOException ex) {
159: setError(ex);
160: }
161: }
162: }
163:
164: public void write(char buf[], int off, int len) {
165: if (checkError())
166: return;
167:
168: try {
169: writeOut(buf, off, len);
170: } catch (Exception ex) {
171: setError(ex);
172: }
173: }
174:
175: public void write(String str, int off, int len) {
176: if (checkError())
177: return;
178:
179: try {
180: writeOut(str, off, len);
181: } catch (Exception ex) {
182: setError(ex);
183: }
184: }
185:
186: public void write(int c) {
187: if (checkError())
188: return;
189:
190: try {
191: writeOut((char) c);
192: } catch (Exception ex) {
193: setError(ex);
194: }
195: }
196:
197: public void write(char cbuf[]) {
198: write(cbuf, 0, cbuf.length);
199: }
200:
201: public void write(String str) {
202: write(str, 0, str.length());
203: }
204:
205: public void print(boolean b) {
206: write(b ? "true" : "false");
207: }
208:
209: public void print(char c) {
210: write(c);
211: }
212:
213: public void print(int i) {
214: write(String.valueOf(i));
215: }
216:
217: public void print(long l) {
218: write(String.valueOf(l));
219: }
220:
221: public void print(float f) {
222: write(String.valueOf(f));
223: }
224:
225: public void print(double d) {
226: write(String.valueOf(d));
227: }
228:
229: public void print(char s[]) {
230: write(s);
231: }
232:
233: public void print(String s) {
234: write(s == null ? "null" : s);
235: }
236:
237: public void print(Object obj) {
238: write(String.valueOf(obj));
239: }
240:
241: public void println() {
242: write(_newline, 0, _newline.length);
243: }
244:
245: public void println(boolean b) {
246: print(b);
247: println();
248: }
249:
250: public void println(char c) {
251: print(c);
252: println();
253: }
254:
255: public void println(int i) {
256: print(i);
257: println();
258: }
259:
260: public void println(long l) {
261: print(l);
262: println();
263: }
264:
265: public void println(float f) {
266: print(f);
267: println();
268: }
269:
270: public void println(double d) {
271: print(d);
272: println();
273: }
274:
275: public void println(char c[]) {
276: print(c);
277: println();
278: }
279:
280: public void println(String s) {
281: print(s);
282: println();
283: }
284:
285: public void println(Object o) {
286: print(o);
287: println();
288: }
289: }
|