1 28 29 package com.caucho.vfs; 30 31 import java.io.IOException ; 32 import java.io.PrintWriter ; 33 import java.io.StringWriter ; 34 import java.io.Writer ; 35 import java.util.logging.Level ; 36 import java.util.logging.Logger ; 37 38 41 public class PrintWriterImpl extends PrintWriter implements FlushBuffer { 42 private final static Logger log 43 = Logger.getLogger(PrintWriterImpl.class.getName()); 44 45 private final static char []_nullChars = "null".toCharArray(); 46 private final static char []_newline = "\n".toCharArray(); 47 48 private final static Writer _dummyWriter = new StringWriter(); 49 50 private final char []_tempCharBuffer = new char[64]; 51 52 55 public PrintWriterImpl() 56 { 57 super((Writer ) _dummyWriter); 58 } 59 60 63 public PrintWriterImpl(Writer out) 64 { 65 super(out); 66 } 67 68 71 public void setWriter(Writer out) 72 { 73 this.out = out; 74 } 75 76 79 final public void write(char ch) 80 { 81 Writer out = this.out; 82 if (out == null) 83 return; 84 85 try { 86 out.write(ch); 87 } catch (IOException e) { 88 log.log(Level.FINE, e.toString(), e); 89 } 90 } 91 92 95 final public void write(char []buf, int offset, int length) 96 { 97 Writer out = this.out; 98 if (out == null) 99 return; 100 101 try { 102 out.write(buf, offset, length); 103 } catch (IOException e) { 104 log.log(Level.FINE, e.toString(), e); 105 } 106 } 107 108 111 final public void write(char []buf) 112 { 113 Writer out = this.out; 114 if (out == null) 115 return; 116 117 try { 118 out.write(buf, 0, buf.length); 119 } catch (IOException e) { 120 log.log(Level.FINE, e.toString(), e); 121 } 122 } 123 124 127 final public void print(char ch) 128 { 129 Writer out = this.out; 130 if (out == null) 131 return; 132 133 try { 134 out.write(ch); 135 } catch (IOException e) { 136 log.log(Level.FINE, e.toString(), e); 137 } 138 } 139 140 143 final public void print(int i) 144 { 145 Writer out = this.out; 146 if (out == null) 147 return; 148 149 if (i == 0x80000000) { 150 print("-2147483648"); 151 return; 152 } 153 154 try { 155 if (i < 0) { 156 out.write('-'); 157 i = -i; 158 } else if (i < 9) { 159 out.write('0' + i); 160 return; 161 } 162 163 int length = 0; 164 int exp = 10; 165 166 if (i >= 1000000000) 167 length = 9; 168 else { 169 for (; i >= exp; length++) 170 exp = 10 * exp; 171 } 172 173 int j = 31; 174 175 while (i > 0) { 176 _tempCharBuffer[--j] = (char) ((i % 10) + '0'); 177 i /= 10; 178 } 179 180 out.write(_tempCharBuffer, j, 31 - j); 181 } catch (IOException e) { 182 log.log(Level.FINE, e.toString(), e); 183 } 184 } 185 186 189 final public void print(long v) 190 { 191 Writer out = this.out; 192 if (out == null) 193 return; 194 195 if (v == 0x8000000000000000L) { 196 print("-9223372036854775808"); 197 return; 198 } 199 200 try { 201 if (v < 0) { 202 out.write('-'); 203 v = -v; 204 } else if (v == 0) { 205 out.write('0'); 206 return; 207 } 208 209 int j = 31; 210 211 while (v > 0) { 212 _tempCharBuffer[--j] = (char) ((v % 10) + '0'); 213 v /= 10; 214 } 215 216 out.write(_tempCharBuffer, j, 31 - j); 217 } catch (IOException e) { 218 log.log(Level.FINE, e.toString(), e); 219 } 220 } 221 222 227 final public void print(float v) 228 { 229 Writer out = this.out; 230 if (out == null) 231 return; 232 233 try { 234 String s = String.valueOf(v); 235 out.write(s, 0, s.length()); 236 } catch (IOException e) { 237 log.log(Level.FINE, e.toString(), e); 238 } 239 } 240 241 246 final public void print(double v) 247 { 248 Writer out = this.out; 249 if (out == null) 250 return; 251 252 try { 253 String s = String.valueOf(v); 254 out.write(s, 0, s.length()); 255 } catch (IOException e) { 256 log.log(Level.FINE, e.toString(), e); 257 } 258 } 259 260 263 final public void print(char []s) 264 { 265 Writer out = this.out; 266 if (out == null) 267 return; 268 269 try { 270 out.write(s, 0, s.length); 271 } catch (IOException e) { 272 log.log(Level.FINE, e.toString(), e); 273 } 274 } 275 276 279 final public void print(String s) 280 { 281 Writer out = this.out; 282 if (out == null) 283 return; 284 285 try { 286 if (s == null) 287 out.write(_nullChars, 0, _nullChars.length); 288 else 289 out.write(s, 0, s.length()); 290 } catch (IOException e) { 291 log.log(Level.FINE, e.toString(), e); 292 } 293 } 294 295 298 final public void print(Object v) 299 { 300 Writer out = this.out; 301 if (out == null) 302 return; 303 304 try { 305 if (v == null) 306 out.write(_nullChars, 0, _nullChars.length); 307 else { 308 String s = v.toString(); 309 310 out.write(s, 0, s.length()); 311 } 312 } catch (IOException e) { 313 log.log(Level.FINE, e.toString(), e); 314 } 315 } 316 317 320 final public void println() 321 { 322 Writer out = this.out; 323 if (out == null) 324 return; 325 326 try { 327 out.write(_newline, 0, _newline.length); 328 } catch (IOException e) { 329 log.log(Level.FINE, e.toString(), e); 330 } 331 } 332 333 338 final public void println(boolean v) 339 { 340 Writer out = this.out; 341 if (out == null) 342 return; 343 344 print(v); 345 346 try { 347 out.write(_newline, 0, _newline.length); 348 } catch (IOException e) { 349 log.log(Level.FINE, e.toString(), e); 350 } 351 } 352 353 358 final public void println(char v) 359 { 360 Writer out = this.out; 361 if (out == null) 362 return; 363 364 try { 365 out.write(v); 366 out.write(_newline, 0, _newline.length); 367 } catch (IOException e) { 368 log.log(Level.FINE, e.toString(), e); 369 } 370 } 371 372 377 final public void println(int v) 378 { 379 Writer out = this.out; 380 if (out == null) 381 return; 382 383 print(v); 384 385 try { 386 out.write(_newline, 0, _newline.length); 387 } catch (IOException e) { 388 log.log(Level.FINE, e.toString(), e); 389 } 390 } 391 392 397 final public void println(long v) 398 { 399 Writer out = this.out; 400 if (out == null) 401 return; 402 403 print(v); 404 405 try { 406 out.write(_newline, 0, _newline.length); 407 } catch (IOException e) { 408 log.log(Level.FINE, e.toString(), e); 409 } 410 } 411 412 417 final public void println(float v) 418 { 419 Writer out = this.out; 420 if (out == null) 421 return; 422 423 String s = String.valueOf(v); 424 425 try { 426 out.write(s, 0, s.length()); 427 out.write(_newline, 0, _newline.length); 428 } catch (IOException e) { 429 log.log(Level.FINE, e.toString(), e); 430 } 431 } 432 433 438 final public void println(double v) 439 { 440 Writer out = this.out; 441 if (out == null) 442 return; 443 444 print(v); 445 446 try { 447 out.write(_newline, 0, _newline.length); 448 } catch (IOException e) { 449 log.log(Level.FINE, e.toString(), e); 450 } 451 } 452 453 456 final public void println(char []s) 457 { 458 Writer out = this.out; 459 if (out == null) 460 return; 461 462 try { 463 out.write(s, 0, s.length); 464 out.write(_newline, 0, _newline.length); 465 } catch (IOException e) { 466 log.log(Level.FINE, e.toString(), e); 467 } 468 } 469 470 473 final public void println(String s) 474 { 475 Writer out = this.out; 476 if (out == null) 477 return; 478 479 try { 480 if (s == null) 481 out.write(_nullChars, 0, _nullChars.length); 482 else 483 out.write(s, 0, s.length()); 484 485 out.write(_newline, 0, _newline.length); 486 } catch (IOException e) { 487 log.log(Level.FINE, e.toString(), e); 488 } 489 } 490 491 494 final public void println(Object v) 495 { 496 Writer out = this.out; 497 if (out == null) 498 return; 499 500 try { 501 if (v == null) 502 out.write(_nullChars, 0, _nullChars.length); 503 else { 504 String s = v.toString(); 505 506 out.write(s, 0, s.length()); 507 } 508 509 out.write(_newline, 0, _newline.length); 510 } catch (IOException e) { 511 log.log(Level.FINE, e.toString(), e); 512 } 513 } 514 515 518 public void flush() 519 { 520 Writer out = this.out; 521 if (out == null) 522 return; 523 524 try { 525 out.flush(); 526 } catch (IOException e) { 527 log.log(Level.FINE, e.toString(), e); 528 } 529 } 530 531 534 public void flushBuffer() 535 { 536 Writer out = this.out; 537 if (out == null) 538 return; 539 540 try { 541 if (out instanceof FlushBuffer) 542 ((FlushBuffer) out).flushBuffer(); 543 } catch (IOException e) { 544 log.log(Level.FINE, e.toString(), e); 545 } 546 } 547 548 public void close() 549 { 550 this.out = null; 551 } 552 } 553 | Popular Tags |