1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package jatoo.exec;
19
20 import java.io.BufferedReader;
21 import java.io.BufferedWriter;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
25 import java.io.OutputStream;
26 import java.io.OutputStreamWriter;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30
31
32
33
34
35
36
37
38 public class InputStreamExhausterWithDumpStream implements Runnable {
39
40 private static final Log LOGGER = LogFactory.getLog(InputStreamExhausterWithDumpStream.class);
41
42 private final InputStream processInputStream;
43 private final OutputStream dumpOutputStream;
44 private final boolean closeDumpOutputStream;
45
46 public InputStreamExhausterWithDumpStream(final InputStream processInputStream, final OutputStream dumpOutputStream, final boolean closeDumpOutputStream) {
47 this.processInputStream = processInputStream;
48 this.dumpOutputStream = dumpOutputStream;
49 this.closeDumpOutputStream = closeDumpOutputStream;
50 }
51
52 public final void exhaust() {
53
54 BufferedReader processInputStreamReader = null;
55 BufferedWriter dumpOutputStreamWriter = null;
56
57 try {
58
59 processInputStreamReader = new BufferedReader(new InputStreamReader(processInputStream));
60 dumpOutputStreamWriter = new BufferedWriter(new OutputStreamWriter(dumpOutputStream));
61
62 String line = null;
63 while ((line = processInputStreamReader.readLine()) != null) {
64
65 dumpOutputStreamWriter.write(line);
66 dumpOutputStreamWriter.newLine();
67
68 dumpOutputStreamWriter.flush();
69 }
70 }
71
72 catch (IOException e) {
73 LOGGER.error("error exhausting the stream", e);
74 }
75
76 finally {
77 if (closeDumpOutputStream && dumpOutputStreamWriter != null) {
78 try {
79 dumpOutputStreamWriter.close();
80 } catch (IOException e) {
81 LOGGER.error("error closing the dump stream", e);
82 }
83 }
84 }
85 }
86
87 @Override
88 public final void run() {
89 exhaust();
90 }
91
92 }