1 25 26 27 package org.apache.coyote.tomcat5; 28 29 import java.io.BufferedReader ; 30 import java.io.IOException ; 31 32 33 38 public class CoyoteReader 39 extends BufferedReader { 40 41 42 44 45 private static final char[] LINE_SEP = { '\r', '\n' }; 46 private static final int MAX_LINE_LENGTH = 4096; 47 48 49 51 52 protected InputBuffer ib; 53 54 protected char[] lineBuffer = null; 55 56 57 59 60 public CoyoteReader(InputBuffer ib) { 61 super(ib, 1); 62 this.ib = ib; 63 } 64 65 67 68 71 protected Object clone() 72 throws CloneNotSupportedException { 73 throw new CloneNotSupportedException (); 74 } 75 76 77 79 80 83 void clear() { 84 ib = null; 85 } 86 87 88 90 91 public void close() 92 throws IOException { 93 ib.close(); 94 } 95 96 97 public int read() 98 throws IOException { 99 return ib.read(); 100 } 101 102 103 public int read(char[] cbuf) 104 throws IOException { 105 return ib.read(cbuf, 0, cbuf.length); 106 } 107 108 109 public int read(char[] cbuf, int off, int len) 110 throws IOException { 111 return ib.read(cbuf, off, len); 112 } 113 114 115 public long skip(long n) 116 throws IOException { 117 return ib.skip(n); 118 } 119 120 121 public boolean ready() 122 throws IOException { 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 { 134 ib.mark(readAheadLimit); 135 } 136 137 138 public void reset() 139 throws IOException { 140 ib.reset(); 141 } 142 143 144 public String readLine() 145 throws IOException { 146 147 if (lineBuffer == null) { 148 lineBuffer = new char[MAX_LINE_LENGTH]; 149 } 150 151 String result = null; 152 153 int pos = 0; 154 int end = -1; 155 int skip = -1; 156 StringBuffer 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 (); 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 (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 |