~ubuntu-branches/ubuntu/jaunty/electric/jaunty

« back to all changes in this revision

Viewing changes to com/sun/electric/tool/user/Exec.java

  • Committer: Bazaar Package Importer
  • Author(s): Onkar Shinde
  • Date: 2009-01-08 02:05:08 UTC
  • mfrom: (1.1.2 upstream) (3.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20090108020508-0h3li7zt9mu5gf0i
Tags: 8.08-1
New upstream version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
 
26
26
import java.io.*;
27
27
import java.util.ArrayList;
 
28
import java.util.List;
28
29
 
29
30
/**
30
31
 * Runtime.exec() has many pitfalls to it's proper use.  This class
50
51
 
51
52
        private InputStream in;
52
53
        private OutputStream redirect;
 
54
        private char [] buf;
53
55
 
54
56
        /**
55
57
         * Create a stream reader that will read from the stream
68
70
        public ExecProcessReader(InputStream in, OutputStream redirect) {
69
71
            this.in = in;
70
72
            this.redirect = redirect;
 
73
            buf = new char[256];
71
74
            setName("ExecProcessReader");
72
75
        }
73
76
 
79
82
                // read from stream
80
83
                InputStreamReader input = new InputStreamReader(in);
81
84
                BufferedReader reader = new BufferedReader(input);
82
 
                String line = null;
83
 
                while ((line = reader.readLine()) != null) {
 
85
                int read = 0;
 
86
                while ((read = reader.read(buf)) >= 0) {
84
87
                    if (pw != null) {
85
 
                        pw.println(line);
 
88
                        pw.write(buf, 0, read);
86
89
                        pw.flush();
87
90
                    }
88
91
                }
189
192
    }
190
193
 
191
194
    public void run() {
 
195
        if (outStreamRedir instanceof OutputStreamChecker) {
 
196
            ((OutputStreamChecker)outStreamRedir).setExec(this);
 
197
        }
 
198
        if (errStreamRedir instanceof OutputStreamChecker) {
 
199
            ((OutputStreamChecker)errStreamRedir).setExec(this);
 
200
        }
 
201
 
192
202
        try {
193
203
            Runtime rt = Runtime.getRuntime();
194
204
 
303
313
    }
304
314
 
305
315
    public int getExitVal() { return exitVal; }
 
316
 
 
317
    /**
 
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.
 
322
     */
 
323
    public static class OutputStreamChecker extends OutputStream implements Serializable {
 
324
        private OutputStream ostream;
 
325
        private String checkFor;
 
326
        private StringBuffer lastLine;
 
327
        private char [] buf;
 
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;
 
336
 
 
337
        /**
 
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
 
342
         */
 
343
        public OutputStreamChecker(OutputStream ostream, String checkFor) {
 
344
            this(ostream, checkFor, false, null);
 
345
        }
 
346
 
 
347
 
 
348
        /**
 
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
 
354
         */
 
355
        public OutputStreamChecker(OutputStream ostream, String checkFor, boolean regexp, File copyToFile) {
 
356
            this.ostream = ostream;
 
357
            this.checkFor = checkFor;
 
358
            this.regexp = regexp;
 
359
            lastLine = null;
 
360
            buf = null;
 
361
            bufOffset = 0;
 
362
            lastLine = new StringBuffer();
 
363
            if (!regexp)
 
364
                buf = new char[checkFor.length()];
 
365
 
 
366
            found = false;
 
367
            foundLine = null;
 
368
            out = null;
 
369
            this.copyToFile = copyToFile;
 
370
            if (copyToFile != null) {
 
371
                try {
 
372
                    out = new PrintWriter(new BufferedWriter(new FileWriter(this.copyToFile)));
 
373
                } catch (IOException e) {
 
374
                    System.out.println(e.getMessage());
 
375
                    out = null;
 
376
                }
 
377
            }
 
378
            listeners = new ArrayList<OutputStreamCheckerListener>();
 
379
        }
 
380
 
 
381
        public void write(int b) throws IOException {
 
382
            ostream.write(b);
 
383
            if (out != null) out.write(b);
 
384
            lastLine.append((char)b);
 
385
            if (regexp) {
 
386
                // match against regular expression on end-of-line
 
387
                if (b == '\n') {
 
388
                    if (lastLine.toString().matches(checkFor)) {
 
389
                        found = true;
 
390
                        foundLine = lastLine.toString();
 
391
                        alertListeners(foundLine);
 
392
                    }
 
393
                }
 
394
            } else {
 
395
                // store data in buffer of same length as string trying to match
 
396
                buf[bufOffset] = (char)b;
 
397
                bufOffset++;
 
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;
 
406
                    }
 
407
                }
 
408
                if (matched) {
 
409
                    found = true;
 
410
                    foundLine = lastLine.toString();
 
411
                    alertListeners(foundLine);
 
412
                }
 
413
            }
 
414
            if (b == '\n') {
 
415
                lastLine.delete(0, lastLine.length());
 
416
            }
 
417
        }
 
418
 
 
419
        public void addOutputStreamCheckerListener(OutputStreamCheckerListener l) {
 
420
            listeners.add(l);
 
421
        }
 
422
        public void removeOutputStreamCheckerListener(OutputStreamCheckerListener l) {
 
423
            listeners.remove(l);
 
424
        }
 
425
        private void alertListeners(String matched) {
 
426
            for (OutputStreamCheckerListener l : listeners) {
 
427
                l.matchFound(exec, matched);
 
428
            }
 
429
        }
 
430
 
 
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; }
 
435
 
 
436
        private void setExec(Exec e) { this.exec = e; }
 
437
    }
 
438
 
 
439
    /**
 
440
     * Interface for objects to be notified immediately when OutputStreamChecker
 
441
     * matches when it is looking for.
 
442
     */
 
443
    public interface OutputStreamCheckerListener {
 
444
        public void matchFound(Exec exec, String matched);
 
445
    }
306
446
}