Package com.diffplug.spotless
Class ProcessRunner
- java.lang.Object
-
- com.diffplug.spotless.ProcessRunner
-
- All Implemented Interfaces:
java.lang.AutoCloseable
public class ProcessRunner extends java.lang.Object implements java.lang.AutoCloseableShelling 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 classProcessRunner.LongRunningProcessA long-running process that can be waited for.static classProcessRunner.Result
-
Constructor Summary
Constructors Constructor Description ProcessRunner()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()ProcessRunner.Resultexec(byte[] stdin, java.lang.String... args)Creates a process with the given arguments, the given byte array is written to stdin immediately.ProcessRunner.Resultexec(byte[] stdin, java.util.List<java.lang.String> args)Creates a process with the given arguments, the given byte array is written to stdin immediately.ProcessRunner.Resultexec(java.io.File cwd, java.util.Map<java.lang.String,java.lang.String> environment, byte[] stdin, java.util.List<java.lang.String> args)Creates a process with the given arguments, the given byte array is written to stdin immediately.ProcessRunner.Resultexec(java.lang.String... args)Creates a process with the given arguments.ProcessRunner.Resultexec(java.util.List<java.lang.String> args)Creates a process with the given arguments.ProcessRunner.Resultshell(java.lang.String cmd)Executes the given shell command (usingcmdon windows andshon unix).ProcessRunner.ResultshellWinUnix(java.io.File cwd, java.util.Map<java.lang.String,java.lang.String> environment, java.lang.String cmdWin, java.lang.String cmdUnix)Executes the given shell command (usingcmdon windows andshon unix).ProcessRunner.ResultshellWinUnix(java.lang.String cmdWin, java.lang.String cmdUnix)Executes the given shell command (usingcmdon windows andshon unix).ProcessRunner.LongRunningProcessstart(java.io.File cwd, java.util.Map<java.lang.String,java.lang.String> environment, byte[] stdin, boolean redirectErrorStream, java.util.List<java.lang.String> args)Creates a process with the given arguments, the given byte array is written to stdin immediately.ProcessRunner.LongRunningProcessstart(java.io.File cwd, java.util.Map<java.lang.String,java.lang.String> environment, byte[] stdin, java.util.List<java.lang.String> args)Creates a process with the given arguments, the given byte array is written to stdin immediately.static ProcessRunnerusingRingBuffersOfCapacity(int limit)
-
-
-
Method Detail
-
usingRingBuffersOfCapacity
public static ProcessRunner usingRingBuffersOfCapacity(int limit)
-
shell
public ProcessRunner.Result shell(java.lang.String cmd) throws java.io.IOException, java.lang.InterruptedException
Executes the given shell command (usingcmdon windows andshon unix).- Throws:
java.io.IOExceptionjava.lang.InterruptedException
-
shellWinUnix
public ProcessRunner.Result shellWinUnix(java.lang.String cmdWin, java.lang.String cmdUnix) throws java.io.IOException, java.lang.InterruptedException
Executes the given shell command (usingcmdon windows andshon unix).- Throws:
java.io.IOExceptionjava.lang.InterruptedException
-
shellWinUnix
public ProcessRunner.Result shellWinUnix(@Nullable java.io.File cwd, @Nullable java.util.Map<java.lang.String,java.lang.String> environment, java.lang.String cmdWin, java.lang.String cmdUnix) throws java.io.IOException, java.lang.InterruptedException
Executes the given shell command (usingcmdon windows andshon unix).- Throws:
java.io.IOExceptionjava.lang.InterruptedException
-
exec
public ProcessRunner.Result exec(java.lang.String... args) throws java.io.IOException, java.lang.InterruptedException
Creates a process with the given arguments.- Throws:
java.io.IOExceptionjava.lang.InterruptedException
-
exec
public ProcessRunner.Result exec(@Nullable byte[] stdin, java.lang.String... args) throws java.io.IOException, java.lang.InterruptedException
Creates a process with the given arguments, the given byte array is written to stdin immediately.- Throws:
java.io.IOExceptionjava.lang.InterruptedException
-
exec
public ProcessRunner.Result exec(java.util.List<java.lang.String> args) throws java.io.IOException, java.lang.InterruptedException
Creates a process with the given arguments.- Throws:
java.io.IOExceptionjava.lang.InterruptedException
-
exec
public ProcessRunner.Result exec(@Nullable byte[] stdin, java.util.List<java.lang.String> args) throws java.io.IOException, java.lang.InterruptedException
Creates a process with the given arguments, the given byte array is written to stdin immediately.- Throws:
java.io.IOExceptionjava.lang.InterruptedException
-
exec
public ProcessRunner.Result exec(@Nullable java.io.File cwd, @Nullable java.util.Map<java.lang.String,java.lang.String> environment, @Nullable byte[] stdin, java.util.List<java.lang.String> args) throws java.io.IOException, java.lang.InterruptedException
Creates a process with the given arguments, the given byte array is written to stdin immediately.- Throws:
java.io.IOExceptionjava.lang.InterruptedException
-
start
public ProcessRunner.LongRunningProcess start(@Nullable java.io.File cwd, @Nullable java.util.Map<java.lang.String,java.lang.String> environment, @Nullable byte[] stdin, java.util.List<java.lang.String> args) throws java.io.IOException
Creates a process with the given arguments, the given byte array is written to stdin immediately.
Delegates tostart(File, Map, byte[], boolean, List)withfalseforredirectErrorStream.- Throws:
java.io.IOException
-
start
public ProcessRunner.LongRunningProcess start(@Nullable java.io.File cwd, @Nullable java.util.Map<java.lang.String,java.lang.String> environment, @Nullable byte[] stdin, boolean redirectErrorStream, java.util.List<java.lang.String> args) throws java.io.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 thisProcessRunnerinstance, either callclose()orProcessRunner.LongRunningProcess.close(). Afterclose()orProcessRunner.LongRunningProcess.close()has been called, thisProcessRunnerinstance must not be used anymore.- Throws:
java.io.IOException
-
close
public void close()
- Specified by:
closein interfacejava.lang.AutoCloseable
-
-