1 29 30 package com.caucho.server.connection; 31 32 import com.caucho.log.Log; 33 import com.caucho.util.L10N; 34 import com.caucho.vfs.TempBuffer; 35 36 import javax.servlet.ServletOutputStream ; 37 import javax.servlet.http.HttpServletResponse ; 38 import java.io.IOException ; 39 import java.io.PrintWriter ; 40 import java.util.logging.Level ; 41 import java.util.logging.Logger ; 42 43 public class WrapperResponseStream extends AbstractResponseStream { 44 static final Logger log = Log.open(WrapperResponseStream.class); 45 46 static final L10N L = new L10N(WrapperResponseStream.class); 47 48 private static final int SIZE = TempBuffer.SIZE; 49 50 private final byte []_byteBuffer = new byte[SIZE]; 51 private final char []_charBuffer = new char[SIZE]; 52 53 private HttpServletResponse _next; 54 55 private ServletOutputStream _os; 56 private PrintWriter _writer; 57 58 public WrapperResponseStream() 59 { 60 } 61 62 public void init(HttpServletResponse next) 63 { 64 if (next == null) 65 throw new NullPointerException (); 66 67 _next = next; 68 _os = null; 69 _writer = null; 70 } 71 72 75 public boolean isCauchoResponseStream() 76 { 77 if (_next instanceof AbstractHttpResponse) 78 return ((AbstractHttpResponse) _next).isCauchoResponseStream(); 79 else 80 return false; 81 } 82 83 86 public void setBufferSize(int size) 87 { 88 _next.setBufferSize(size); 89 } 90 91 94 public int getBufferSize() 95 { 96 return _next.getBufferSize(); 97 } 98 99 102 public int getRemaining() 103 { 104 return 0; 106 } 107 108 111 public char []getCharBuffer() 112 { 113 return _charBuffer; 114 } 115 116 119 public int getCharOffset() 120 { 121 return 0; 122 } 123 124 127 public void setCharOffset(int offset) 128 { 129 if (offset > 0) { 130 try { 131 print(_charBuffer, 0, offset); 132 } catch (IOException e) { 133 log.log(Level.FINE, e.toString(), e); 134 } 135 } 136 } 137 138 141 public char []nextCharBuffer(int offset) 142 throws IOException 143 { 144 if (offset > 0) 145 print(_charBuffer, 0, offset); 146 147 return _charBuffer; 148 } 149 150 157 public void print(int ch) 158 throws IOException 159 { 160 if (_writer == null) { 161 if (_next == null) 162 return; 163 164 _writer = _next.getWriter(); 165 } 166 167 _writer.write(ch); 168 } 169 170 177 public void print(char []buffer, int offset, int length) 178 throws IOException 179 { 180 if (length == 0) 181 return; 182 183 if (_writer == null) { 184 if (_next == null) 185 return; 186 187 _writer = _next.getWriter(); 188 } 189 190 _writer.write(buffer, offset, length); 191 } 192 193 196 public int getBufferOffset() 197 { 198 return 0; 199 } 200 201 204 public void setBufferOffset(int offset) 205 { 206 if (offset > 0) { 207 try { 208 write(_byteBuffer, 0, offset); 209 } catch (IOException e) { 210 log.log(Level.FINE, e.toString(), e); 211 } 212 } 213 } 214 215 218 public byte []getBuffer() 219 { 220 return _byteBuffer; 221 } 222 223 226 public byte []nextBuffer(int offset) 227 throws IOException 228 { 229 if (offset > 0) 230 write(_byteBuffer, 0, offset); 231 232 return _byteBuffer; 233 } 234 235 242 public void write(int ch) 243 throws IOException 244 { 245 if (_os == null) { 246 if (_next == null) 247 return; 248 249 _os = _next.getOutputStream(); 250 } 251 252 _os.write(ch); 253 } 254 255 262 public void write(byte []buf, int offset, int length) 263 throws IOException 264 { 265 if (length == 0) 266 return; 267 268 if (_os == null) { 269 if (_next == null) 270 return; 271 272 _os = _next.getOutputStream(); 273 } 274 275 _os.write(buf, offset, length); 276 } 277 278 281 public void flushCharBuffer() 282 throws IOException 283 { 284 } 285 286 289 public void flushBuffer() 290 throws IOException 291 { 292 } 293 294 297 public void flushChar() 298 throws IOException 299 { 300 if (_writer == null) { 301 if (_next == null) 302 return; 303 304 _writer = _next.getWriter(); 305 } 306 307 if (_writer != null) 308 _writer.flush(); 309 } 310 311 314 public void flushByte() 315 throws IOException 316 { 317 if (_os == null) { 318 if (_next == null) 319 return; 320 321 _os = _next.getOutputStream(); 322 } 323 324 if (_os != null) 325 _os.flush(); 326 } 327 328 331 public void clearBuffer() 332 { 333 if (_next != null) 334 _next.resetBuffer(); 335 } 336 337 340 public void flush() 341 throws IOException 342 { 343 if (_writer != null) 344 _writer.flush(); 345 346 if (_os != null) 347 _os.flush(); 348 } 349 350 353 public void finish() 354 throws IOException 355 { 356 363 364 _next = null; 365 _os = null; 366 _writer = null; 367 } 368 369 372 public void close() 373 throws IOException 374 { 375 PrintWriter writer = _writer; 377 _writer = null; 378 379 if (writer != null) { 380 try { 381 writer.close(); 382 } catch (Throwable e) { 383 log.log(Level.FINER, e.toString(), e); 384 } 385 } 386 387 ServletOutputStream os = _os; 388 _os = null; 389 390 if (os != null) { 391 try { 392 os.close(); 393 } catch (Throwable e) { 394 log.log(Level.FINER, e.toString(), e); 395 } 396 } 397 398 _next = null; 399 } 400 } 401 | Popular Tags |