KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > coyote > tomcat5 > CoyoteReader


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  *
21  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
22  *
23  * Portions Copyright Apache Software Foundation.
24  */

25
26
27 package org.apache.coyote.tomcat5;
28
29 import java.io.BufferedReader JavaDoc;
30 import java.io.IOException JavaDoc;
31
32
33 /**
34  * Coyote implementation of the buffred reader.
35  *
36  * @author Remy Maucherat
37  */

38 public class CoyoteReader
39     extends BufferedReader JavaDoc {
40
41
42     // -------------------------------------------------------------- Constants
43

44
45     private static final char[] LINE_SEP = { '\r', '\n' };
46     private static final int MAX_LINE_LENGTH = 4096;
47
48
49     // ----------------------------------------------------- Instance Variables
50

51
52     protected InputBuffer ib;
53
54     protected char[] lineBuffer = null;
55
56
57     // ----------------------------------------------------------- Constructors
58

59
60     public CoyoteReader(InputBuffer ib) {
61         super(ib, 1);
62         this.ib = ib;
63     }
64
65     // --------------------------------------------------------- Public Methods
66

67
68     /**
69     * Prevent cloning the facade.
70     */

71     protected Object JavaDoc clone()
72         throws CloneNotSupportedException JavaDoc {
73         throw new CloneNotSupportedException JavaDoc();
74     }
75     
76     
77     // -------------------------------------------------------- Package Methods
78

79
80     /**
81      * Clear facade.
82      */

83     void clear() {
84         ib = null;
85     }
86
87
88     // --------------------------------------------------------- Reader Methods
89

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