1 package com.quadcap.util.text; 2 3 40 41 import java.io.BufferedInputStream ; 42 import java.io.IOException ; 43 import java.io.InputStream ; 44 import java.io.OutputStream ; 45 46 import com.quadcap.util.Debug; 47 48 import java.io.ByteArrayOutputStream ; 50 import com.quadcap.io.LogInputStream; 51 53 58 public class Scanner { 59 InputStream is; 60 StringBuffer sb = new StringBuffer (); 61 int pushback = -1; 62 63 LogInputStream log = null; 65 ByteArrayOutputStream bos = null; 66 67 public Scanner(InputStream is, boolean saveit) { 68 if (saveit) { 69 bos = new ByteArrayOutputStream (); 70 this.is = new LogInputStream(is, bos, ""); 71 } else { 72 this.is = is; 73 } 74 } 75 76 public String getLog() { return bos.toString(); } 77 79 84 public Scanner(InputStream is) { 85 this.is = is; 86 } 87 88 public void reset(InputStream is) { 89 this.is = is; 90 if (bos != null) { 92 bos.reset(); 93 this.is = new LogInputStream(is, bos, ""); 94 } 95 pushback = -1; 97 } 98 99 final int read() throws IOException { 100 int c = pushback; 101 if (c >= 0) { 102 pushback = -1; 103 } else { 104 c = is.read(); 105 } 106 return c; 107 } 108 109 final void unread(int c) { 110 pushback = c; 111 } 112 113 120 final public void skipWhile(OctetMap map) throws IOException { 121 int c; 122 while (map.has(c = read()) && c >= 0) continue; 123 if (c >= 0) unread(c); 124 } 125 126 133 final public void skipUntil(OctetMap map) throws IOException { 134 int c; 135 while (!map.has(c = read()) && c >= 0) continue; 136 if (c >= 0) unread(c); 137 } 138 139 146 final public String parseWhile(OctetMap map) throws IOException { 147 sb.setLength(0); 148 int c; 149 while (map.has(c = read()) && c >= 0) { 150 sb.append((char)c); 151 } 152 if (c >= 0) unread(c); 153 return sb.toString(); 154 } 155 156 163 final public String parseUntil(OctetMap map) throws IOException { 164 sb.setLength(0); 165 int c; 166 while (!map.has(c = read()) && c >= 0) { 167 sb.append((char)c); 168 } 169 if (c >= 0) unread(c); 170 return sb.toString(); 171 } 172 173 174 182 final public void matchChar(int expected) throws IOException { 183 int c = read(); 184 if (c != expected) { 185 if (c < 0x1f || expected < 0x1f) { 186 throw new IOException ("Expected: " + expected + ", got " + c); 187 } else { 188 throw new IOException ("Expected: " + expected + "(" + 189 (char)expected + "), got " + c + "(" + 190 (char)c + ")"); 191 } 192 } 193 } 194 195 203 final public void matchString(OctetMap map, String expected) 204 throws IOException 205 { 206 String actual = parseWhile(map); 207 if (!actual.equals(expected)) { 208 throw new IOException ("Expected: " + expected + 209 ", got: " + actual); 210 } 211 } 212 213 222 final public void matchStringIgnoreCase(OctetMap map, 223 String expected) 224 throws IOException 225 { 226 String actual = parseWhile(map); 227 if (!actual.equalsIgnoreCase(expected)) { 228 throw new IOException ("Expected: " + expected + 229 ", got: '" + actual + "', next char = " + 230 ((char)peek()) + " (" + peek() + ")"); 231 } 232 } 233 234 242 final public int peek() throws IOException { 243 if (pushback > 0) { 244 return pushback; 245 } else { 246 int c = read(); 247 unread(c); 248 return c; 249 } 250 } 251 252 263 public static int copyWhile(InputStream is, OutputStream os, OctetMap map) 264 throws IOException 265 { 266 int c = is.read(); 267 while (c >= 0 && map.has(c)) { 268 os.write(c); 269 c = is.read(); 270 } 271 return c; 272 } 273 274 285 public static int copyUntil(InputStream is, OutputStream os, OctetMap map) 286 throws IOException 287 { 288 int c = is.read(); 289 while (c >= 0 && !map.has(c)) { 290 os.write(c); 291 c = is.read(); 292 } 293 return c; 294 } 295 296 public static int copyUntil(InputStream is, OutputStream os, int dc) 297 throws IOException 298 { 299 int c = is.read(); 300 while (c >= 0 && c != dc) { 301 os.write(c); 302 c = is.read(); 303 } 304 return c; 305 } 306 307 319 public static int copyUntil(BufferedInputStream is, OutputStream os, 320 String s) 321 throws IOException 322 { 323 if (s.length() == 0) throw new IOException ("empty target"); 324 325 int ret = -1; 326 int dc = s.charAt(0) & 0xff; 327 for (boolean found = false; !found; ) { 328 if (copyUntil(is, os, dc) < 0) return -1; 329 330 is.mark(s.length()); 331 found = true; 332 for (int i = 1; found && i < s.length(); i++) { 333 int c = 0; 334 if ((c = is.read()) != s.charAt(i)) { 335 for (int j = 0; j < i; j++) os.write(s.charAt(j)); 336 337 if (c < 0) return -1; 338 339 is.reset(); 340 found = false; 341 } 342 } 343 } 344 return 1; 345 } 346 347 355 public static int skipWhile(InputStream is, int dc) throws IOException { 356 int c; 357 while (dc == (c = is.read())) continue; 358 return c; 359 } 360 } 361 | Popular Tags |