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