KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > filters > FilterWrapperResponseStream


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.filters;
30
31 import com.caucho.log.Log;
32 import com.caucho.server.connection.ToByteResponseStream;
33 import com.caucho.util.L10N;
34
35 import java.io.IOException JavaDoc;
36 import java.io.OutputStream JavaDoc;
37 import java.util.logging.Logger JavaDoc;
38
39 public class FilterWrapperResponseStream extends ToByteResponseStream {
40   static final Logger JavaDoc log = Log.open(FilterWrapperResponseStream.class);
41   
42   static final L10N L = new L10N(FilterWrapperResponseStream.class);
43
44   private CauchoResponseWrapper _response;
45   
46   private OutputStream JavaDoc _os;
47   
48   public FilterWrapperResponseStream()
49   {
50   }
51
52   public void init(CauchoResponseWrapper response)
53   {
54     _response = response;
55     _os = null;
56   }
57
58   /**
59    * Writes the next chunk of data to the response stream.
60    *
61    * @param buf the buffer containing the data
62    * @param offset start offset into the buffer
63    * @param length length of the data in the buffer
64    */

65   protected void writeNext(byte []buf, int offset, int length, boolean isEnd)
66     throws IOException JavaDoc
67   {
68     OutputStream JavaDoc os = getStream();
69     
70     if (os != null)
71       os.write(buf, offset, length);
72   }
73
74   /**
75    * flushing
76    */

77   public void flush()
78     throws IOException JavaDoc
79   {
80     flushBuffer();
81
82     OutputStream JavaDoc os = getStream();
83     if (os != null)
84       os.flush();
85   }
86
87   /**
88    * Gets the stream.
89    */

90   private OutputStream JavaDoc getStream()
91     throws IOException JavaDoc
92   {
93     if (_os != null)
94       return _os;
95     else if (_response != null)
96       _os = _response.getStream();
97
98     return _os;
99   }
100
101   /**
102    * Finish.
103    */

104   public void finish()
105     throws IOException JavaDoc
106   {
107     flushBuffer();
108
109     /*
110     if (_os != null)
111       _os.flush();
112     */

113
114     _response = null;
115     _os = null;
116   }
117
118   /**
119    * Close.
120    */

121   public void close()
122     throws IOException JavaDoc
123   {
124     super.close();
125
126     _response = null;
127     
128     OutputStream JavaDoc os = _os;
129     _os = null;
130     // server/1839
131

132     if (os != null)
133       os.close();
134   }
135 }
136
Popular Tags