00001
package com.quadcap.util.text;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
import java.io.BufferedInputStream;
00042
import java.io.IOException;
00043
import java.io.InputStream;
00044
import java.io.OutputStream;
00045
00046
import com.quadcap.util.Debug;
00047
00048
00049
import java.io.ByteArrayOutputStream;
00050
import com.quadcap.io.LogInputStream;
00051
00052
00053
00054
00055
00056
00057
00058 public class Scanner {
00059 InputStream
is;
00060 StringBuffer
sb =
new StringBuffer();
00061 int pushback = -1;
00062
00063
00064 LogInputStream
log = null;
00065 ByteArrayOutputStream
bos = null;
00066
00067 public Scanner(InputStream is,
boolean saveit) {
00068
if (saveit) {
00069
bos =
new ByteArrayOutputStream();
00070
this.is =
new LogInputStream(is,
bos,
"");
00071 }
else {
00072
this.is = is;
00073 }
00074 }
00075
00076 public String
getLog() {
return bos.toString(); }
00077
00078
00079
00080
00081
00082
00083
00084 public Scanner(InputStream is) {
00085
this.is = is;
00086 }
00087
00088 public void reset(InputStream is) {
00089
this.is = is;
00090
00091
if (
bos != null) {
00092
bos.reset();
00093
this.is =
new LogInputStream(is,
bos,
"");
00094 }
00095
00096
pushback = -1;
00097 }
00098
00099 final int read() throws IOException {
00100
int c =
pushback;
00101
if (c >= 0) {
00102 pushback = -1;
00103 }
else {
00104 c =
is.read();
00105 }
00106
return c;
00107 }
00108
00109 final void unread(
int c) {
00110
pushback = c;
00111 }
00112
00113
00114
00115
00116
00117
00118
00119
00120 final public void skipWhile(
OctetMap map)
throws IOException {
00121
int c;
00122
while (map.has(c =
read()) && c >= 0)
continue;
00123
if (c >= 0) unread(c);
00124 }
00125
00126
00127
00128
00129
00130
00131
00132
00133 final public void skipUntil(
OctetMap map)
throws IOException {
00134
int c;
00135
while (!map.has(c =
read()) && c >= 0)
continue;
00136
if (c >= 0) unread(c);
00137 }
00138
00139
00140
00141
00142
00143
00144
00145
00146 final public String
parseWhile(
OctetMap map)
throws IOException {
00147
sb.setLength(0);
00148
int c;
00149
while (map.has(c =
read()) && c >= 0) {
00150
sb.append((
char)c);
00151 }
00152
if (c >= 0) unread(c);
00153
return sb.toString();
00154 }
00155
00156
00157
00158
00159
00160
00161
00162
00163 final public String
parseUntil(
OctetMap map)
throws IOException {
00164
sb.setLength(0);
00165
int c;
00166
while (!map.has(c =
read()) && c >= 0) {
00167
sb.append((
char)c);
00168 }
00169
if (c >= 0) unread(c);
00170
return sb.toString();
00171 }
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182 final public void matchChar(
int expected)
throws IOException {
00183
int c =
read();
00184
if (c != expected) {
00185
if (c < 0x1f || expected < 0x1f) {
00186
throw new IOException(
"Expected: " + expected +
", got " + c);
00187 }
else {
00188
throw new IOException(
"Expected: " + expected +
"(" +
00189 (
char)expected +
"), got " + c +
"(" +
00190 (
char)c +
")");
00191 }
00192 }
00193 }
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203 final public void matchString(
OctetMap map, String expected)
00204
throws IOException
00205 {
00206 String actual = parseWhile(map);
00207
if (!actual.equals(expected)) {
00208
throw new IOException(
"Expected: " + expected +
00209
", got: " + actual);
00210 }
00211 }
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222 final public void matchStringIgnoreCase(
OctetMap map,
00223 String expected)
00224
throws IOException
00225 {
00226 String actual = parseWhile(map);
00227
if (!actual.equalsIgnoreCase(expected)) {
00228
throw new IOException(
"Expected: " + expected +
00229
", got: '" + actual +
"', next char = " +
00230 ((
char)
peek()) +
" (" +
peek() +
")");
00231 }
00232 }
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242 final public int peek() throws IOException {
00243
if (
pushback > 0) {
00244
return pushback;
00245 }
else {
00246
int c =
read();
00247 unread(c);
00248
return c;
00249 }
00250 }
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263 public static int copyWhile(InputStream is, OutputStream os,
OctetMap map)
00264
throws IOException
00265 {
00266
int c =
is.read();
00267
while (c >= 0 && map.has(c)) {
00268 os.write(c);
00269 c =
is.read();
00270 }
00271
return c;
00272 }
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285 public static int copyUntil(InputStream is, OutputStream os,
OctetMap map)
00286
throws IOException
00287 {
00288
int c =
is.read();
00289
while (c >= 0 && !map.has(c)) {
00290 os.write(c);
00291 c =
is.read();
00292 }
00293
return c;
00294 }
00295
00296 public static int copyUntil(InputStream is, OutputStream os,
int dc)
00297
throws IOException
00298 {
00299
int c =
is.read();
00300
while (c >= 0 && c != dc) {
00301 os.write(c);
00302 c =
is.read();
00303 }
00304
return c;
00305 }
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319 public static int copyUntil(BufferedInputStream is, OutputStream os,
00320 String s)
00321
throws IOException
00322 {
00323
if (s.length() == 0)
throw new IOException(
"empty target");
00324
00325
int ret = -1;
00326
int dc = s.charAt(0) & 0xff;
00327
for (
boolean found =
false; !found; ) {
00328
if (copyUntil(
is, os, dc) < 0)
return -1;
00329
00330
is.mark(s.length());
00331 found =
true;
00332
for (
int i = 1; found && i < s.length(); i++) {
00333
int c = 0;
00334
if ((c =
is.read()) != s.charAt(i)) {
00335
for (
int j = 0; j < i; j++) os.write(s.charAt(j));
00336
00337
if (c < 0)
return -1;
00338
00339
is.reset();
00340 found =
false;
00341 }
00342 }
00343 }
00344
return 1;
00345 }
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355 public static int skipWhile(InputStream is,
int dc)
throws IOException {
00356
int c;
00357
while (dc == (c =
is.read()))
continue;
00358
return c;
00359 }
00360 }