KickJava   Java API By Example, From Geeks To Geeks.

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


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.L10N;
34 import com.caucho.vfs.TempBuffer;
35
36 import javax.servlet.ServletOutputStream JavaDoc;
37 import javax.servlet.http.HttpServletResponse JavaDoc;
38 import java.io.IOException JavaDoc;
39 import java.io.PrintWriter JavaDoc;
40 import java.util.logging.Level JavaDoc;
41 import java.util.logging.Logger JavaDoc;
42
43 public class WrapperResponseStream extends AbstractResponseStream {
44   static final Logger JavaDoc 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 JavaDoc _next;
54   
55   private ServletOutputStream JavaDoc _os;
56   private PrintWriter JavaDoc _writer;
57
58   public WrapperResponseStream()
59   {
60   }
61
62   public void init(HttpServletResponse JavaDoc next)
63   {
64     if (next == null)
65       throw new NullPointerException JavaDoc();
66
67     _next = next;
68     _os = null;
69     _writer = null;
70   }
71
72   /**
73    * Returns true for a caucho response stream.
74    */

75   public boolean isCauchoResponseStream()
76   {
77     if (_next instanceof AbstractHttpResponse)
78       return ((AbstractHttpResponse) _next).isCauchoResponseStream();
79     else
80       return false;
81   }
82
83   /**
84    * Sets the buffer size.
85    */

86   public void setBufferSize(int size)
87   {
88     _next.setBufferSize(size);
89   }
90
91   /**
92    * Gets the buffer size.
93    */

94   public int getBufferSize()
95   {
96     return _next.getBufferSize();
97   }
98
99   /**
100    * Returns the remaining buffer entries.
101    */

102   public int getRemaining()
103   {
104     //return _next.getRemaining();
105
return 0;
106   }
107
108   /**
109    * Returns the char buffer.
110    */

111   public char []getCharBuffer()
112   {
113     return _charBuffer;
114   }
115
116   /**
117    * Returns the char buffer offset.
118    */

119   public int getCharOffset()
120   {
121     return 0;
122   }
123
124   /**
125    * Sets the char buffer offset.
126    */

127   public void setCharOffset(int offset)
128   {
129     if (offset > 0) {
130       try {
131     print(_charBuffer, 0, offset);
132       } catch (IOException JavaDoc e) {
133     log.log(Level.FINE, e.toString(), e);
134       }
135     }
136   }
137
138   /**
139    * Returns the next char buffer.
140    */

141   public char []nextCharBuffer(int offset)
142     throws IOException JavaDoc
143   {
144     if (offset > 0)
145       print(_charBuffer, 0, offset);
146
147     return _charBuffer;
148   }
149
150   /**
151    * Writes the next chunk of data to the response stream.
152    *
153    * @param buf the buffer containing the data
154    * @param offset start offset into the buffer
155    * @param length length of the data in the buffer
156    */

157   public void print(int ch)
158     throws IOException JavaDoc
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   /**
171    * Writes the next chunk of data to the response stream.
172    *
173    * @param buf the buffer containing the data
174    * @param offset start offset into the buffer
175    * @param length length of the data in the buffer
176    */

177   public void print(char []buffer, int offset, int length)
178     throws IOException JavaDoc
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   /**
194    * Returns the buffer offset.
195    */

196   public int getBufferOffset()
197   {
198     return 0;
199   }
200
201   /**
202    * Sets the byte buffer offset.
203    */

204   public void setBufferOffset(int offset)
205   {
206     if (offset > 0) {
207       try {
208     write(_byteBuffer, 0, offset);
209       } catch (IOException JavaDoc e) {
210     log.log(Level.FINE, e.toString(), e);
211       }
212     }
213   }
214
215   /**
216    * Gets the byte buffer
217    */

218   public byte []getBuffer()
219   {
220     return _byteBuffer;
221   }
222
223   /**
224    * Returns the next buffer.
225    */

226   public byte []nextBuffer(int offset)
227     throws IOException JavaDoc
228   {
229     if (offset > 0)
230       write(_byteBuffer, 0, offset);
231
232     return _byteBuffer;
233   }
234
235   /**
236    * Writes the next chunk of data to the response stream.
237    *
238    * @param buf the buffer containing the data
239    * @param offset start offset into the buffer
240    * @param length length of the data in the buffer
241    */

242   public void write(int ch)
243     throws IOException JavaDoc
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   /**
256    * Writes the next chunk of data to the response stream.
257    *
258    * @param buf the buffer containing the data
259    * @param offset start offset into the buffer
260    * @param length length of the data in the buffer
261    */

262   public void write(byte []buf, int offset, int length)
263     throws IOException JavaDoc
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   /**
279    * Writes the char buffer to the output stream.
280    */

281   public void flushCharBuffer()
282     throws IOException JavaDoc
283   {
284   }
285
286   /**
287    * Flushes the buffer.
288    */

289   public void flushBuffer()
290     throws IOException JavaDoc
291   {
292   }
293
294   /**
295    * Flushes the buffer.
296    */

297   public void flushChar()
298     throws IOException JavaDoc
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   /**
312    * Flushes the buffer.
313    */

314   public void flushByte()
315     throws IOException JavaDoc
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   /**
329    * Clears the buffer.
330    */

331   public void clearBuffer()
332   {
333     if (_next != null)
334       _next.resetBuffer();
335   }
336
337   /**
338    * Flushes the output.
339    */

340   public void flush()
341     throws IOException JavaDoc
342   {
343     if (_writer != null)
344       _writer.flush();
345
346     if (_os != null)
347       _os.flush();
348   }
349
350   /**
351    * Finish.
352    */

353   public void finish()
354     throws IOException JavaDoc
355   {
356     /*
357     if (_writer != null)
358       _writer.flush();
359     
360     if (_os != null)
361       _os.flush();
362     */

363
364     _next = null;
365     _os = null;
366     _writer = null;
367   }
368
369   /**
370    * Close.
371    */

372   public void close()
373     throws IOException JavaDoc
374   {
375     // jsp/17f0
376
PrintWriter JavaDoc writer = _writer;
377     _writer = null;
378
379     if (writer != null) {
380       try {
381     writer.close();
382       } catch (Throwable JavaDoc e) {
383     log.log(Level.FINER, e.toString(), e);
384       }
385     }
386
387     ServletOutputStream JavaDoc os = _os;
388     _os = null;
389     
390     if (os != null) {
391       try {
392     os.close();
393       } catch (Throwable JavaDoc e) {
394     log.log(Level.FINER, e.toString(), e);
395       }
396     }
397
398     _next = null;
399   }
400 }
401
Popular Tags