KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > connector > CoyoteReader


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
19 package org.apache.catalina.connector;
20
21 import java.io.BufferedReader JavaDoc;
22 import java.io.IOException JavaDoc;
23
24
25 /**
26  * Coyote implementation of the buffred reader.
27  *
28  * @author Remy Maucherat
29  */

30 public class CoyoteReader
31     extends BufferedReader JavaDoc {
32
33
34     // -------------------------------------------------------------- Constants
35

36
37     private static final char[] LINE_SEP = { '\r', '\n' };
38     private static final int MAX_LINE_LENGTH = 4096;
39
40
41     // ----------------------------------------------------- Instance Variables
42

43
44     protected InputBuffer ib;
45
46
47     protected char[] lineBuffer = null;
48
49
50     // ----------------------------------------------------------- Constructors
51

52
53     public CoyoteReader(InputBuffer ib) {
54         super(ib, 1);
55         this.ib = ib;
56     }
57
58
59     // --------------------------------------------------------- Public Methods
60

61
62     /**
63      * Prevent cloning the facade.
64      */

65     protected Object JavaDoc clone()
66         throws CloneNotSupportedException JavaDoc {
67         throw new CloneNotSupportedException JavaDoc();
68     }
69
70
71     // -------------------------------------------------------- Package Methods
72

73
74     /**
75      * Clear facade.
76      */

77     void clear() {
78         ib = null;
79     }
80
81
82     // --------------------------------------------------------- Reader Methods
83

84
85     public void close()
86         throws IOException JavaDoc {
87         ib.close();
88     }
89
90
91     public int read()
92         throws IOException JavaDoc {
93         return ib.read();
94     }
95
96
97     public int read(char[] cbuf)
98         throws IOException JavaDoc {
99         return ib.read(cbuf, 0, cbuf.length);
100     }
101
102
103     public int read(char[] cbuf, int off, int len)
104         throws IOException JavaDoc {
105         return ib.read(cbuf, off, len);
106     }
107
108
109     public long skip(long n)
110         throws IOException JavaDoc {
111         return ib.skip(n);
112     }
113
114
115     public boolean ready()
116         throws IOException JavaDoc {
117         return ib.ready();
118     }
119
120
121     public boolean markSupported() {
122         return true;
123     }
124
125
126     public void mark(int readAheadLimit)
127         throws IOException JavaDoc {
128         ib.mark(readAheadLimit);
129     }
130
131
132     public void reset()
133         throws IOException JavaDoc {
134         ib.reset();
135     }
136
137
138     public String JavaDoc readLine()
139         throws IOException JavaDoc {
140
141         if (lineBuffer == null) {
142             lineBuffer = new char[MAX_LINE_LENGTH];
143        }
144
145         String JavaDoc result = null;
146
147         int pos = 0;
148         int end = -1;
149         int skip = -1;
150         StringBuffer JavaDoc aggregator = null;
151         while (end < 0) {
152             mark(MAX_LINE_LENGTH);
153             while ((pos < MAX_LINE_LENGTH) && (end < 0)) {
154                 int nRead = read(lineBuffer, pos, MAX_LINE_LENGTH - pos);
155                 if (nRead < 0) {
156                     if (pos == 0) {
157                         return null;
158                     }
159                     end = pos;
160                     skip = pos;
161                 }
162                 for (int i = pos; (i < (pos + nRead)) && (end < 0); i++) {
163                     if (lineBuffer[i] == LINE_SEP[0]) {
164                         end = i;
165                         skip = i + 1;
166                         char nextchar;
167                         if (i == (pos + nRead - 1)) {
168                             nextchar = (char) read();
169                         } else {
170                             nextchar = lineBuffer[i+1];
171                         }
172                         if (nextchar == LINE_SEP[1]) {
173                             skip++;
174                         }
175                     } else if (lineBuffer[i] == LINE_SEP[1]) {
176                         end = i;
177                         skip = i + 1;
178                     }
179                 }
180                 if (nRead > 0) {
181                     pos += nRead;
182                 }
183             }
184             if (end < 0) {
185                 if (aggregator == null) {
186                     aggregator = new StringBuffer JavaDoc();
187                 }
188                 aggregator.append(lineBuffer);
189                 pos = 0;
190             } else {
191                 reset();
192                 skip(skip);
193             }
194         }
195
196         if (aggregator == null) {
197             result = new String JavaDoc(lineBuffer, 0, end);
198         } else {
199             aggregator.append(lineBuffer, 0, end);
200             result = aggregator.toString();
201         }
202
203         return result;
204
205     }
206
207
208 }
209
Popular Tags