001: /*
002: * Copyright 1999,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.coyote.tomcat5;
018:
019: import java.io.BufferedReader;
020: import java.io.IOException;
021:
022: /**
023: * Coyote implementation of the buffred reader.
024: *
025: * @author Remy Maucherat
026: */
027: public class CoyoteReader extends BufferedReader {
028:
029: // -------------------------------------------------------------- Constants
030:
031: private static final char[] LINE_SEP = { '\r', '\n' };
032: private static final int MAX_LINE_LENGTH = 4096;
033:
034: // ----------------------------------------------------- Instance Variables
035:
036: protected InputBuffer ib;
037:
038: protected char[] lineBuffer = null;
039:
040: // ----------------------------------------------------------- Constructors
041:
042: public CoyoteReader(InputBuffer ib) {
043: super (ib, 1);
044: this .ib = ib;
045: }
046:
047: // -------------------------------------------------------- Package Methods
048:
049: /**
050: * Clear facade.
051: */
052: void clear() {
053: ib = null;
054: }
055:
056: // --------------------------------------------------------- Reader Methods
057:
058: public void close() throws IOException {
059: ib.close();
060: }
061:
062: public int read() throws IOException {
063: return ib.read();
064: }
065:
066: public int read(char[] cbuf) throws IOException {
067: return ib.read(cbuf, 0, cbuf.length);
068: }
069:
070: public int read(char[] cbuf, int off, int len) throws IOException {
071: return ib.read(cbuf, off, len);
072: }
073:
074: public long skip(long n) throws IOException {
075: return ib.skip(n);
076: }
077:
078: public boolean ready() throws IOException {
079: return ib.ready();
080: }
081:
082: public boolean markSupported() {
083: return true;
084: }
085:
086: public void mark(int readAheadLimit) throws IOException {
087: ib.mark(readAheadLimit);
088: }
089:
090: public void reset() throws IOException {
091: ib.reset();
092: }
093:
094: public String readLine() throws IOException {
095:
096: if (lineBuffer == null) {
097: lineBuffer = new char[MAX_LINE_LENGTH];
098: }
099:
100: String result = null;
101:
102: int pos = 0;
103: int end = -1;
104: int skip = -1;
105: StringBuffer aggregator = null;
106: while (end < 0) {
107: mark(MAX_LINE_LENGTH);
108: while ((pos < MAX_LINE_LENGTH) && (end < 0)) {
109: int nRead = read(lineBuffer, pos, MAX_LINE_LENGTH - pos);
110: if (nRead < 0) {
111: if (pos == 0) {
112: return null;
113: }
114: end = pos;
115: skip = pos;
116: }
117: for (int i = pos; (i < (pos + nRead)) && (end < 0); i++) {
118: if (lineBuffer[i] == LINE_SEP[0]) {
119: end = i;
120: skip = i + 1;
121: char nextchar;
122: if (i == (pos + nRead - 1)) {
123: nextchar = (char) read();
124: } else {
125: nextchar = lineBuffer[i + 1];
126: }
127: if (nextchar == LINE_SEP[1]) {
128: skip++;
129: }
130: } else if (lineBuffer[i] == LINE_SEP[1]) {
131: end = i;
132: skip = i + 1;
133: }
134: }
135: if (nRead > 0) {
136: pos += nRead;
137: }
138: }
139: if (end < 0) {
140: if (aggregator == null) {
141: aggregator = new StringBuffer();
142: }
143: aggregator.append(lineBuffer);
144: pos = 0;
145: } else {
146: reset();
147: skip(skip);
148: }
149: }
150:
151: if (aggregator == null) {
152: result = new String(lineBuffer, 0, end);
153: } else {
154: aggregator.append(lineBuffer, 0, end);
155: result = aggregator.toString();
156: }
157:
158: return result;
159:
160: }
161:
162: }
|