KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.apache.tomcat.util.buf.HexUtils;
24
25 import org.apache.coyote.OutputBuffer;
26 import org.apache.coyote.Response;
27 import org.apache.coyote.http11.OutputFilter;
28
29 /**
30  * Chunked output filter.
31  *
32  * @author Remy Maucherat
33  */

34 public class ChunkedOutputFilter implements OutputFilter {
35
36
37     // -------------------------------------------------------------- Constants
38

39
40     protected static final String JavaDoc ENCODING_NAME = "chunked";
41     protected static final ByteChunk ENCODING = new ByteChunk();
42
43
44     /**
45      * End chunk.
46      */

47     protected static final ByteChunk END_CHUNK = new ByteChunk();
48
49
50     // ----------------------------------------------------- Static Initializer
51

52
53     static {
54         ENCODING.setBytes(ENCODING_NAME.getBytes(), 0, ENCODING_NAME.length());
55         byte[] END_CHUNK_BYTES = {(byte) '0', (byte) '\r', (byte) '\n',
56                                   (byte) '\r', (byte) '\n'};
57         END_CHUNK.setBytes(END_CHUNK_BYTES, 0, END_CHUNK_BYTES.length);
58     }
59
60
61     // ------------------------------------------------------------ Constructor
62

63
64     /**
65      * Default constructor.
66      */

67     public ChunkedOutputFilter() {
68         chunkLength = new byte[10];
69         chunkLength[8] = (byte) '\r';
70         chunkLength[9] = (byte) '\n';
71     }
72
73
74     // ----------------------------------------------------- Instance Variables
75

76
77     /**
78      * Next buffer in the pipeline.
79      */

80     protected OutputBuffer buffer;
81
82
83     /**
84      * Buffer used for chunk length conversion.
85      */

86     protected byte[] chunkLength = new byte[10];
87
88
89     /**
90      * Chunk header.
91      */

92     protected ByteChunk chunkHeader = new ByteChunk();
93
94
95     // ------------------------------------------------------------- Properties
96

97
98     // --------------------------------------------------- OutputBuffer Methods
99

100
101     /**
102      * Write some bytes.
103      *
104      * @return number of bytes written by the filter
105      */

106     public int doWrite(ByteChunk chunk, Response res)
107         throws IOException JavaDoc {
108
109         int result = chunk.getLength();
110
111         if (result <= 0) {
112             return 0;
113         }
114
115         // Calculate chunk header
116
int pos = 7;
117         int current = result;
118         while (current > 0) {
119             int digit = current % 16;
120             current = current / 16;
121             chunkLength[pos--] = HexUtils.HEX[digit];
122         }
123         chunkHeader.setBytes(chunkLength, pos + 1, 9 - pos);
124         buffer.doWrite(chunkHeader, res);
125
126         buffer.doWrite(chunk, res);
127
128         chunkHeader.setBytes(chunkLength, 8, 2);
129         buffer.doWrite(chunkHeader, res);
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     }
146
147
148     /**
149      * Set the next buffer in the filter pipeline.
150      */

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

160     public long end()
161         throws IOException JavaDoc {
162
163         // Write end chunk
164
buffer.doWrite(END_CHUNK, null);
165         
166         return 0;
167
168     }
169
170
171     /**
172      * Make the filter ready to process the next request.
173      */

174     public void recycle() {
175     }
176
177
178     /**
179      * Return the name of the associated encoding; Here, the value is
180      * "identity".
181      */

182     public ByteChunk getEncodingName() {
183         return ENCODING;
184     }
185
186
187 }
188
Popular Tags