KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > portal > generic > FastPrintWriter


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2001-2004 Caucho Technology, Inc. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution, if
19  * any, must include the following acknowlegement:
20  * "This product includes software developed by the
21  * Caucho Technology (http://www.caucho.com/)."
22  * Alternately, this acknowlegement may appear in the software itself,
23  * if and wherever such third-party acknowlegements normally appear.
24  *
25  * 4. The names "Hessian", "Resin", and "Caucho" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * info@caucho.com.
29  *
30  * 5. Products derived from this software may not be called "Resin"
31  * nor may "Resin" appear in their names without prior written
32  * permission of Caucho Technology.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
35  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37  * DISCLAIMED. IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS
38  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
39  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
40  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
41  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
42  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
43  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
44  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45  *
46  * @author Sam
47  */

48
49
50 package com.caucho.portal.generic;
51
52 import java.io.IOException JavaDoc;
53 import java.io.PipedWriter JavaDoc;
54 import java.io.PrintWriter JavaDoc;
55 import java.io.Writer JavaDoc;
56 import java.util.logging.Logger JavaDoc;
57
58
59 /**
60  * An unsynchronized PrintWriter. Derived classes override three writeOut()
61  * methods to intercept the content being written.
62  */

63 public class FastPrintWriter
64   extends PrintWriter JavaDoc
65 {
66   static final public Logger JavaDoc log =
67     Logger.getLogger(FastPrintWriter.class.getName());
68
69   final static Writer JavaDoc _dummy = new PipedWriter JavaDoc();
70   private final static char []_newline = "\n".toCharArray();
71
72   protected Writer JavaDoc _out;
73   private boolean _error;
74   private Exception JavaDoc _errorCause;
75
76   public FastPrintWriter()
77   {
78     super(_dummy);
79   }
80
81   public FastPrintWriter(Writer JavaDoc out)
82     throws IOException JavaDoc
83   {
84     super(_dummy);
85     open(out);
86   }
87
88
89   public void open(Writer JavaDoc out)
90     throws IOException JavaDoc
91   {
92     if (_out != null)
93       throw new IOException JavaDoc("already open");
94
95     if (out == null)
96       throw new NullPointerException JavaDoc();
97
98     _out = out;
99
100   }
101
102   protected void setError()
103   {
104     if (_error == false) {
105       _error = true;
106       _errorCause = new IOException JavaDoc("stream failed (no exception)");
107     }
108   }
109
110   protected void setError(Exception JavaDoc errorCause)
111   {
112     if (_error == false) {
113       _error = true;
114       _errorCause = errorCause;
115     }
116   }
117
118   public boolean checkError()
119   {
120     return _error;
121   }
122
123   public Exception JavaDoc getErrorCause()
124   {
125     return _errorCause;
126   }
127
128   public void clearError()
129   {
130     _error = false;
131     _errorCause = null;
132   }
133
134   public void flush()
135   {
136     if (checkError())
137       return;
138
139     try {
140       _out.flush();
141     }
142     catch (Exception JavaDoc ex) {
143       setError(ex);
144     }
145   }
146
147   public void writeOut(char buf[], int off, int len)
148     throws IOException JavaDoc
149   {
150     _out.write(buf, off, len);
151   }
152
153   public void writeOut(String JavaDoc str, int off, int len)
154     throws IOException JavaDoc
155   {
156     _out.write(str, off, len);
157   }
158
159   public void writeOut(char c)
160     throws IOException JavaDoc
161   {
162     _out.write((int) c);
163   }
164
165   public void close()
166   {
167     Writer JavaDoc out = _out;
168     boolean error = _error;
169
170     flush();
171
172     _out = null;
173     _error = false;
174     _errorCause = null;
175
176     if (!_error) {
177       try {
178         out.close();
179       }
180       catch (IOException JavaDoc ex) {
181         setError(ex);
182       }
183     }
184   }
185
186   public void write(char buf[], int off, int len)
187   {
188     if (checkError())
189       return;
190
191     try {
192       writeOut(buf, off, len);
193     }
194     catch (Exception JavaDoc ex) {
195       setError(ex);
196     }
197   }
198
199   public void write(String JavaDoc str, int off, int len)
200   {
201     if (checkError())
202       return;
203
204     try {
205       writeOut(str, off, len);
206     }
207     catch (Exception JavaDoc ex) {
208       setError(ex);
209     }
210   }
211
212   public void write(int c)
213   {
214     if (checkError())
215       return;
216
217     try {
218       writeOut((char) c);
219     }
220     catch (Exception JavaDoc ex) {
221       setError(ex);
222     }
223   }
224
225   public void write(char cbuf[])
226   {
227     write(cbuf, 0, cbuf.length);
228   }
229
230   public void write(String JavaDoc str)
231   {
232     write(str, 0, str.length());
233   }
234
235   public void print(boolean b)
236   {
237     write(b ? "true" : "false");
238   }
239
240   public void print(char c)
241   {
242     write(c);
243   }
244
245   public void print(int i)
246   {
247     write(String.valueOf(i));
248   }
249
250   public void print(long l)
251   {
252     write(String.valueOf(l));
253   }
254
255   public void print(float f)
256   {
257     write(String.valueOf(f));
258   }
259
260   public void print(double d)
261   {
262     write(String.valueOf(d));
263   }
264
265   public void print(char s[])
266   {
267     write(s);
268   }
269
270   public void print(String JavaDoc s)
271   {
272     write(s == null ? "null" : s);
273   }
274
275   public void print(Object JavaDoc obj)
276   {
277     write(String.valueOf(obj));
278   }
279
280   public void println()
281   {
282     write(_newline, 0, _newline.length);
283   }
284
285
286   public void println(boolean b)
287   {
288     print(b);
289     println();
290   }
291
292   public void println(char c)
293   {
294     print(c);
295     println();
296   }
297
298   public void println(int i)
299   {
300     print(i);
301     println();
302   }
303
304   public void println(long l)
305   {
306     print(l);
307     println();
308   }
309
310   public void println(float f)
311   {
312     print(f);
313     println();
314   }
315
316   public void println(double d)
317   {
318     print(d);
319     println();
320   }
321
322   public void println(char c[])
323   {
324     print(c);
325     println();
326   }
327
328   public void println(String JavaDoc s)
329   {
330     print(s);
331     println();
332   }
333
334   public void println(Object JavaDoc o)
335   {
336     print(o);
337     println();
338   }
339 }
340
Popular Tags