• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

LearnLib / automatalib / 13138848026

04 Feb 2025 02:53PM UTC coverage: 92.108% (+2.2%) from 89.877%
13138848026

push

github

mtf90
[maven-release-plugin] prepare release automatalib-0.12.0

16609 of 18032 relevant lines covered (92.11%)

1.7 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.79
/commons/util/src/main/java/net/automatalib/common/util/process/ProcessUtil.java
1
/* Copyright (C) 2013-2025 TU Dortmund University
2
 * This file is part of AutomataLib <https://automatalib.net>.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package net.automatalib.common.util.process;
17

18
import java.io.IOException;
19
import java.io.OutputStream;
20
import java.io.Reader;
21
import java.io.Writer;
22
import java.util.List;
23
import java.util.function.Consumer;
24

25
import net.automatalib.common.util.IOUtil;
26
import net.automatalib.common.util.process.InputStreamConsumer.DelegatingConsumer;
27
import net.automatalib.common.util.process.InputStreamConsumer.NOPConsumer;
28
import org.checkerframework.checker.nullness.qual.Nullable;
29

30
/**
31
 * Utility class for invoking system processes.
32
 */
33
public final class ProcessUtil {
34

35
    private ProcessUtil() {
36
        // prevent instantiation
37
    }
38

39
    /**
40
     * Runs the given set of command line arguments as a system process and returns the exit value of the spawned
41
     * process. Discards any output of the process.
42
     *
43
     * @param commandLine
44
     *         the list of command line arguments to run
45
     *
46
     * @return the exit code of the process
47
     *
48
     * @throws IOException
49
     *         if an exception occurred while reading the process' outputs
50
     * @throws InterruptedException
51
     *         if an exception occurred during process exception
52
     */
53
    public static int invokeProcess(String[] commandLine) throws IOException, InterruptedException {
54
        return invokeProcess(commandLine, null, new NOPConsumer());
1✔
55
    }
56

57
    /**
58
     * Runs the given set of command line arguments as a system process and returns the exit value of the spawned
59
     * process. Discards any output of the process.
60
     *
61
     * @param commandLine
62
     *         the list of command line arguments to run
63
     *
64
     * @return the exit code of the process
65
     *
66
     * @throws IOException
67
     *         if an exception occurred while reading the process' outputs
68
     * @throws InterruptedException
69
     *         if an exception occurred during process exception
70
     */
71
    public static int invokeProcess(List<String> commandLine) throws IOException, InterruptedException {
72
        return invokeProcess(commandLine, null, new NOPConsumer());
×
73
    }
74

75
    /**
76
     * Runs the given set of command line arguments as a system process and returns the exit value of the spawned
77
     * process. Additionally, allows to supply an input stream to the invoked program. Discards any output of the
78
     * process.
79
     *
80
     * @param commandLine
81
     *         the list of command line arguments to run
82
     * @param input
83
     *         the input passed to the program
84
     *
85
     * @return the exit code of the process
86
     *
87
     * @throws IOException
88
     *         if an exception occurred while reading the process' outputs, or writing the process' inputs
89
     * @throws InterruptedException
90
     *         if an exception occurred during process exception
91
     */
92
    public static int invokeProcess(String[] commandLine, Reader input) throws IOException, InterruptedException {
93
        return invokeProcess(commandLine, input, new NOPConsumer());
×
94
    }
95

96
    /**
97
     * Runs the given set of command line arguments as a system process and returns the exit value of the spawned
98
     * process. Additionally, allows to supply an input stream to the invoked program. Discards any output of the
99
     * process.
100
     *
101
     * @param commandLine
102
     *         the list of command line arguments to run
103
     * @param input
104
     *         the input passed to the program
105
     *
106
     * @return the exit code of the process
107
     *
108
     * @throws IOException
109
     *         if an exception occurred while reading the process' outputs, or writing the process' inputs
110
     * @throws InterruptedException
111
     *         if an exception occurred during process exception
112
     */
113
    public static int invokeProcess(List<String> commandLine, Reader input) throws IOException, InterruptedException {
114
        return invokeProcess(commandLine, input, new NOPConsumer());
×
115
    }
116

117
    /**
118
     * Runs the given set of command line arguments as a system process and returns the exit value of the spawned
119
     * process. Outputs of the process (both normal and error) are passed to the {@code consumer}.
120
     *
121
     * @param commandLine
122
     *         the list of command line arguments to run
123
     * @param consumer
124
     *         the consumer for the program's output
125
     *
126
     * @return the exit code of the process
127
     *
128
     * @throws IOException
129
     *         if an exception occurred while reading the process' outputs
130
     * @throws InterruptedException
131
     *         if an exception occurred during process exception
132
     */
133
    public static int invokeProcess(String[] commandLine, Consumer<String> consumer)
134
            throws IOException, InterruptedException {
135
        return invokeProcess(commandLine, null, new DelegatingConsumer(consumer));
1✔
136
    }
137

138
    /**
139
     * Runs the given set of command line arguments as a system process and returns the exit value of the spawned
140
     * process. Outputs of the process (both normal and error) are passed to the {@code consumer}.
141
     *
142
     * @param commandLine
143
     *         the list of command line arguments to run
144
     * @param consumer
145
     *         the consumer for the program's output
146
     *
147
     * @return the exit code of the process
148
     *
149
     * @throws IOException
150
     *         if an exception occurred while reading the process' outputs
151
     * @throws InterruptedException
152
     *         if an exception occurred during process exception
153
     */
154
    public static int invokeProcess(List<String> commandLine, Consumer<String> consumer)
155
            throws IOException, InterruptedException {
156
        return invokeProcess(commandLine, null, new DelegatingConsumer(consumer));
1✔
157
    }
158

159
    /**
160
     * Runs the given set of command line arguments as a system process and returns the exit value of the spawned
161
     * process. Additionally, allows to supply an input stream to the invoked program. Outputs of the process (both
162
     * normal and error) are passed to the {@code consumer}.
163
     *
164
     * @param commandLine
165
     *         the list of command line arguments to run
166
     * @param input
167
     *         the input passed to the program
168
     * @param consumer
169
     *         the consumer for the program's output
170
     *
171
     * @return the exit code of the process
172
     *
173
     * @throws IOException
174
     *         if an exception occurred while reading the process' outputs, or writing the process' inputs
175
     * @throws InterruptedException
176
     *         if an exception occurred during process exception
177
     */
178
    public static int invokeProcess(String[] commandLine, Reader input, Consumer<String> consumer)
179
            throws IOException, InterruptedException {
180
        return invokeProcess(commandLine, input, new DelegatingConsumer(consumer));
1✔
181
    }
182

183
    /**
184
     * Runs the given set of command line arguments as a system process and returns the exit value of the spawned
185
     * process. Additionally, allows to supply an input stream to the invoked program. Outputs of the process (both
186
     * normal and error) are passed to the {@code consumer}.
187
     *
188
     * @param commandLine
189
     *         the list of command line arguments to run
190
     * @param input
191
     *         the input passed to the program
192
     * @param consumer
193
     *         the consumer for the program's output
194
     *
195
     * @return the exit code of the process
196
     *
197
     * @throws IOException
198
     *         if an exception occurred while reading the process' outputs, or writing the process' inputs
199
     * @throws InterruptedException
200
     *         if an exception occurred during process exception
201
     */
202
    public static int invokeProcess(List<String> commandLine, Reader input, Consumer<String> consumer)
203
            throws IOException, InterruptedException {
204
        return invokeProcess(commandLine, input, new DelegatingConsumer(consumer));
×
205
    }
206

207
    private static int invokeProcess(String[] commandLine, @Nullable Reader input, InputStreamConsumer consumer)
208
            throws IOException, InterruptedException {
209
        return invokeProcess(new ProcessBuilder(commandLine), input, consumer);
1✔
210
    }
211

212
    private static int invokeProcess(List<String> commandLine, @Nullable Reader input, InputStreamConsumer consumer)
213
            throws IOException, InterruptedException {
214
        return invokeProcess(new ProcessBuilder(commandLine), input, consumer);
1✔
215
    }
216

217
    private static int invokeProcess(ProcessBuilder processBuilder,
218
                                     @Nullable Reader input,
219
                                     InputStreamConsumer consumer) throws IOException, InterruptedException {
220

221
        processBuilder.redirectErrorStream(true);
1✔
222
        final Process process = processBuilder.start();
1✔
223

224
        writeProcessInput(process, input);
1✔
225

226
        // consume process output to prevent blocking from full buffers
227
        consumer.consume(process.getInputStream());
1✔
228

229
        try {
230
            return process.waitFor();
1✔
231
        } finally {
232
            // cleanup
233
            process.destroy();
1✔
234
        }
235
    }
236

237
    /**
238
     * Builds and starts a system process for the given set of command line arguments. Additionally, allows to supply an
239
     * input stream to the invoked program, as well as independent consumers for the process' standard and error output.
240
     * <p>
241
     * The consumers for the process' outputs run in separate threads, preventing potential deadlock scenarios where
242
     * client code waits for the process' termination (e.g. {@link Process#waitFor()}) which is blocked by full system
243
     * buffers.
244
     *
245
     * @param commandLine
246
     *         the list of command line arguments to run
247
     * @param input
248
     *         the input passed to the program, maybe be {@code null} if the process expects no input
249
     * @param stdOutConsumer
250
     *         the consumer for the programs output
251
     * @param stdErrConsumer
252
     *         the consumer for the programs output
253
     *
254
     * @return the reference to the running process
255
     *
256
     * @throws IOException
257
     *         if an exception occurred while reading the process outputs
258
     */
259
    public static Process buildProcess(String[] commandLine,
260
                                       @Nullable Reader input,
261
                                       @Nullable Consumer<String> stdOutConsumer,
262
                                       @Nullable Consumer<String> stdErrConsumer) throws IOException {
263

264
        final ProcessBuilder processBuilder = new ProcessBuilder(commandLine);
1✔
265
        final Process process = processBuilder.start();
1✔
266

267
        writeProcessInput(process, input);
1✔
268

269
        if (stdOutConsumer != null) {
1✔
270
            final Thread stdOutThread =
×
271
                    new StreamGobbler(process.getInputStream(), new DelegatingConsumer(stdOutConsumer));
×
272
            stdOutThread.start();
×
273
        }
274

275
        if (stdErrConsumer != null) {
1✔
276
            final Thread stdErrThread =
1✔
277
                    new StreamGobbler(process.getErrorStream(), new DelegatingConsumer(stdErrConsumer));
1✔
278
            stdErrThread.start();
1✔
279
        }
280

281
        return process;
1✔
282
    }
283

284
    private static void writeProcessInput(Process process, @Nullable Reader input) throws IOException {
285
        if (input != null) {
1✔
286
            try (OutputStream processInput = process.getOutputStream();
1✔
287
                 Writer writer = IOUtil.asBufferedUTF8Writer(processInput)) {
1✔
288
                IOUtil.copy(input, writer);
1✔
289
            }
290
        }
291
    }
1✔
292

293
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc