1 29 30 package com.caucho.jsp; 31 32 import com.caucho.server.connection.AbstractResponseStream; 33 import com.caucho.util.L10N; 34 35 import javax.servlet.jsp.JspWriter ; 36 import java.io.IOException ; 37 import java.util.logging.Level ; 38 39 42 public class JspWriterAdapter extends AbstractBodyContent { 43 private static final L10N L = new L10N(JspWriterAdapter.class); 44 45 private JspWriter _parent; 47 48 private AbstractResponseStream _out; 50 51 private PageContextImpl _pageContext; 53 54 private boolean _isClosed; 55 56 59 JspWriterAdapter() 60 { 61 } 62 63 68 void init(PageContextImpl pageContext) 69 { 70 _pageContext = pageContext; 71 _out = null; 72 _isClosed = false; 73 } 74 75 80 void init(JspWriter parent, AbstractResponseStream out) 81 { 82 _parent = parent; 83 _out = out; 84 _isClosed = false; 85 } 86 87 94 final public void write(char []buf, int offset, int length) 95 throws IOException 96 { 97 if (_isClosed) 98 throw new IOException (L.l("write() forbidden after writer is closed")); 99 100 _out.print(buf, offset, length); 101 } 102 103 108 final public void write(int ch) throws IOException 109 { 110 if (_isClosed) { 111 if (Character.isWhitespace(ch)) 112 return; 113 114 throw new IOException (L.l("write() forbidden after writer is closed")); 115 } 116 117 _out.print(ch); 118 } 119 120 123 final public void println() throws IOException 124 { 125 if (_isClosed) { 126 throw new IOException (L.l("write() forbidden after writer is closed")); 127 } 128 129 _out.print('\n'); 130 } 131 132 135 final public void write(String s, int off, int len) 136 throws IOException 137 { 138 if (_isClosed) 139 throw new IOException (L.l("write() forbidden after writer is closed")); 140 141 char []writeBuffer = _out.getCharBuffer(); 142 int size = writeBuffer.length; 143 int writeLength = _out.getCharOffset(); 144 int end = off + len; 145 146 while (off < end) { 147 int sublen = end - off; 148 149 if (size - writeLength < sublen) { 150 if (size == writeLength) { 151 writeBuffer = _out.nextCharBuffer(writeLength); 152 writeLength = 0; 153 154 if (size < sublen) 155 sublen = size; 156 } 157 else 158 sublen = size - writeLength; 159 } 160 161 int tail = off + sublen; 162 s.getChars(off, tail, writeBuffer, writeLength); 163 164 off = tail; 165 writeLength += sublen; 166 } 167 168 _out.setCharOffset(writeLength); 169 } 170 171 174 public int getBufferSize() 175 { 176 return _out.getBufferSize(); 177 } 178 179 182 public int getRemaining() 183 { 184 return _out.getRemaining(); 185 } 186 187 public void clear() throws IOException 188 { 189 if (_isClosed) 190 throw new IOException (L.l("clear() forbidden after writer is closed")); 191 197 198 _out.clear(); 199 } 201 202 public void clearBuffer() throws IOException 203 { 204 if (_isClosed) 205 return; 206 207 _out.clearBuffer(); 208 } 209 210 public void flushBuffer() 211 throws IOException 212 { 213 if (_isClosed) 214 throw new IOException (L.l("flushBuffer() forbidden after writer is closed")); 215 216 _out.flushBuffer(); 217 } 218 219 222 public void flush() throws IOException 223 { 224 if (_isClosed) 225 throw new IOException (L.l("flush() forbidden after writer is closed")); 226 227 _out.flushChar(); 228 } 229 230 233 AbstractJspWriter popWriter() 234 { 235 try { 236 close(); 237 } catch (Throwable e) { 238 log.log(Level.FINER, e.toString(), e); 239 } 240 241 return super.popWriter(); 242 } 243 244 final public void close() throws IOException 245 { 246 _isClosed = true; 247 248 _out = null; 249 _parent = null; 250 _pageContext = null; 251 } 252 } 253 | Popular Tags |