01: package org.uispec4j.extension;
02:
03: import org.uispec4j.utils.Utils;
04:
05: import java.io.BufferedReader;
06: import java.io.IOException;
07: import java.io.InputStream;
08: import java.io.InputStreamReader;
09:
10: class StreamRecorder extends Thread {
11:
12: private InputStream inputStream;
13: private StringBuffer buffer = new StringBuffer();
14:
15: public static StreamRecorder run(InputStream stream) {
16: StreamRecorder recorder = new StreamRecorder(stream);
17: recorder.start();
18: return recorder;
19: }
20:
21: private StreamRecorder(InputStream inputStream) {
22: this .inputStream = inputStream;
23: }
24:
25: public void run() {
26: BufferedReader reader = null;
27: try {
28: reader = new BufferedReader(new InputStreamReader(
29: inputStream));
30: String line = null;
31: while ((line = reader.readLine()) != null) {
32: if (buffer.length() > 0) {
33: buffer.append(Utils.LINE_SEPARATOR);
34: }
35: buffer.append(line);
36: }
37: } catch (IOException ioe) {
38: ioe.printStackTrace();
39: } finally {
40: if (reader != null) {
41: try {
42: reader.close();
43: } catch (IOException e) {
44: }
45: }
46: }
47: }
48:
49: public String getResult() throws InterruptedException {
50: join();
51: return buffer.toString();
52: }
53: }
|