1 29 30 package com.caucho.jsp; 31 32 import com.caucho.log.Log; 33 import com.caucho.util.CharBuffer; 34 import com.caucho.util.FreeList; 35 import com.caucho.util.L10N; 36 import com.caucho.vfs.TempCharBuffer; 37 import com.caucho.vfs.TempCharReader; 38 import com.caucho.vfs.TempCharStream; 39 40 import javax.servlet.jsp.JspWriter ; 41 import java.io.IOException ; 42 import java.io.PrintWriter ; 43 import java.io.Reader ; 44 import java.io.Writer ; 45 import java.util.logging.Logger ; 46 47 50 public class BodyContentImpl extends AbstractBodyContent { 51 static final L10N L = new L10N(BodyContentImpl.class); 52 static final Logger log = Log.open(BodyContentImpl.class); 53 54 private static final FreeList<BodyContentImpl> _freeList 55 = new FreeList<BodyContentImpl>(32); 56 57 private TempCharStream _tempStream = new TempCharStream(); 58 private TempCharReader _charReader; 59 private JspPrintWriter _printWriter; 60 private JspWriter _prev; 61 62 67 private BodyContentImpl(JspWriter prev) 68 { 69 setParent(prev); 70 } 71 72 75 static BodyContentImpl allocate() 76 { 77 BodyContentImpl body = (BodyContentImpl) _freeList.allocate(); 78 79 if (body == null) 80 body = new BodyContentImpl(null); 81 82 return body; 83 } 84 85 90 void init(JspWriter prev) 91 { 92 setParent(prev); 93 94 _tempStream.openWrite(); 95 } 96 97 104 final public void write(char []buf, int off, int len) throws IOException 105 { 106 _tempStream.write(buf, off, len); 107 } 108 109 116 final public void write(String s, int off, int len) throws IOException 117 { 118 _tempStream.write(s, off, len); 119 } 120 121 126 final public void write(int ch) throws IOException 127 { 128 _tempStream.write(ch); 129 } 130 131 final public void clear() throws IOException 132 { 133 _tempStream.clearWrite(); 134 } 135 136 final public void clearBuffer() throws IOException 137 { 138 clear(); 139 } 140 141 final public void flush() throws IOException 142 { 143 throw new IOException (L.l("flush() may not be called in a body")); 145 } 146 147 final public void close() throws IOException 148 { 149 } 150 151 final public int getBufferSize() 152 { 153 return -1; 154 } 155 156 final public int getRemaining() 157 { 158 return 0; 159 } 160 161 164 public void clearBody() 165 { 166 _tempStream.clearWrite(); 167 } 168 169 172 public Reader getReader() 173 { 174 _charReader = new TempCharReader(); 175 _charReader.init(_tempStream.getHead()); 176 177 return _charReader; 178 } 179 180 public CharBuffer getCharBuffer() 181 { 182 CharBuffer cb = new CharBuffer(); 183 184 TempCharBuffer head = _tempStream.getHead(); 185 186 for (; head != null; head = head.getNext()) { 187 char []cbuf = head.getBuffer(); 188 189 cb.append(cbuf, 0, head.getLength()); 190 } 191 192 return cb; 193 } 194 195 198 public String getString() 199 { 200 TempCharBuffer head = _tempStream.getHead(); 201 202 if (head == null) 203 return ""; 204 205 if (head.getNext() == null) 206 return new String (head.getBuffer(), 0, head.getLength()); 207 208 int length = 0; 209 for (; head != null; head = head.getNext()) 210 length += head.getLength(); 211 212 char []buf = new char[length]; 213 214 int offset = 0; 215 for (head = _tempStream.getHead(); head != null; head = head.getNext()) { 216 char []cbuf = head.getBuffer(); 217 int sublen = head.getLength(); 218 219 System.arraycopy(cbuf, 0, buf, offset, sublen); 220 221 offset += sublen; 222 } 223 224 return new String (buf, 0, length); 225 } 226 227 230 public String getTrimString() 231 { 232 TempCharBuffer head = _tempStream.getHead(); 233 234 boolean hasData = false; 235 236 char []buf = null; 237 int totalLength = 0; 238 for (; head != null; head = head.getNext()) { 239 char []cbuf = head.getBuffer(); 240 int end = head.getLength(); 241 int offset = 0; 242 243 if (! hasData) { 244 for (offset = 0; offset < end; offset++) { 245 if (! Character.isWhitespace(cbuf[offset])) { 246 hasData = true; 247 break; 248 } 249 } 250 } 251 252 if (head.getNext() == null) { 253 for (; offset < end; end--) { 254 if (! Character.isWhitespace(cbuf[end - 1])) 255 break; 256 } 257 258 if (buf != null) { 259 System.arraycopy(cbuf, offset, buf, totalLength, end - offset); 260 totalLength += end - offset; 261 262 return new String (buf, 0, totalLength); 263 } 264 else if (offset == end) 265 return ""; 266 else 267 return new String (cbuf, offset, end - offset); 268 269 } 270 else if (buf == null) { 271 int length = 0; 272 273 for (TempCharBuffer ptr = head; ptr != null; ptr = ptr.getNext()) 274 length += ptr.getLength(); 275 276 buf = new char[length]; 277 278 System.arraycopy(cbuf, offset, buf, 0, end - offset); 279 totalLength += end - offset; 280 } 281 else { 282 System.arraycopy(cbuf, offset, buf, totalLength, end - offset); 283 totalLength += end - offset; 284 } 285 } 286 287 return ""; 288 } 289 290 293 public void writeOut(Writer out) throws IOException 294 { 295 try { 296 TempCharBuffer head = _tempStream.getHead(); 297 298 for (; head != null; head = head.getNext()) { 299 char []cbuf = head.getBuffer(); 300 301 out.write(cbuf, 0, head.getLength()); 302 } 303 } catch (IOException e) { 304 } 305 } 306 307 310 public void flushBuffer() throws IOException 311 { 312 } 313 314 317 public PrintWriter getWriter() 318 { 319 if (_printWriter == null) 320 _printWriter = new JspPrintWriter(this); 321 322 _printWriter.init(this); 323 324 return _printWriter; 325 } 326 327 330 public void release() 331 { 332 releaseNoFree(); 333 334 _freeList.free(this); 335 } 336 337 void releaseNoFree() 338 { 339 if (_charReader != null && ! _charReader.isEmpty()) { 340 _charReader.setFree(true); 341 _tempStream.discard(); 342 } 343 else 344 _tempStream.destroy(); 345 346 _charReader = null; 347 _prev = null; 348 } 349 350 AbstractJspWriter popWriter() 351 { 352 AbstractJspWriter parent = super.popWriter(); 353 354 release(); 355 356 return parent; 357 } 358 } 359 | Popular Tags |