001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.server.connection;
031:
032: import com.caucho.log.Log;
033: import com.caucho.util.FreeList;
034:
035: import javax.servlet.http.HttpServletResponse;
036: import java.io.IOException;
037: import java.util.logging.Logger;
038:
039: public class ToCharResponseAdapter extends ResponseAdapter {
040: private static final Logger log = Log
041: .open(ToCharResponseAdapter.class);
042:
043: private static final FreeList<ToCharResponseAdapter> _freeList = new FreeList<ToCharResponseAdapter>(
044: 32);
045:
046: private ToCharResponseStreamWrapper _responseStream;
047:
048: private ToCharResponseAdapter(HttpServletResponse response) {
049: super (response);
050: }
051:
052: /**
053: * Creates a new ResponseAdapter.
054: */
055: public static ToCharResponseAdapter create(
056: HttpServletResponse response) {
057: ToCharResponseAdapter resAdapt = _freeList.allocate();
058:
059: if (resAdapt == null)
060: resAdapt = new ToCharResponseAdapter(response);
061: else
062: resAdapt.setResponse(response);
063:
064: resAdapt.init(response);
065:
066: return resAdapt;
067: }
068:
069: protected AbstractResponseStream createWrapperResponseStream() {
070: if (_responseStream == null)
071: _responseStream = new ToCharResponseStreamWrapper();
072:
073: return _responseStream;
074: }
075:
076: @Override
077: public void init(HttpServletResponse response) {
078: _responseStream.start();
079:
080: super .init(response);
081: }
082:
083: public void resetBuffer() {
084: _responseStream.clearBuffer();
085:
086: super .resetBuffer();
087:
088: /*
089: if (_currentWriter instanceof JspPrintWriter)
090: ((JspPrintWriter) _currentWriter).clear();
091: */
092: }
093:
094: public static void free(ToCharResponseAdapter resAdapt) {
095: resAdapt.free();
096:
097: _freeList.free(resAdapt);
098: }
099:
100: class ToCharResponseStreamWrapper extends ToCharResponseStream {
101: protected String getEncoding() {
102: return getResponse().getCharacterEncoding();
103: }
104:
105: /**
106: * Flushes the buffer.
107: */
108: public void flushChar() throws IOException {
109: flushBuffer();
110:
111: getResponse().getWriter().flush();
112: }
113:
114: /**
115: * Flushes the buffer.
116: */
117: public void close() throws IOException {
118: // jsp/1730
119: flushBuffer();
120:
121: // server/172q
122: // getResponse().getWriter().close();
123: }
124:
125: protected void writeNext(char[] buffer, int offset, int length)
126: throws IOException {
127: getResponse().getWriter().write(buffer, offset, length);
128: }
129: }
130: }
|