305
315
public int getExitVal() { return exitVal; }
318
* Check for a string passed to the OutputStream. All chars passed to
319
* this class are also transparently passed to System.out.
320
* This only checks for strings within a single line of text.
321
* The strings are simple strings, not regular expressions.
323
public static class OutputStreamChecker extends OutputStream implements Serializable {
324
private OutputStream ostream;
325
private String checkFor;
326
private StringBuffer lastLine;
328
private int bufOffset;
329
private boolean found;
330
private boolean regexp; // if checkFor string is a regular expression
331
private String foundLine;
332
private File copyToFile;
333
private PrintWriter out;
334
private List<OutputStreamCheckerListener> listeners;
335
private Exec exec = null;
338
* Checks for string in output stream. The string may span multiple lines, or may be contained
339
* within a non-terminated line (such as an input query).
340
* @param ostream send read data to this output stream (usually System.out)
341
* @param checkFor the string to check for
343
public OutputStreamChecker(OutputStream ostream, String checkFor) {
344
this(ostream, checkFor, false, null);
349
* Checks for string in output stream. String must be contained within one line.
350
* @param ostream send read data to this output stream (usually System.out)
351
* @param checkFor the string to check for
352
* @param regexp if true, the string is considered a regular expression
353
* @param copyToFile if non-null, the output is copied to this file
355
public OutputStreamChecker(OutputStream ostream, String checkFor, boolean regexp, File copyToFile) {
356
this.ostream = ostream;
357
this.checkFor = checkFor;
358
this.regexp = regexp;
362
lastLine = new StringBuffer();
364
buf = new char[checkFor.length()];
369
this.copyToFile = copyToFile;
370
if (copyToFile != null) {
372
out = new PrintWriter(new BufferedWriter(new FileWriter(this.copyToFile)));
373
} catch (IOException e) {
374
System.out.println(e.getMessage());
378
listeners = new ArrayList<OutputStreamCheckerListener>();
381
public void write(int b) throws IOException {
383
if (out != null) out.write(b);
384
lastLine.append((char)b);
386
// match against regular expression on end-of-line
388
if (lastLine.toString().matches(checkFor)) {
390
foundLine = lastLine.toString();
391
alertListeners(foundLine);
395
// store data in buffer of same length as string trying to match
396
buf[bufOffset] = (char)b;
398
if (bufOffset >= buf.length) bufOffset = 0;
399
// check against string. Since same length, when string is found bufOffset
400
// will be at start of string.
401
boolean matched = true;
402
for (int i=0; i<buf.length; i++) {
403
int y = (i+bufOffset) % buf.length;
404
if (checkFor.charAt(i) != buf[y]) {
405
matched = false; break;
410
foundLine = lastLine.toString();
411
alertListeners(foundLine);
415
lastLine.delete(0, lastLine.length());
419
public void addOutputStreamCheckerListener(OutputStreamCheckerListener l) {
422
public void removeOutputStreamCheckerListener(OutputStreamCheckerListener l) {
425
private void alertListeners(String matched) {
426
for (OutputStreamCheckerListener l : listeners) {
427
l.matchFound(exec, matched);
431
public boolean getFound() { return found; }
432
public String getFoundLine() { return foundLine; }
433
public void close() { if (out != null) out.close(); }
434
public File getCopyToFile() { return copyToFile; }
436
private void setExec(Exec e) { this.exec = e; }
440
* Interface for objects to be notified immediately when OutputStreamChecker
441
* matches when it is looking for.
443
public interface OutputStreamCheckerListener {
444
public void matchFound(Exec exec, String matched);