Package com.diffplug.spotless
Class ProcessRunner
- java.lang.Object
-
- com.diffplug.spotless.ProcessRunner
-
- All Implemented Interfaces:
AutoCloseable
public class ProcessRunner extends Object implements AutoCloseable
Shelling out to a process is harder than it ought to be in Java. If you don't read stdout and stderr on their own threads, you risk deadlock on a clogged buffer. ProcessRunner allocates two threads specifically for the purpose of flushing stdout and stderr to buffers. These threads will remain alive until the ProcessRunner is closed, so it is especially useful for repeated calls to an external process.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
ProcessRunner.LongRunningProcess
A long-running process that can be waited for.static class
ProcessRunner.Result
-
Constructor Summary
Constructors Constructor Description ProcessRunner()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
ProcessRunner.Result
exec(byte[] stdin, String... args)
Creates a process with the given arguments, the given byte array is written to stdin immediately.ProcessRunner.Result
exec(byte[] stdin, List<String> args)
Creates a process with the given arguments, the given byte array is written to stdin immediately.ProcessRunner.Result
exec(File cwd, Map<String,String> environment, byte[] stdin, List<String> args)
Creates a process with the given arguments, the given byte array is written to stdin immediately.ProcessRunner.Result
exec(String... args)
Creates a process with the given arguments.ProcessRunner.Result
exec(List<String> args)
Creates a process with the given arguments.ProcessRunner.Result
shell(String cmd)
Executes the given shell command (usingcmd
on windows andsh
on unix).ProcessRunner.Result
shellWinUnix(File cwd, Map<String,String> environment, String cmdWin, String cmdUnix)
Executes the given shell command (usingcmd
on windows andsh
on unix).ProcessRunner.Result
shellWinUnix(String cmdWin, String cmdUnix)
Executes the given shell command (usingcmd
on windows andsh
on unix).ProcessRunner.LongRunningProcess
start(File cwd, Map<String,String> environment, byte[] stdin, boolean redirectErrorStream, List<String> args)
Creates a process with the given arguments, the given byte array is written to stdin immediately.ProcessRunner.LongRunningProcess
start(File cwd, Map<String,String> environment, byte[] stdin, List<String> args)
Creates a process with the given arguments, the given byte array is written to stdin immediately.static ProcessRunner
usingRingBuffersOfCapacity(int limit)
-
-
-
Method Detail
-
usingRingBuffersOfCapacity
public static ProcessRunner usingRingBuffersOfCapacity(int limit)
-
shell
public ProcessRunner.Result shell(String cmd) throws IOException, InterruptedException
Executes the given shell command (usingcmd
on windows andsh
on unix).- Throws:
IOException
InterruptedException
-
shellWinUnix
public ProcessRunner.Result shellWinUnix(String cmdWin, String cmdUnix) throws IOException, InterruptedException
Executes the given shell command (usingcmd
on windows andsh
on unix).- Throws:
IOException
InterruptedException
-
shellWinUnix
public ProcessRunner.Result shellWinUnix(@Nullable File cwd, @Nullable Map<String,String> environment, String cmdWin, String cmdUnix) throws IOException, InterruptedException
Executes the given shell command (usingcmd
on windows andsh
on unix).- Throws:
IOException
InterruptedException
-
exec
public ProcessRunner.Result exec(String... args) throws IOException, InterruptedException
Creates a process with the given arguments.- Throws:
IOException
InterruptedException
-
exec
public ProcessRunner.Result exec(@Nullable byte[] stdin, String... args) throws IOException, InterruptedException
Creates a process with the given arguments, the given byte array is written to stdin immediately.- Throws:
IOException
InterruptedException
-
exec
public ProcessRunner.Result exec(List<String> args) throws IOException, InterruptedException
Creates a process with the given arguments.- Throws:
IOException
InterruptedException
-
exec
public ProcessRunner.Result exec(@Nullable byte[] stdin, List<String> args) throws IOException, InterruptedException
Creates a process with the given arguments, the given byte array is written to stdin immediately.- Throws:
IOException
InterruptedException
-
exec
public ProcessRunner.Result exec(@Nullable File cwd, @Nullable Map<String,String> environment, @Nullable byte[] stdin, List<String> args) throws IOException, InterruptedException
Creates a process with the given arguments, the given byte array is written to stdin immediately.- Throws:
IOException
InterruptedException
-
start
public ProcessRunner.LongRunningProcess start(@Nullable File cwd, @Nullable Map<String,String> environment, @Nullable byte[] stdin, List<String> args) throws IOException
Creates a process with the given arguments, the given byte array is written to stdin immediately.
Delegates tostart(File, Map, byte[], boolean, List)
withfalse
forredirectErrorStream
.- Throws:
IOException
-
start
public ProcessRunner.LongRunningProcess start(@Nullable File cwd, @Nullable Map<String,String> environment, @Nullable byte[] stdin, boolean redirectErrorStream, List<String> args) throws IOException
Creates a process with the given arguments, the given byte array is written to stdin immediately.
The process is not waited for, so the caller is responsible for callingProcessRunner.LongRunningProcess.waitFor()
(if needed).
To dispose thisProcessRunner
instance, either callclose()
orProcessRunner.LongRunningProcess.close()
. Afterclose()
orProcessRunner.LongRunningProcess.close()
has been called, thisProcessRunner
instance must not be used anymore.- Throws:
IOException
-
close
public void close()
- Specified by:
close
in interfaceAutoCloseable
-
-