KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > coyote > http11 > filters > IdentityOutputFilter


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.coyote.http11.filters;
19
20 import java.io.IOException JavaDoc;
21
22 import org.apache.tomcat.util.buf.ByteChunk;
23
24 import org.apache.coyote.OutputBuffer;
25 import org.apache.coyote.Response;
26 import org.apache.coyote.http11.OutputFilter;
27
28 /**
29  * Identity output filter.
30  *
31  * @author Remy Maucherat
32  */

33 public class IdentityOutputFilter implements OutputFilter {
34
35
36     // -------------------------------------------------------------- Constants
37

38
39     protected static final String JavaDoc ENCODING_NAME = "identity";
40     protected static final ByteChunk ENCODING = new ByteChunk();
41
42
43     // ----------------------------------------------------- Static Initializer
44

45
46     static {
47         ENCODING.setBytes(ENCODING_NAME.getBytes(), 0, ENCODING_NAME.length());
48     }
49
50
51     // ----------------------------------------------------- Instance Variables
52

53
54     /**
55      * Content length.
56      */

57     protected long contentLength = -1;
58
59
60     /**
61      * Remaining bytes.
62      */

63     protected long remaining = 0;
64
65
66     /**
67      * Next buffer in the pipeline.
68      */

69     protected OutputBuffer buffer;
70
71
72     // ------------------------------------------------------------- Properties
73

74
75     /**
76      * Get content length.
77      */

78     public long getContentLength() {
79         return contentLength;
80     }
81
82
83     /**
84      * Get remaining bytes.
85      */

86     public long getRemaining() {
87         return remaining;
88     }
89
90
91     // --------------------------------------------------- OutputBuffer Methods
92

93
94     /**
95      * Write some bytes.
96      *
97      * @return number of bytes written by the filter
98      */

99     public int doWrite(ByteChunk chunk, Response res)
100         throws IOException JavaDoc {
101
102         int result = -1;
103
104         if (contentLength >= 0) {
105             if (remaining > 0) {
106                 result = chunk.getLength();
107                 if (result > remaining) {
108                     // The chunk is longer than the number of bytes remaining
109
// in the body; changing the chunk length to the number
110
// of bytes remaining
111
chunk.setBytes(chunk.getBytes(), chunk.getStart(),
112                                    (int) remaining);
113                     result = (int) remaining;
114                     remaining = 0;
115                 } else {
116                     remaining = remaining - result;
117                 }
118                 buffer.doWrite(chunk, res);
119             } else {
120                 // No more bytes left to be written : return -1 and clear the
121
// buffer
122
chunk.recycle();
123                 result = -1;
124             }
125         } else {
126             // If no content length was set, just write the bytes
127
buffer.doWrite(chunk, res);
128             result = chunk.getLength();
129         }
130
131         return result;
132
133     }
134
135
136     // --------------------------------------------------- OutputFilter Methods
137

138
139     /**
140      * Some filters need additional parameters from the response. All the
141      * necessary reading can occur in that method, as this method is called
142      * after the response header processing is complete.
143      */

144     public void setResponse(Response response) {
145         contentLength = response.getContentLengthLong();
146         remaining = contentLength;
147     }
148
149
150     /**
151      * Set the next buffer in the filter pipeline.
152      */

153     public void setBuffer(OutputBuffer buffer) {
154         this.buffer = buffer;
155     }
156
157
158     /**
159      * End the current request. It is acceptable to write extra bytes using
160      * buffer.doWrite during the execution of this method.
161      */

162     public long end()
163         throws IOException JavaDoc {
164
165         if (remaining > 0)
166             return remaining;
167         return 0;
168
169     }
170
171
172     /**
173      * Make the filter ready to process the next request.
174      */

175     public void recycle() {
176         contentLength = -1;
177         remaining = 0;
178     }
179
180
181     /**
182      * Return the name of the associated encoding; Here, the value is
183      * "identity".
184      */

185     public ByteChunk getEncodingName() {
186         return ENCODING;
187     }
188
189
190 }
191
Popular Tags