1 17 18 package org.apache.jasper.runtime; 19 20 import java.io.IOException ; 21 import java.io.Writer ; 22 import java.security.AccessController ; 23 import java.security.PrivilegedAction ; 24 25 import javax.servlet.ServletResponse ; 26 import javax.servlet.jsp.JspWriter ; 27 28 import org.apache.jasper.Constants; 29 import org.apache.jasper.compiler.Localizer; 30 import org.apache.jasper.security.SecurityUtil; 31 32 45 public class JspWriterImpl extends JspWriter { 46 47 private Writer out; 48 private ServletResponse response; 49 private char cb[]; 50 private int nextChar; 51 private boolean flushed = false; 52 private boolean closed = false; 53 54 public JspWriterImpl() { 55 super( Constants.DEFAULT_BUFFER_SIZE, true ); 56 } 57 58 64 public JspWriterImpl(ServletResponse response) { 65 this(response, Constants.DEFAULT_BUFFER_SIZE, true); 66 } 67 68 77 public JspWriterImpl(ServletResponse response, int sz, 78 boolean autoFlush) { 79 super(sz, autoFlush); 80 if (sz < 0) 81 throw new IllegalArgumentException ("Buffer size <= 0"); 82 this.response = response; 83 cb = sz == 0 ? null : new char[sz]; 84 nextChar = 0; 85 } 86 87 void init( ServletResponse response, int sz, boolean autoFlush ) { 88 this.response= response; 89 if( sz > 0 && ( cb == null || sz > cb.length ) ) 90 cb=new char[sz]; 91 nextChar = 0; 92 this.autoFlush=autoFlush; 93 this.bufferSize=sz; 94 } 95 96 98 void recycle() { 99 flushed = false; 100 closed = false; 101 out = null; 102 nextChar = 0; 103 response = null; 104 } 105 106 111 protected final void flushBuffer() throws IOException { 112 if (bufferSize == 0) 113 return; 114 flushed = true; 115 ensureOpen(); 116 if (nextChar == 0) 117 return; 118 initOut(); 119 out.write(cb, 0, nextChar); 120 nextChar = 0; 121 } 122 123 private void initOut() throws IOException { 124 if (out == null) { 125 out = response.getWriter(); 126 } 127 } 128 129 private String getLocalizeMessage(final String message){ 130 if (SecurityUtil.isPackageProtectionEnabled()){ 131 return (String )AccessController.doPrivileged(new PrivilegedAction (){ 132 public Object run(){ 133 return Localizer.getMessage(message); 134 } 135 }); 136 } else { 137 return Localizer.getMessage(message); 138 } 139 } 140 141 144 public final void clear() throws IOException { 145 if ((bufferSize == 0) && (out != null)) 146 throw new IllegalStateException ( 148 getLocalizeMessage("jsp.error.ise_on_clear")); 149 if (flushed) 150 throw new IOException ( 151 getLocalizeMessage("jsp.error.attempt_to_clear_flushed_buffer")); 152 ensureOpen(); 153 nextChar = 0; 154 } 155 156 public void clearBuffer() throws IOException { 157 if (bufferSize == 0) 158 throw new IllegalStateException ( 159 getLocalizeMessage("jsp.error.ise_on_clear")); 160 ensureOpen(); 161 nextChar = 0; 162 } 163 164 private final void bufferOverflow() throws IOException { 165 throw new IOException (getLocalizeMessage("jsp.error.overflow")); 166 } 167 168 172 public void flush() throws IOException { 173 flushBuffer(); 174 if (out != null) { 175 out.flush(); 176 } 177 } 178 179 183 public void close() throws IOException { 184 if (response == null || closed) 185 return; 187 flush(); 188 if (out != null) 189 out.close(); 190 out = null; 191 closed = true; 192 } 193 194 197 public int getRemaining() { 198 return bufferSize - nextChar; 199 } 200 201 202 private void ensureOpen() throws IOException { 203 if (response == null || closed) 204 throw new IOException ("Stream closed"); 205 } 206 207 208 211 public void write(int c) throws IOException { 212 ensureOpen(); 213 if (bufferSize == 0) { 214 initOut(); 215 out.write(c); 216 } 217 else { 218 if (nextChar >= bufferSize) 219 if (autoFlush) 220 flushBuffer(); 221 else 222 bufferOverflow(); 223 cb[nextChar++] = (char) c; 224 } 225 } 226 227 231 private int min(int a, int b) { 232 if (a < b) return a; 233 return b; 234 } 235 236 250 public void write(char cbuf[], int off, int len) 251 throws IOException 252 { 253 ensureOpen(); 254 255 if (bufferSize == 0) { 256 initOut(); 257 out.write(cbuf, off, len); 258 return; 259 } 260 261 if ((off < 0) || (off > cbuf.length) || (len < 0) || 262 ((off + len) > cbuf.length) || ((off + len) < 0)) { 263 throw new IndexOutOfBoundsException (); 264 } else if (len == 0) { 265 return; 266 } 267 268 if (len >= bufferSize) { 269 272 if (autoFlush) 273 flushBuffer(); 274 else 275 bufferOverflow(); 276 initOut(); 277 out.write(cbuf, off, len); 278 return; 279 } 280 281 int b = off, t = off + len; 282 while (b < t) { 283 int d = min(bufferSize - nextChar, t - b); 284 System.arraycopy(cbuf, b, cb, nextChar, d); 285 b += d; 286 nextChar += d; 287 if (nextChar >= bufferSize) 288 if (autoFlush) 289 flushBuffer(); 290 else 291 bufferOverflow(); 292 } 293 294 } 295 296 300 public void write(char buf[]) throws IOException { 301 write(buf, 0, buf.length); 302 } 303 304 311 public void write(String s, int off, int len) throws IOException { 312 ensureOpen(); 313 if (bufferSize == 0) { 314 initOut(); 315 out.write(s, off, len); 316 return; 317 } 318 int b = off, t = off + len; 319 while (b < t) { 320 int d = min(bufferSize - nextChar, t - b); 321 s.getChars(b, b + d, cb, nextChar); 322 b += d; 323 nextChar += d; 324 if (nextChar >= bufferSize) 325 if (autoFlush) 326 flushBuffer(); 327 else 328 bufferOverflow(); 329 } 330 } 331 332 336 public void write(String s) throws IOException { 337 if(s == null) { 340 write(s, 0, 0); 341 } else { 342 write(s, 0, s.length()); 343 } 344 } 345 346 347 static String lineSeparator = System.getProperty("line.separator"); 348 349 356 357 public void newLine() throws IOException { 358 write(lineSeparator); 359 } 360 361 362 363 364 373 public void print(boolean b) throws IOException { 374 write(b ? "true" : "false"); 375 } 376 377 385 public void print(char c) throws IOException { 386 write(String.valueOf(c)); 387 } 388 389 398 public void print(int i) throws IOException { 399 write(String.valueOf(i)); 400 } 401 402 411 public void print(long l) throws IOException { 412 write(String.valueOf(l)); 413 } 414 415 424 public void print(float f) throws IOException { 425 write(String.valueOf(f)); 426 } 427 428 437 public void print(double d) throws IOException { 438 write(String.valueOf(d)); 439 } 440 441 451 public void print(char s[]) throws IOException { 452 write(s); 453 } 454 455 464 public void print(String s) throws IOException { 465 if (s == null) { 466 s = "null"; 467 } 468 write(s); 469 } 470 471 480 public void print(Object obj) throws IOException { 481 write(String.valueOf(obj)); 482 } 483 484 485 486 496 public void println() throws IOException { 497 newLine(); 498 } 499 500 505 public void println(boolean x) throws IOException { 506 print(x); 507 println(); 508 } 509 510 515 public void println(char x) throws IOException { 516 print(x); 517 println(); 518 } 519 520 525 public void println(int x) throws IOException { 526 print(x); 527 println(); 528 } 529 530 535 public void println(long x) throws IOException { 536 print(x); 537 println(); 538 } 539 540 545 public void println(float x) throws IOException { 546 print(x); 547 println(); 548 } 549 550 555 public void println(double x) throws IOException { 556 print(x); 557 println(); 558 } 559 560 565 public void println(char x[]) throws IOException { 566 print(x); 567 println(); 568 } 569 570 575 public void println(String x) throws IOException { 576 print(x); 577 println(); 578 } 579 580 585 public void println(Object x) throws IOException { 586 print(x); 587 println(); 588 } 589 590 } 591 | Popular Tags |