1 | |
package net.sf.statsvn.util; |
2 | |
|
3 | |
import java.io.BufferedInputStream; |
4 | |
import java.io.File; |
5 | |
import java.io.IOException; |
6 | |
import java.io.InputStream; |
7 | |
import java.io.InputStreamReader; |
8 | |
|
9 | |
import net.sf.statcvs.util.LookaheadReader; |
10 | |
import net.sf.statsvn.output.SvnConfigurationOptions; |
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
public final class ProcessUtils { |
24 | |
private BufferedInputStream inputStream; |
25 | |
|
26 | |
private BufferedInputStream errorStream; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | 59 | public ProcessUtils() { |
33 | 59 | } |
34 | |
|
35 | |
public static synchronized ProcessUtils call(final String sCommand) throws IOException { |
36 | 0 | final ProcessUtils util = new ProcessUtils(); |
37 | 0 | final Process lastProcess = Runtime.getRuntime().exec(sCommand, null, getWorkingFolder()); |
38 | 0 | util.errorStream = new BufferedInputStream(lastProcess.getErrorStream()); |
39 | 0 | util.inputStream = new BufferedInputStream(lastProcess.getInputStream()); |
40 | |
|
41 | 0 | return util; |
42 | |
} |
43 | |
|
44 | |
public void close() throws IOException { |
45 | 59 | if (errorStream != null) { |
46 | 0 | errorStream.close(); |
47 | 0 | errorStream = null; |
48 | |
} |
49 | 59 | if (inputStream != null) { |
50 | 59 | inputStream.close(); |
51 | 59 | inputStream = null; |
52 | |
} |
53 | 59 | } |
54 | |
|
55 | |
private static File getWorkingFolder() { |
56 | 0 | return SvnConfigurationOptions.getCheckedOutDirectoryAsFile(); |
57 | |
} |
58 | |
|
59 | |
protected boolean hasErrorOccured() throws IOException { |
60 | 59 | return errorStream != null && errorStream.available() > 0; |
61 | |
} |
62 | |
|
63 | |
protected String getErrorMessage() { |
64 | 0 | if (errorStream == null) { |
65 | 0 | return null; |
66 | |
} else { |
67 | 0 | final LookaheadReader diffReader = new LookaheadReader(new InputStreamReader(errorStream)); |
68 | 0 | final StringBuffer builder = new StringBuffer(); |
69 | |
try { |
70 | 0 | while (diffReader.hasNextLine()) { |
71 | 0 | builder.append(diffReader.nextLine()); |
72 | |
} |
73 | 0 | } catch (final IOException e) { |
74 | 0 | SvnConfigurationOptions.getTaskLogger().error(e.toString()); |
75 | 0 | } |
76 | |
|
77 | 0 | return builder.toString(); |
78 | |
} |
79 | |
} |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
public BufferedInputStream getErrorStream() { |
85 | 0 | return errorStream; |
86 | |
} |
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
public BufferedInputStream getInputStream() { |
92 | 59 | return inputStream; |
93 | |
} |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
public void setErrorStream(final InputStream errorStream) { |
99 | 0 | this.errorStream = new BufferedInputStream(errorStream); |
100 | 0 | } |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
public void setInputStream(final InputStream inputStream) { |
106 | 59 | this.inputStream = new BufferedInputStream(inputStream); |
107 | 59 | } |
108 | |
} |