com.ajjpj.abase.proc.CliCommand Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of a-base Show documentation
Show all versions of a-base Show documentation
a-base is a library of basic (hence the name) classes, most notably immutable collection classes with copy-on-write operations
package com.ajjpj.abase.proc;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
/**
* This utility class executes a command as a separate process, returning its output (i.e. stdout) as a list of strings, with each
* line as a separate element.
*
* To guard the calling JVM against overflow, it limits the number of lines of output that are stored.
*
* NB: This class executes the command *synchronously*, blocking until it returns. For long running commands, that may
* or may not be a problem for calling code.
*
* @author arno
*/
@SuppressWarnings("unused")
public class CliCommand {
private final List stdout = new ArrayList<>();
private final int returnCode;
@SuppressWarnings("unused")
public CliCommand(String... cmd) throws IOException, InterruptedException {
this(1000, cmd);
}
public CliCommand(int maxLinesOfOutput, String... cmd) throws IOException, InterruptedException {
this(maxLinesOfOutput, null, cmd);
}
public CliCommand(int maxLinesOfOutput, Pattern grep, String... cmd) throws IOException, InterruptedException {
final ProcessBuilder pb = new ProcessBuilder(cmd);
final Process process = pb.start();
final BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream())); // platform encoding is intended here!
String line;
while(stdout.size() < maxLinesOfOutput && (line = in.readLine()) != null) {
if(grep == null || grep.matcher(line).matches()) {
stdout.add(line);
}
}
returnCode = process.waitFor();
}
@SuppressWarnings("unused")
public List getOutput() {
return stdout;
}
@SuppressWarnings("unused")
public int getReturnCode() {
return returnCode;
}
}