forked from BimberLab/DiscvrLabKeyModules
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProcessUtils.java
More file actions
108 lines (98 loc) · 3.67 KB
/
ProcessUtils.java
File metadata and controls
108 lines (98 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package org.labkey.api.sequenceanalysis.pipeline;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.Logger;
import org.labkey.api.util.StringUtilsLabKey;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Created by bimber on 7/12/2016.
*/
public class ProcessUtils
{
//adapted from: http://stackoverflow.com/questions/35706921/redirecting-the-output-of-a-process-into-the-input-of-another-process-using-proc
public static class StreamRedirector
{
private final Logger _log;
public StreamRedirector(Logger log)
{
_log = log;
}
public void redirectStreams(Process process1, Process process2) throws IOException
{
final Process tmpProcess1 = process1;
final Process tmpProcess2 = process2;
new Thread(new Runnable()
{
private static final int BUFFER_SIZE = 1024 * 4;
@Override
public void run()
{
try (BufferedInputStream bufferedInputStream = new BufferedInputStream(tmpProcess1.getInputStream());
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(tmpProcess2.getOutputStream())
)
{
IOUtils.copy(bufferedInputStream, bufferedOutputStream);
}
catch (IOException e)
{
if (tmpProcess1.isAlive())
{
_log.error(e.getMessage(), e);
}
else
{
_log.info("process closed");
}
}
}
}, "ProcessUtils.StreamRedirector").start();
}
}
public static class ProcessReader
{
private final Logger _log;
private final boolean _writeOutputToLog;
private final boolean _readStdErr;
public ProcessReader(Logger log, boolean writeOutputToLog, boolean readStdErr)
{
_log = log;
_writeOutputToLog = writeOutputToLog;
_readStdErr = readStdErr;
}
public void readProcess(Process process) throws IOException
{
final Process tmpProcess = process;
new Thread(new Runnable()
{
@Override
public void run()
{
try (BufferedReader procReader = new BufferedReader(new InputStreamReader(_readStdErr ? tmpProcess.getErrorStream() : tmpProcess.getInputStream(), StringUtilsLabKey.DEFAULT_CHARSET)))
{
String line;
while ((line = procReader.readLine()) != null)
{
if (_writeOutputToLog)
_log.log(Level.DEBUG, "\t" + line);
}
}
catch (IOException e)
{
if (_log != null)
{
if ("Stream closed".equals(e.getMessage()))
{
return; //ignore. unsure how to avoid this error?
}
_log.error(e.getMessage(), e);
}
}
}
}, "ProcessUtils.ProcessReader").start();
}
}
}