KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > connection > ToCharResponseAdapter


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.server.connection;
31
32 import com.caucho.log.Log;
33 import com.caucho.util.FreeList;
34
35 import javax.servlet.http.HttpServletResponse JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.util.logging.Logger JavaDoc;
38
39 public class ToCharResponseAdapter extends ResponseAdapter {
40   private static final Logger JavaDoc log = Log.open(ToCharResponseAdapter.class);
41   
42   private static final FreeList<ToCharResponseAdapter> _freeList =
43     new FreeList<ToCharResponseAdapter>(32);
44
45   private ToCharResponseStreamWrapper _responseStream;
46
47   private ToCharResponseAdapter(HttpServletResponse JavaDoc response)
48   {
49     super(response);
50   }
51
52   /**
53    * Creates a new ResponseAdapter.
54    */

55   public static ToCharResponseAdapter create(HttpServletResponse JavaDoc response)
56   {
57     ToCharResponseAdapter resAdapt = _freeList.allocate();
58
59     if (resAdapt == null)
60       resAdapt = new ToCharResponseAdapter(response);
61     else
62       resAdapt.setResponse(response);
63
64     resAdapt.init(response);
65
66     return resAdapt;
67   }
68
69   protected AbstractResponseStream createWrapperResponseStream()
70   {
71     if (_responseStream == null)
72       _responseStream = new ToCharResponseStreamWrapper();
73
74     return _responseStream;
75   }
76
77   public void init(HttpServletResponse JavaDoc response)
78   {
79     _responseStream.start();
80     
81     super.init(response);
82   }
83
84   public void resetBuffer()
85   {
86     _responseStream.clearBuffer();
87
88     super.resetBuffer();
89
90     /*
91     if (_currentWriter instanceof JspPrintWriter)
92       ((JspPrintWriter) _currentWriter).clear();
93     */

94   }
95
96   public static void free(ToCharResponseAdapter resAdapt)
97   {
98     resAdapt.free();
99
100     _freeList.free(resAdapt);
101   }
102
103   class ToCharResponseStreamWrapper extends ToCharResponseStream {
104     protected String JavaDoc getEncoding()
105     {
106       return getResponse().getCharacterEncoding();
107     }
108
109     /**
110      * Flushes the buffer.
111      */

112     public void flushChar()
113       throws IOException JavaDoc
114     {
115       flushBuffer();
116       
117       getResponse().getWriter().flush();
118     }
119
120     /**
121      * Flushes the buffer.
122      */

123     public void close()
124       throws IOException JavaDoc
125     {
126       // jsp/1730
127
flushBuffer();
128
129       // server/172q
130
// getResponse().getWriter().close();
131
}
132     
133     protected void writeNext(char []buffer, int offset, int length)
134       throws IOException JavaDoc
135     {
136       getResponse().getWriter().write(buffer, offset, length);
137     }
138   }
139 }
140
Popular Tags