1 55 56 57 package com.icesoft.faces.webapp.parser; 58 59 import com.icesoft.jasper.Constants; 60 61 import javax.servlet.jsp.JspWriter ; 62 import javax.servlet.jsp.tagext.BodyContent ; 63 import java.io.CharArrayReader ; 64 import java.io.IOException ; 65 import java.io.Reader ; 66 import java.io.Writer ; 67 68 77 public class BodyContentImpl extends BodyContent { 78 79 private static final String LINE_SEPARATOR = System.getProperty( 80 "line.separator"); 81 82 private char[] cb; 83 private int nextChar; 84 private boolean closed; 85 86 private Writer writer; 88 89 private int bufferSizeSave; 91 92 95 public BodyContentImpl(JspWriter enclosingWriter) { 96 super(enclosingWriter); 97 bufferSize = Constants.DEFAULT_TAG_BUFFER_SIZE; 98 cb = new char[bufferSize]; 99 nextChar = 0; 100 closed = false; 101 } 102 103 106 public void write(int c) throws IOException { 107 if (writer != null) { 108 writer.write(c); 109 } else { 110 ensureOpen(); 111 if (nextChar >= bufferSize) { 112 reAllocBuff(1); 113 } 114 cb[nextChar++] = (char) c; 115 } 116 } 117 118 133 public void write(char[] cbuf, int off, int len) throws IOException { 134 if (writer != null) { 135 writer.write(cbuf, off, len); 136 } else { 137 ensureOpen(); 138 139 if ((off < 0) || (off > cbuf.length) || (len < 0) || 140 ((off + len) > cbuf.length) || ((off + len) < 0)) { 141 throw new IndexOutOfBoundsException (); 142 } else if (len == 0) { 143 return; 144 } 145 146 if (len >= bufferSize - nextChar) 147 reAllocBuff(len); 148 149 System.arraycopy(cbuf, off, cb, nextChar, len); 150 nextChar += len; 151 } 152 } 153 154 158 public void write(char[] buf) throws IOException { 159 if (writer != null) { 160 writer.write(buf); 161 } else { 162 write(buf, 0, buf.length); 163 } 164 } 165 166 173 public void write(String s, int off, int len) throws IOException { 174 if (writer != null) { 175 writer.write(s, off, len); 176 } else { 177 ensureOpen(); 178 if (len >= bufferSize - nextChar) 179 reAllocBuff(len); 180 181 s.getChars(off, off + len, cb, nextChar); 182 nextChar += len; 183 } 184 } 185 186 190 public void write(String s) throws IOException { 191 if (writer != null) { 192 writer.write(s); 193 } else { 194 write(s, 0, s.length()); 195 } 196 } 197 198 205 public void newLine() throws IOException { 206 if (writer != null) { 207 writer.write(LINE_SEPARATOR); 208 } else { 209 write(LINE_SEPARATOR); 210 } 211 } 212 213 223 public void print(boolean b) throws IOException { 224 if (writer != null) { 225 writer.write(b ? "true" : "false"); 226 } else { 227 write(b ? "true" : "false"); 228 } 229 } 230 231 240 public void print(char c) throws IOException { 241 if (writer != null) { 242 writer.write(String.valueOf(c)); 243 } else { 244 write(String.valueOf(c)); 245 } 246 } 247 248 257 public void print(int i) throws IOException { 258 if (writer != null) { 259 writer.write(String.valueOf(i)); 260 } else { 261 write(String.valueOf(i)); 262 } 263 } 264 265 274 public void print(long l) throws IOException { 275 if (writer != null) { 276 writer.write(String.valueOf(l)); 277 } else { 278 write(String.valueOf(l)); 279 } 280 } 281 282 292 public void print(float f) throws IOException { 293 if (writer != null) { 294 writer.write(String.valueOf(f)); 295 } else { 296 write(String.valueOf(f)); 297 } 298 } 299 300 310 public void print(double d) throws IOException { 311 if (writer != null) { 312 writer.write(String.valueOf(d)); 313 } else { 314 write(String.valueOf(d)); 315 } 316 } 317 318 328 public void print(char[] s) throws IOException { 329 if (writer != null) { 330 writer.write(s); 331 } else { 332 write(s); 333 } 334 } 335 336 346 public void print(String s) throws IOException { 347 if (s == null) s = "null"; 348 if (writer != null) { 349 writer.write(s); 350 } else { 351 write(s); 352 } 353 } 354 355 365 public void print(Object obj) throws IOException { 366 if (writer != null) { 367 writer.write(String.valueOf(obj)); 368 } else { 369 write(String.valueOf(obj)); 370 } 371 } 372 373 381 public void println() throws IOException { 382 newLine(); 383 } 384 385 392 public void println(boolean x) throws IOException { 393 print(x); 394 println(); 395 } 396 397 404 public void println(char x) throws IOException { 405 print(x); 406 println(); 407 } 408 409 416 public void println(int x) throws IOException { 417 print(x); 418 println(); 419 } 420 421 428 public void println(long x) throws IOException { 429 print(x); 430 println(); 431 } 432 433 440 public void println(float x) throws IOException { 441 print(x); 442 println(); 443 } 444 445 452 public void println(double x) throws IOException { 453 print(x); 454 println(); 455 } 456 457 464 public void println(char x[]) throws IOException { 465 print(x); 466 println(); 467 } 468 469 476 public void println(String x) throws IOException { 477 print(x); 478 println(); 479 } 480 481 488 public void println(Object x) throws IOException { 489 print(x); 490 println(); 491 } 492 493 501 public void clear() throws IOException { 502 if (writer != null) { 503 throw new IOException (); 504 } else { 505 nextChar = 0; 506 } 507 } 508 509 516 public void clearBuffer() throws IOException { 517 if (writer == null) { 518 this.clear(); 519 } 520 } 521 522 529 public void close() throws IOException { 530 if (writer != null) { 531 writer.close(); 532 } else { 533 cb = null; 534 closed = true; 535 } 536 } 537 538 541 public int getRemaining() { 542 return (writer == null) ? bufferSize - nextChar : 0; 543 } 544 545 551 public Reader getReader() { 552 return (writer == null) ? new CharArrayReader (cb, 0, nextChar) : null; 553 } 554 555 561 public String getString() { 562 return (writer == null) ? new String (cb, 0, nextChar) : null; 563 } 564 565 573 public void writeOut(Writer out) throws IOException { 574 if (writer == null) { 575 out.write(cb, 0, nextChar); 576 } 579 } 580 581 584 void setWriter(Writer writer) { 585 this.writer = writer; 586 if (writer != null) { 587 if (bufferSize != 0) { 597 bufferSizeSave = bufferSize; 598 bufferSize = 0; 599 } 600 } else { 601 bufferSize = bufferSizeSave; 602 clearBody(); 603 } 604 } 605 606 private void ensureOpen() throws IOException { 607 if (closed) throw new IOException ("Stream closed"); 608 } 609 610 613 private void reAllocBuff(int len) { 614 615 if (bufferSize + len <= cb.length) { 616 bufferSize = cb.length; 617 return; 618 } 619 620 if (len < cb.length) { 621 len = cb.length; 622 } 623 624 bufferSize = cb.length + len; 625 char[] tmp = new char[bufferSize]; 626 627 System.arraycopy(cb, 0, tmp, 0, cb.length); 628 cb = tmp; 629 tmp = null; 630 631 } 632 633 634 } 635 | Popular Tags |