1 48 49 50 package com.caucho.portal.generic; 51 52 import java.io.IOException ; 53 import java.io.PipedWriter ; 54 import java.io.PrintWriter ; 55 import java.io.Writer ; 56 import java.util.logging.Logger ; 57 58 59 63 public class FastPrintWriter 64 extends PrintWriter 65 { 66 static final public Logger log = 67 Logger.getLogger(FastPrintWriter.class.getName()); 68 69 final static Writer _dummy = new PipedWriter (); 70 private final static char []_newline = "\n".toCharArray(); 71 72 protected Writer _out; 73 private boolean _error; 74 private Exception _errorCause; 75 76 public FastPrintWriter() 77 { 78 super(_dummy); 79 } 80 81 public FastPrintWriter(Writer out) 82 throws IOException 83 { 84 super(_dummy); 85 open(out); 86 } 87 88 89 public void open(Writer out) 90 throws IOException 91 { 92 if (_out != null) 93 throw new IOException ("already open"); 94 95 if (out == null) 96 throw new NullPointerException (); 97 98 _out = out; 99 100 } 101 102 protected void setError() 103 { 104 if (_error == false) { 105 _error = true; 106 _errorCause = new IOException ("stream failed (no exception)"); 107 } 108 } 109 110 protected void setError(Exception 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 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 ex) { 143 setError(ex); 144 } 145 } 146 147 public void writeOut(char buf[], int off, int len) 148 throws IOException 149 { 150 _out.write(buf, off, len); 151 } 152 153 public void writeOut(String str, int off, int len) 154 throws IOException 155 { 156 _out.write(str, off, len); 157 } 158 159 public void writeOut(char c) 160 throws IOException 161 { 162 _out.write((int) c); 163 } 164 165 public void close() 166 { 167 Writer 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 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 ex) { 195 setError(ex); 196 } 197 } 198 199 public void write(String str, int off, int len) 200 { 201 if (checkError()) 202 return; 203 204 try { 205 writeOut(str, off, len); 206 } 207 catch (Exception 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 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 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 s) 271 { 272 write(s == null ? "null" : s); 273 } 274 275 public void print(Object 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 s) 329 { 330 print(s); 331 println(); 332 } 333 334 public void println(Object o) 335 { 336 print(o); 337 println(); 338 } 339 } 340 | Popular Tags |