001 package railo.runtime.writer; 002 003 import java.io.IOException; 004 import java.io.OutputStream; 005 import java.io.Writer; 006 007 import javax.servlet.http.HttpServletResponse; 008 009 import railo.commons.lang.CharBuffer; 010 011 /** 012 * Implementation of a JSpWriter 013 */ 014 public class JspWriterImplCharBuffer extends CFMLWriter { 015 016 private static final int BUFFER_SIZE = 1024; 017 018 private Writer out; 019 020 private HttpServletResponse response; 021 022 private boolean flushed; 023 024 //private boolean closed; 025 026 private String headData; 027 028 //StringBuffer buffer=new StringBuffer(BUFFER_SIZE); 029 private CharBuffer buffer=new CharBuffer(BUFFER_SIZE); 030 031 private boolean closed=false; 032 033 /** 034 * constructor of the class 035 * @param response Response Object 036 * @param bufferSize buffer Size 037 * @param autoFlush do auto flush Content 038 */ 039 public JspWriterImplCharBuffer(HttpServletResponse response, int bufferSize, boolean autoFlush) { 040 super(bufferSize, autoFlush); 041 this.response=response; 042 this.autoFlush=autoFlush; 043 this.bufferSize=bufferSize; 044 //initBuffer(response.getCharacterEncoding()); 045 } 046 047 /** 048 * constructor of the class 049 * @param response Response Object 050 */ 051 public JspWriterImplCharBuffer(HttpServletResponse response) { 052 this(response, BUFFER_SIZE, false); 053 } 054 055 private void _check() throws IOException { 056 if(autoFlush && buffer.size()>bufferSize) { 057 _flush(); 058 } 059 } 060 061 /** 062 * @throws IOException 063 */ 064 protected void initOut() throws IOException { 065 if (out == null) { 066 out=response.getWriter(); 067 } 068 } 069 070 /*public void splitForCacheTo(Resource res) throws IOException { 071 initOut(); 072 out=new CacheWriter(out,res); 073 } 074 075 public void clearCacheSplit() { 076 if(out instanceof CacheWriter) { 077 CacheWriter cw = (CacheWriter)out; 078 out=cw.getOut(); 079 if(cw.getCacheFile().exists())cw.getCacheFile().delete(); 080 } 081 } */ 082 083 084 /** 085 * @see javax.servlet.jsp.JspWriter#print(char[]) 086 */ 087 public void print(char[] arg) throws IOException { 088 buffer.append(arg); 089 _check(); 090 } 091 092 /** 093 * reset configuration of buffer 094 * @param bufferSize size of the buffer 095 * @param autoFlush does the buffer autoflush 096 * @throws IOException 097 */ 098 public void setBufferConfig(int bufferSize, boolean autoFlush) throws IOException { 099 this.bufferSize=bufferSize; 100 this.autoFlush=autoFlush; 101 _check(); 102 } 103 104 /** 105 * 106 * @param headData 107 * @throws IOException 108 */ 109 public void appendHTMLHead(String headData) throws IOException { 110 if(!flushed) { 111 if(this.headData==null)this.headData=headData; 112 else this.headData+=headData; 113 } 114 else throw new IOException("page already flushed"); 115 } 116 117 /** 118 * @see railo.runtime.writer.CFMLWriter#writeHTMLHead(java.lang.String) 119 */ 120 public void writeHTMLHead(String headData) throws IOException { 121 if(!flushed) { 122 this.headData=headData; 123 } 124 else throw new IOException("page already flushed"); 125 } 126 127 128 129 /** 130 * @see railo.runtime.writer.CFMLWriter#getHTMLHead() 131 */ 132 public String getHTMLHead() throws IOException { 133 if(flushed) throw new IOException("page already flushed"); 134 return headData==null?"":headData; 135 } 136 137 /** 138 * @see railo.runtime.writer.CFMLWriter#resetHTMLHead() 139 */ 140 public void resetHTMLHead() throws IOException { 141 if(flushed) throw new IOException("page already flushed"); 142 headData=null; 143 } 144 145 /** 146 * just a wrapper function for ACF 147 * @throws IOException 148 */ 149 public void initHeaderBuffer() throws IOException{ 150 resetHTMLHead(); 151 } 152 153 154 /** 155 * @see java.io.Writer#write(char[], int, int) 156 */ 157 public void write(char[] cbuf, int off, int len) throws IOException { 158 buffer.append(cbuf,off,len); 159 _check(); 160 } 161 162 /** 163 * @see javax.servlet.jsp.JspWriter#clear() 164 */ 165 public void clear() throws IOException { 166 if (flushed) throw new IOException("jsp writer is already flushed"); 167 clearBuffer(); 168 } 169 170 /** 171 * @see javax.servlet.jsp.JspWriter#clearBuffer() 172 */ 173 public void clearBuffer() { 174 //buffer.clear(); 175 buffer=new CharBuffer(BUFFER_SIZE); 176 } 177 178 /** 179 * @see java.io.Writer#flush() 180 */ 181 public void flush() throws IOException { 182 flushBuffer(); 183 // weil flushbuffer das out erstellt muss ich nicht mehr checken 184 out.flush(); 185 } 186 /** 187 * @see java.io.Writer#flush() 188 */ 189 private void _flush() throws IOException { 190 flushBuffer(); 191 // weil flushbuffer das out erstellt muss ich nicht mehr checken 192 out.flush(); 193 } 194 195 /** 196 * Flush the output buffer to the underlying character stream, without 197 * flushing the stream itself. This method is non-private only so that it 198 * may be invoked by PrintStream. 199 * @throws IOException 200 */ 201 protected final void flushBuffer() throws IOException { 202 flushed = true; 203 initOut(); 204 //out.write(__toString()); 205 _writeOut(out); 206 clearBuffer(); 207 } 208 209 private void _writeOut(Writer writer) throws IOException { 210 if(headData==null) { 211 headData=null; 212 buffer.writeOut(writer); 213 return; 214 //return buffer.toString(); 215 } 216 String str=buffer.toString(); 217 int index=str.indexOf("</head>"); 218 if(index==-1) { 219 str= headData.concat(str); 220 headData=null; 221 writer.write(str); 222 return; 223 //return str; 224 } 225 str= str.substring(0,index).concat(headData).concat(str.substring(index)); 226 headData=null; 227 writer.write(str); 228 //return str; 229 } 230 231 /** 232 * @see java.io.Writer#close() 233 */ 234 public void close() throws IOException { 235 if (response == null || closed) return; 236 if(out==null) { 237 //response.setContentLength(buffer.size()); 238 out=response.getWriter(); 239 _writeOut(out); 240 241 242 //byte[] barr = _toString().getBytes(response.getCharacterEncoding()); 243 //response.setContentLength(barr.length); 244 //ServletOutputStream os = response.getOutputStream(); 245 //os.write(barr); 246 out.flush(); 247 out.close(); 248 } 249 else { 250 flush(); 251 out.close(); 252 out = null; 253 } 254 closed = true; 255 } 256 257 /** 258 * @see javax.servlet.jsp.JspWriter#getRemaining() 259 */ 260 public int getRemaining() { 261 return bufferSize - buffer.size(); 262 } 263 264 /** 265 * @see javax.servlet.jsp.JspWriter#newLine() 266 */ 267 public void newLine() throws IOException { 268 println(); 269 } 270 271 /** 272 * @see javax.servlet.jsp.JspWriter#print(boolean) 273 */ 274 public void print(boolean arg) throws IOException { 275 print(arg?new char[]{'t','r','u','e'}:new char[]{'f','a','l','s','e'}); 276 } 277 278 /** 279 * @see javax.servlet.jsp.JspWriter#print(char) 280 */ 281 public void print(char arg) throws IOException { 282 buffer.append(arg); 283 _check(); 284 } 285 286 /** 287 * @see javax.servlet.jsp.JspWriter#print(int) 288 */ 289 public void print(int arg) throws IOException { 290 _print(String.valueOf(arg)); 291 } 292 293 /** 294 * @see javax.servlet.jsp.JspWriter#print(long) 295 */ 296 public void print(long arg) throws IOException { 297 _print(String.valueOf(arg)); 298 299 } 300 301 /** 302 * @see javax.servlet.jsp.JspWriter#print(float) 303 */ 304 public void print(float arg) throws IOException { 305 _print(String.valueOf(arg)); 306 } 307 308 /** 309 * @see javax.servlet.jsp.JspWriter#print(double) 310 */ 311 public void print(double arg) throws IOException { 312 _print(String.valueOf(arg)); 313 } 314 315 /** 316 * @see javax.servlet.jsp.JspWriter#print(java.lang.String) 317 */ 318 public void print(String arg) throws IOException { 319 buffer.append(arg); 320 _check(); 321 } 322 323 /** 324 * @see javax.servlet.jsp.JspWriter#print(java.lang.Object) 325 */ 326 public void print(Object arg) throws IOException { 327 _print(String.valueOf(arg)); 328 } 329 330 /** 331 * @see javax.servlet.jsp.JspWriter#println() 332 */ 333 public void println() throws IOException { 334 _print("\n"); 335 336 } 337 338 /** 339 * @see javax.servlet.jsp.JspWriter#println(boolean) 340 */ 341 public void println(boolean arg) throws IOException { 342 print(arg?new char[]{'t','r','u','e','\n'}:new char[]{'f','a','l','s','e','\n'}); 343 } 344 345 /** 346 * @see javax.servlet.jsp.JspWriter#println(char) 347 */ 348 public void println(char arg) throws IOException { 349 print(new char[]{arg,'\n'}); 350 } 351 352 /** 353 * @see javax.servlet.jsp.JspWriter#println(int) 354 */ 355 public void println(int arg) throws IOException { 356 print(arg); 357 println(); 358 } 359 360 /** 361 * @see javax.servlet.jsp.JspWriter#println(long) 362 */ 363 public void println(long arg) throws IOException { 364 print(arg); 365 println(); 366 } 367 368 /** 369 * @see javax.servlet.jsp.JspWriter#println(float) 370 */ 371 public void println(float arg) throws IOException { 372 print(arg); 373 println(); 374 } 375 376 /** 377 * @see javax.servlet.jsp.JspWriter#println(double) 378 */ 379 public void println(double arg) throws IOException { 380 print(arg); 381 println(); 382 } 383 384 /** 385 * @see javax.servlet.jsp.JspWriter#println(char[]) 386 */ 387 public void println(char[] arg) throws IOException { 388 print(arg); 389 println(); 390 } 391 392 /** 393 * @see javax.servlet.jsp.JspWriter#println(java.lang.String) 394 */ 395 public void println(String arg) throws IOException { 396 _print(arg); 397 println(); 398 } 399 400 /** 401 * @see javax.servlet.jsp.JspWriter#println(java.lang.Object) 402 */ 403 public void println(Object arg) throws IOException { 404 print(arg); 405 println(); 406 } 407 408 /** 409 * @see java.io.Writer#write(char[]) 410 */ 411 public void write(char[] cbuf) throws IOException { 412 print(cbuf); 413 } 414 415 /** 416 * @see java.io.Writer#write(int) 417 */ 418 public void write(int c) throws IOException { 419 print(c); 420 } 421 422 /** 423 * @see java.io.Writer#write(java.lang.String, int, int) 424 */ 425 public void write(String str, int off, int len) throws IOException { 426 write(str.toCharArray(),off,len); 427 } 428 429 /** 430 * @see java.io.Writer#write(java.lang.String) 431 */ 432 public void write(String str) throws IOException { 433 _print(str); 434 } 435 436 /** 437 * @return Returns the flushed. 438 */ 439 public boolean isFlushed() { 440 return flushed; 441 } 442 443 public void setClosed(boolean closed) { 444 this.closed=closed; 445 } 446 447 private void _print(String arg) throws IOException { 448 buffer.append(arg); 449 _check(); 450 } 451 452 /** 453 * @see railo.runtime.writer.CFMLWriter#getResponseStream() 454 */ 455 public OutputStream getResponseStream() throws IOException { 456 return response.getOutputStream(); 457 } 458 459 /** 460 * @see railo.runtime.writer.CFMLWriter#writeRaw(java.lang.String) 461 */ 462 public void writeRaw(String str) throws IOException { 463 write(str); 464 } 465 466 467 }