1 17 18 package org.apache.jasper.runtime; 19 20 import java.io.CharArrayReader ; 21 import java.io.IOException ; 22 import java.io.Reader ; 23 import java.io.Writer ; 24 25 import javax.servlet.jsp.JspWriter ; 26 import javax.servlet.jsp.tagext.BodyContent ; 27 28 import org.apache.jasper.Constants; 29 30 40 public class BodyContentImpl extends BodyContent { 41 42 private static final String LINE_SEPARATOR = 43 System.getProperty("line.separator"); 44 private static final boolean LIMIT_BUFFER = 45 Boolean.valueOf(System.getProperty("org.apache.jasper.runtime.BodyContentImpl.LIMIT_BUFFER", "false")).booleanValue(); 46 47 private char[] cb; 48 private int nextChar; 49 private boolean closed; 50 51 private Writer writer; 53 54 private int bufferSizeSave; 56 57 60 public BodyContentImpl(JspWriter enclosingWriter) { 61 super(enclosingWriter); 62 bufferSize = Constants.DEFAULT_TAG_BUFFER_SIZE; 63 cb = new char[bufferSize]; 64 nextChar = 0; 65 closed = false; 66 } 67 68 71 public void write(int c) throws IOException { 72 if (writer != null) { 73 writer.write(c); 74 } else { 75 ensureOpen(); 76 if (nextChar >= bufferSize) { 77 reAllocBuff (1); 78 } 79 cb[nextChar++] = (char) c; 80 } 81 } 82 83 98 public void write(char[] cbuf, int off, int len) throws IOException { 99 if (writer != null) { 100 writer.write(cbuf, off, len); 101 } else { 102 ensureOpen(); 103 104 if ((off < 0) || (off > cbuf.length) || (len < 0) || 105 ((off + len) > cbuf.length) || ((off + len) < 0)) { 106 throw new IndexOutOfBoundsException (); 107 } else if (len == 0) { 108 return; 109 } 110 111 if (len >= bufferSize - nextChar) 112 reAllocBuff (len); 113 114 System.arraycopy(cbuf, off, cb, nextChar, len); 115 nextChar+=len; 116 } 117 } 118 119 123 public void write(char[] buf) throws IOException { 124 if (writer != null) { 125 writer.write(buf); 126 } else { 127 write(buf, 0, buf.length); 128 } 129 } 130 131 138 public void write(String s, int off, int len) throws IOException { 139 if (writer != null) { 140 writer.write(s, off, len); 141 } else { 142 ensureOpen(); 143 if (len >= bufferSize - nextChar) 144 reAllocBuff(len); 145 146 s.getChars(off, off + len, cb, nextChar); 147 nextChar += len; 148 } 149 } 150 151 155 public void write(String s) throws IOException { 156 if (writer != null) { 157 writer.write(s); 158 } else { 159 write(s, 0, s.length()); 160 } 161 } 162 163 170 public void newLine() throws IOException { 171 if (writer != null) { 172 writer.write(LINE_SEPARATOR); 173 } else { 174 write(LINE_SEPARATOR); 175 } 176 } 177 178 188 public void print(boolean b) throws IOException { 189 if (writer != null) { 190 writer.write(b ? "true" : "false"); 191 } else { 192 write(b ? "true" : "false"); 193 } 194 } 195 196 205 public void print(char c) throws IOException { 206 if (writer != null) { 207 writer.write(String.valueOf(c)); 208 } else { 209 write(String.valueOf(c)); 210 } 211 } 212 213 223 public void print(int i) throws IOException { 224 if (writer != null) { 225 writer.write(String.valueOf(i)); 226 } else { 227 write(String.valueOf(i)); 228 } 229 } 230 231 241 public void print(long l) throws IOException { 242 if (writer != null) { 243 writer.write(String.valueOf(l)); 244 } else { 245 write(String.valueOf(l)); 246 } 247 } 248 249 259 public void print(float f) throws IOException { 260 if (writer != null) { 261 writer.write(String.valueOf(f)); 262 } else { 263 write(String.valueOf(f)); 264 } 265 } 266 267 277 public void print(double d) throws IOException { 278 if (writer != null) { 279 writer.write(String.valueOf(d)); 280 } else { 281 write(String.valueOf(d)); 282 } 283 } 284 285 296 public void print(char[] s) throws IOException { 297 if (writer != null) { 298 writer.write(s); 299 } else { 300 write(s); 301 } 302 } 303 304 314 public void print(String s) throws IOException { 315 if (s == null) s = "null"; 316 if (writer != null) { 317 writer.write(s); 318 } else { 319 write(s); 320 } 321 } 322 323 333 public void print(Object obj) throws IOException { 334 if (writer != null) { 335 writer.write(String.valueOf(obj)); 336 } else { 337 write(String.valueOf(obj)); 338 } 339 } 340 341 349 public void println() throws IOException { 350 newLine(); 351 } 352 353 360 public void println(boolean x) throws IOException { 361 print(x); 362 println(); 363 } 364 365 372 public void println(char x) throws IOException { 373 print(x); 374 println(); 375 } 376 377 384 public void println(int x) throws IOException { 385 print(x); 386 println(); 387 } 388 389 396 public void println(long x) throws IOException { 397 print(x); 398 println(); 399 } 400 401 408 public void println(float x) throws IOException { 409 print(x); 410 println(); 411 } 412 413 420 public void println(double x) throws IOException { 421 print(x); 422 println(); 423 } 424 425 432 public void println(char x[]) throws IOException { 433 print(x); 434 println(); 435 } 436 437 444 public void println(String x) throws IOException { 445 print(x); 446 println(); 447 } 448 449 456 public void println(Object x) throws IOException { 457 print(x); 458 println(); 459 } 460 461 469 public void clear() throws IOException { 470 if (writer != null) { 471 throw new IOException (); 472 } else { 473 nextChar = 0; 474 if (LIMIT_BUFFER && (cb.length > Constants.DEFAULT_TAG_BUFFER_SIZE)) { 475 bufferSize = Constants.DEFAULT_TAG_BUFFER_SIZE; 476 cb = new char[bufferSize]; 477 } 478 } 479 } 480 481 489 public void clearBuffer() throws IOException { 490 if (writer == null) { 491 this.clear(); 492 } 493 } 494 495 502 public void close() throws IOException { 503 if (writer != null) { 504 writer.close(); 505 } else { 506 closed = true; 507 } 508 } 509 510 513 public int getRemaining() { 514 return (writer == null) ? bufferSize-nextChar : 0; 515 } 516 517 524 public Reader getReader() { 525 return (writer == null) ? new CharArrayReader (cb, 0, nextChar) : null; 526 } 527 528 535 public String getString() { 536 return (writer == null) ? new String (cb, 0, nextChar) : null; 537 } 538 539 547 public void writeOut(Writer out) throws IOException { 548 if (writer == null) { 549 out.write(cb, 0, nextChar); 550 } 553 } 554 555 558 void setWriter(Writer writer) { 559 this.writer = writer; 560 closed = false; 561 if (writer != null) { 562 if (bufferSize != 0) { 572 bufferSizeSave = bufferSize; 573 bufferSize = 0; 574 } 575 } else { 576 bufferSize = bufferSizeSave; 577 clearBody(); 578 } 579 } 580 581 private void ensureOpen() throws IOException { 582 if (closed) throw new IOException ("Stream closed"); 583 } 584 585 588 private void reAllocBuff(int len) { 589 590 if (bufferSize + len <= cb.length) { 591 bufferSize = cb.length; 592 return; 593 } 594 595 if (len < cb.length) { 596 len = cb.length; 597 } 598 599 bufferSize = cb.length + len; 600 char[] tmp = new char[bufferSize]; 601 602 System.arraycopy(cb, 0, tmp, 0, cb.length); 603 cb = tmp; 604 tmp = null; 605 606 } 607 608 609 } 610 | Popular Tags |