01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (license2)
04: * Initial Developer: H2 Group
05: */
06: package org.h2.test.synth;
07:
08: import java.io.IOException;
09: import java.io.InputStream;
10: import java.util.LinkedList;
11:
12: import org.h2.util.IOUtils;
13:
14: /**
15: * Catches the output of another process.
16: */
17: class OutputCatcher extends Thread {
18: private InputStream in;
19: private LinkedList list = new LinkedList();
20:
21: OutputCatcher(InputStream in) {
22: this .in = in;
23: }
24:
25: String readLine(long wait) {
26: long start = System.currentTimeMillis();
27: while (true) {
28: synchronized (list) {
29: if (list.size() > 0) {
30: return (String) list.removeFirst();
31: }
32: try {
33: list.wait(wait);
34: } catch (InterruptedException e) {
35: }
36: long time = System.currentTimeMillis() - start;
37: if (time >= wait) {
38: return null;
39: }
40: }
41: }
42: }
43:
44: public void run() {
45: StringBuffer buff = new StringBuffer();
46: while (true) {
47: try {
48: int x = in.read();
49: if (x < 0) {
50: break;
51: }
52: if (x < ' ') {
53: if (buff.length() > 0) {
54: String s = buff.toString();
55: buff.setLength(0);
56: synchronized (list) {
57: list.add(s);
58: list.notifyAll();
59: }
60: }
61: } else {
62: buff.append((char) x);
63: }
64: } catch (IOException e) {
65: break;
66: }
67: }
68: IOUtils.closeSilently(in);
69: }
70: }
|