1 17 18 19 package org.apache.catalina.connector; 20 21 import java.io.BufferedReader ; 22 import java.io.IOException ; 23 24 25 30 public class CoyoteReader 31 extends BufferedReader { 32 33 34 36 37 private static final char[] LINE_SEP = { '\r', '\n' }; 38 private static final int MAX_LINE_LENGTH = 4096; 39 40 41 43 44 protected InputBuffer ib; 45 46 47 protected char[] lineBuffer = null; 48 49 50 52 53 public CoyoteReader(InputBuffer ib) { 54 super(ib, 1); 55 this.ib = ib; 56 } 57 58 59 61 62 65 protected Object clone() 66 throws CloneNotSupportedException { 67 throw new CloneNotSupportedException (); 68 } 69 70 71 73 74 77 void clear() { 78 ib = null; 79 } 80 81 82 84 85 public void close() 86 throws IOException { 87 ib.close(); 88 } 89 90 91 public int read() 92 throws IOException { 93 return ib.read(); 94 } 95 96 97 public int read(char[] cbuf) 98 throws IOException { 99 return ib.read(cbuf, 0, cbuf.length); 100 } 101 102 103 public int read(char[] cbuf, int off, int len) 104 throws IOException { 105 return ib.read(cbuf, off, len); 106 } 107 108 109 public long skip(long n) 110 throws IOException { 111 return ib.skip(n); 112 } 113 114 115 public boolean ready() 116 throws IOException { 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 { 128 ib.mark(readAheadLimit); 129 } 130 131 132 public void reset() 133 throws IOException { 134 ib.reset(); 135 } 136 137 138 public String readLine() 139 throws IOException { 140 141 if (lineBuffer == null) { 142 lineBuffer = new char[MAX_LINE_LENGTH]; 143 } 144 145 String result = null; 146 147 int pos = 0; 148 int end = -1; 149 int skip = -1; 150 StringBuffer 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 (); 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 (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 |